这是我的代码:
public void playSound(String uri,int flag)
{
Uri uri1=Uri.parse(uri);
ContestVoteFragment fragment = (ContestVoteFragment) fragments.get(0);
if (fragment.playSound(flag))
{
if (mediaPlayer==null)
{
mediaPlayer=new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(uri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}else {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer=new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(uri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
}
}else {
if (mediaPlayer!=null)
{
if (mediaPlayer.isPlaying())
{
mediaPlayer.stop();
}
}
}
}
}
但是当我播放Internet mp3时,错误消息是无法打开http://p9snltngh.bkt.clouddn.com/Fs3M3Cq_Mkex9AsunJt57N22JQp2:java.io.FileNotFoundException:没有内容提供者 我听不懂,因为这个uri可以玩“ http://p9snltngh.bkt.clouddn.com/FhEbUwGYcmQbXIVMVzd0GyPypDVC”
答案 0 :(得分:0)
两个链接均正常工作
public void playSound(String uri){
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(uri);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
快乐的编码:)
答案 1 :(得分:0)
解决方案::您需要将String URI解析为URI类。所以,
代替:
try {
mediaPlayer.setDataSource(uri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
尝试这样:
try {
mediaPlayer.setDataSource(this, Uri.parse(uri));
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
如果有任何问题,请发表评论。