【问题标题】:IOException Error on Putting Uri Source In Media Player (setDataSource)将 Uri 源放入媒体播放器 (setDataSource) 时出现 IOException 错误
【发布时间】:2017-02-06 11:17:20
【问题描述】:

使用 setDataSource 将 Uri 源放入 MediaPlayer 对象时出现 IOException 错误。尝试从移动设备中获取歌曲的 Uri

Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 10);

onActivityResult

 @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            if(resultCode == RESULT_OK && requestCode == 10){
                Uri uriSound=data.getData();
                Toast.makeText(getApplicationContext(),"It Came Here",Toast.LENGTH_SHORT).show();
                play_sound(uriSound);
            }
        }

play_sound

private void play_sound( Uri uri) {


             Toast.makeText(getApplicationContext(),"It Came Here 2" + uri,Toast.LENGTH_LONG).show();

            MediaPlayer mp = new MediaPlayer();

            try {

                mp.setDataSource(getApplicationContext(), uri);
                mp.start();
            } catch (IllegalArgumentException e) {

                Toast.makeText(getApplicationContext(),"Error 1",Toast.LENGTH_LONG).show();
                e.printStackTrace();
            } catch (SecurityException e) {
                Toast.makeText(getApplicationContext(),"Error 2",Toast.LENGTH_LONG).show();
                e.printStackTrace();
            } catch (IllegalStateException e) {
                Toast.makeText(getApplicationContext(),"Error 3",Toast.LENGTH_LONG).show();
                e.printStackTrace();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(),"Error 4",Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

        }

我用 Toast 来检查哪里出错了。

在“IOException e”中出现错误我不知道在谷歌上搜索它是什么我得到的只是它是所选文件的某种“文件格式”问题。

【问题讨论】:

    标签: android-mediaplayer ioexception file-type


    【解决方案1】:

    解决了!!只需在播放声音中添加一行

     mP.setAudioStreamType(AudioManager.STREAM_MUSIC);
    

    Play_Sound(Uri uri):

    private void Play_Sound(Uri uri)
            {
                MediaPlayer mPlayer = new MediaPlayer();
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                try {
                    mPlayer.setDataSource(getApplicationContext(), uri);
                } catch (IllegalArgumentException e) {
                    Toast.makeText(getApplicationContext(), "You might 1 not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (SecurityException e) {
                    Toast.makeText(getApplicationContext(), "You might 2 not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might 3 not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "You might 4 not set the URI correctly!", Toast.LENGTH_LONG).show();
    
                    e.printStackTrace();
                }
                try {
                    mPlayer.prepare();
                } catch (IllegalStateException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
                }
                mPlayer.start();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多