简体   繁体   中英

Android MediaPlayer: error (1, -2147483648) when referencing from string variable

I'm using jsoup to scrape some audio file source URLs and store them in a string. If the string is later not null, a button is created with an onClick listener to play that file. This has worked but in testing with different pages it has broken. If I setDatasource directly to the URL that would be scraped it works so it is not a decoder problem. Here is where the problem happens.

if (play1 != null) {

            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.buttonPanel);
            final Button btn = new Button(MainActivity.this);
            btn.setText("Play");
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    try {



                        mp.reset();
                        mp.setDataSource(play1);
                        mp.prepareAsync();
                        mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mp) {
                                mp.start();
                            }
                        });


                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

            try{
                linearLayout.addView(
                        btn,
                        new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.WRAP_CONTENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT)
                );
            } catch (NullPointerException e) {
            e.printStackTrace();
        }

        }

If you want to recreate the problem I have included the main functionality of my project which is needed to reproduce.
Here is an example of the code that works with the key variable to focus on here being the dataSource String.

And here is an example of the same exact code with the only change being to the dataSource String and this one does not work.

If you want to run this code to check you will need to import the Jsoup library and also add my checker class .

Replace

mp.setDataSource(play1);

by :

mp.setDataSource(play1.replaceAll(" ", "%20"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM