使用setDataSource将Uri源放入MediaPlayer的对象时出现IOException错误。尝试使用
从移动设备获取歌曲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"我不知道谷歌搜索它是什么它我得到的是它是什么样的"文件格式"选定文件的问题。
答案 0 :(得分:0)
解决!!只需在播放声音中添加一行
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();
}