简体   繁体   English

Android MediaPlayer:从字符串变量引用时出现错误(1,-2147483648)

[英]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. 我正在使用jsoup抓取一些音频文件源URL,并将它们存储在字符串中。 If the string is later not null, a button is created with an onClick listener to play that file. 如果该字符串以后不为null,则将使用onClick侦听器创建一个按钮来播放该文件。 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. 如果将setDatasource直接设置为将被抓取的URL,则它可以工作,因此不是解码器问题。 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. 是与key变量一起使用的代码示例,此处重点是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. 是一个完全相同的代码示例,唯一的变化是对dataSource String的修改,但此代码不起作用。

If you want to run this code to check you will need to import the Jsoup library and also add my checker class . 如果要运行此代码进行检查,则需要导入Jsoup库并添加我的checker类

Replace 更换

mp.setDataSource(play1); mp.setDataSource(play1);

by : 创建人:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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