使用 Media 在 Android 中使用 URL 播放媒体
Play Media using URL in Android using Media
我正在尝试使用 Media Player 播放 URL 中的音乐,但播放时出现错误
final MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(url[0]);
mp.prepare();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp.start();
}
});
} catch(Exception e){
System.out.println(e);
}
我也试过使用 URI
Uri uri = Uri.parse(url[0]);
mp.setDataSource(context,uri);
mp.prepare();
并且还尝试使用 mp.prepareAsync();方法
但随后也会出现此类错误并且无法播放媒体
W/MediaPlayer: Couldn't open
java.io.FileNotFoundException: No content provider:
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1675)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1505)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1422)
at android.media.MediaPlayer.attemptDataSource(MediaPlayer.java:1101)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1073)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1001)
at com.tuwy.Dailog.DialogClass.showAudioDialog(DialogClass.java:272)
也得到这个
W/AudioTrack: Use of stream types is deprecated for operations other than volume control
See the documentation of AudioTrack() for what to use instead with android.media.AudioAttributes to qualify your playback use case
V/AudioTrack: pause(2131): prior state:STATE_ACTIVE
I/System.out: java.io.FileNotFoundException: : open failed: ENOENT (No such file or directory)
尝试将 url 解析为 Uri.parse(url),这可能有效。
我遇到了同样的问题,这段代码解决了我的问题
我用另一个线程播放来自url的音乐...
final Handler handler =new Handler();
final Thread audioThread = new Thread(new Runnable() {
@Override
public void run() {
final MediaPlayer mediaPlayer = new MediaPlayer();
Uri myUri = Uri.parse(url[0]);
try {
mediaPlayer.setDataSource(context, myUri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
btnPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
if (mediaPlayer.getCurrentPosition() > 0)
mediaPlayer.seekTo(0);
mediaPlayer.start();
}
}
});
}
});
}
});
** url[0] 表示您的音乐 url 和 btnplaypause 是播放和暂停音频的按钮
希望这也能解决您的问题...
我正在尝试使用 Media Player 播放 URL 中的音乐,但播放时出现错误
final MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(url[0]);
mp.prepare();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp.start();
}
});
} catch(Exception e){
System.out.println(e);
}
我也试过使用 URI
Uri uri = Uri.parse(url[0]);
mp.setDataSource(context,uri);
mp.prepare();
并且还尝试使用 mp.prepareAsync();方法
但随后也会出现此类错误并且无法播放媒体
W/MediaPlayer: Couldn't open
java.io.FileNotFoundException: No content provider:
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1675)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1505)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1422)
at android.media.MediaPlayer.attemptDataSource(MediaPlayer.java:1101)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1073)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1001)
at com.tuwy.Dailog.DialogClass.showAudioDialog(DialogClass.java:272)
也得到这个
W/AudioTrack: Use of stream types is deprecated for operations other than volume control
See the documentation of AudioTrack() for what to use instead with android.media.AudioAttributes to qualify your playback use case
V/AudioTrack: pause(2131): prior state:STATE_ACTIVE
I/System.out: java.io.FileNotFoundException: : open failed: ENOENT (No such file or directory)
尝试将 url 解析为 Uri.parse(url),这可能有效。
我遇到了同样的问题,这段代码解决了我的问题
我用另一个线程播放来自url的音乐...
final Handler handler =new Handler();
final Thread audioThread = new Thread(new Runnable() {
@Override
public void run() {
final MediaPlayer mediaPlayer = new MediaPlayer();
Uri myUri = Uri.parse(url[0]);
try {
mediaPlayer.setDataSource(context, myUri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
btnPlayPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
} else {
if (mediaPlayer.getCurrentPosition() > 0)
mediaPlayer.seekTo(0);
mediaPlayer.start();
}
}
});
}
});
}
});
** url[0] 表示您的音乐 url 和 btnplaypause 是播放和暂停音频的按钮
希望这也能解决您的问题...