0

当我尝试在 android 设备中播放 .mov 文件时,如果我的设备中没有安装支持的视频播放器,我会收到一条错误消息“视频无法播放”。在这里,我只想将错误消息自定义为“不支持文件格式,请安装媒体播放器”。

伙计们请建议我如何解决这个问题。

非常感谢

4

1 回答 1

1

你试过OnErrorListener吗?它允许您从 MediaPlayer 捕获错误并做出相应的反应。如果您从 OnErrorListener.OnError 方法返回 true,则表明您已处理该错误,并且不应显示该错误。

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MediaActivity extends Activity implements OnErrorListener{

    MediaPlayer mpPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media);
        //Code to setup MediaPlayer
        mpPlayer.setOnErrorListener(this);
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        try{
            //Handle error however you like i.e with custom message and dialog
            return true; //This indicates that your code handled the error and the OS will not handle the error further.
        }catch(Exception exception){            
            return false;// reutrning false indicates that your code did not handle the error and the OS will display whatever the default error message is.
        }

    }
}
于 2013-04-30T13:20:23.953 回答