android-如何捕获mediaplayer数据源无文件错误?
作者:互联网
我有一个mediaplayer应用程序,我在其中使用链接路径从服务器中获取数据源.问题是当我进行测试时,我故意删除了输入到mp.setDataSource(musicUri)的音频链接路径的数据源文件;检查,但我似乎无法捕捉到错误并解决.
我已经替换了mp.prepare();到mp.prepareAsync();和简单的mp.start()
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
});
在启动前先听Mediaplayer是否准备就绪.
这是堆栈跟踪:
07-23 13:55:52.492: E/MediaPlayer(6870): Attempt to call getDuration without a valid mediaplayer
07-23 13:55:52.492: E/MediaPlayer(6870): error (-38, 0)
07-23 13:55:52.492: E/MediaPlayer(6870): Error (-38,0)
07-23 13:55:52.492: E/MediaPlayer(6870): stop called in state 0
07-23 13:55:52.492: E/MediaPlayer(6870): error (-38, 0)
07-23 13:55:52.492: E/MediaPlayer(6870): error (1, -107)
似乎没有任何效果,我想捕获此错误以修复可能导致媒体播放器出现的异常结果.任何帮助都可以.谢谢!
编辑:
好的,所以我添加了onErrorListener,但仍然无法正常工作:
mp.reset();
mp.setDataSource(musicUri);
mp.prepareAsync();
// mp.prepare();
mp.setOnErrorListener(new one rrorListener() {
@Override
public boolean one rror(final MediaPlayer mp, final int what,
final int extra) {
Log.e(Constant.TAG_MYREC, "Error occurred while playing audio.");
mp.stop();
return false;
}
});
mp.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
});
解决方法:
mMediaPlayer.setOnErrorListener(new one rrorListener() {
@Override
public boolean one rror(final MediaPlayer mp, final int what,
final int extra) {
Log.e("Error occurred while playing audio.");
mp.stop();
在文件完全加载之前,您可能正在调用getDuration.查看this question的解决方案是否适合您.
标签:error-handling,try-catch,media-player,android-mediaplayer,android 来源: https://codeday.me/bug/20191030/1969335.html