2013-04-30 39 views
0

當我試圖在Android設備中播放.mov文件時,我收到一條錯誤消息 「視頻無法播放」(如果支持的視頻播放器尚未安裝在我的設備中)。在這裏,我只想將錯誤消息自定義爲「不支持文件格式,請安裝媒體播放器」。如何在android中自定義錯誤信息?

夥計們請告訴我如何解決這個問題。

非常感謝

+1

陷阱異常,然後拋出一個新的消息,你想要的。 – Simon 2013-04-30 12:46:10

+0

但是我不想在最初顯示默認錯誤信息,請您詳細說明它,以便它使我容易理解。 – SAHIL 2013-04-30 12:53:07

+0

查看此..http://developer.android.com/reference/android/widget/VideoView.html#setOnErrorListener%28android.media.MediaPlayer.OnErrorListener%29 – Simon 2013-04-30 12:55:36

回答

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. 
     } 

    } 
} 
相關問題