MediaPlayer setOnErrorListener - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for MediaPlayer setOnErrorListener method.

This code example shows how to use the following methods:setOnCompletionListener, setOnErrorListener
	@Override 
	public void onCreate() 
	{ 
		super.onCreate(); 
		Log.i(TAG,"onCreate()");
		player = new MediaPlayer();
		setupPlayerListeners(); 
	} 
	 
	private void setupPlayerListeners() 
	{ 
		if(player != null)
		{ 
			player.setOnCompletionListener(new OnCompletionListener() {
 
				@Override 
				public void onCompletion(MediaPlayer mp) {
					Log.i(TAG,"onCompletion called");
					 
				} }); 

8
CodeRank
This code example shows how to use the following methods:setAudioStreamType, setDataSource, setLooping, setOnErrorListener
    vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
    player = createPlayer();
  } 
 
  private MediaPlayer createPlayer() {
    MediaPlayer newPlayer = new MediaPlayer();
    try { 
      Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
      newPlayer.setOnErrorListener(playerErrorListener);
      newPlayer.setDataSource(context, ringtoneUri);
      newPlayer.setLooping(true);
      newPlayer.setAudioStreamType(AudioManager.STREAM_RING);
      return newPlayer;
    } catch (IOException e) {
      Log.e(TAG, "Failed to create player for incoming call ringer");
      return null; 
    } 
  } 
 
  public void start() { 
8
CodeRank
This code example shows how to use the following methods:setAudioStreamType, setOnBufferingUpdateListener, setOnCompletionListener, setOnErrorListener, setOnPreparedListener
This code example shows how to use the following methods:prepare, setDataSource, setOnCompletionListener, setOnErrorListener, start
    } 
     
    public void startPlayback() { 
        stop(); 
         
        mPlayer = new MediaPlayer();
        try { 
            mPlayer.setDataSource(mSampleFile.getAbsolutePath());
            mPlayer.setOnCompletionListener(this);
            mPlayer.setOnErrorListener(this);
            mPlayer.prepare();
            mPlayer.start();
        } catch (IllegalArgumentException e) {
            setError(INTERNAL_ERROR);
            mPlayer = null;
            return; 
        } catch (IOException e) {
            setError(SDCARD_ACCESS_ERROR);
            mPlayer = null;
            return; 
8
CodeRank
This code example shows how to use the following methods:setOnCompletionListener, setOnErrorListener
	protected void setListeners() 
	{ 
		if(player != null)
		{ 
			if(onCompletionListener != null)
				player.setOnCompletionListener(onCompletionListener);
			if(onErrorListener != null)
				player.setOnErrorListener(onErrorListener);
		} 
	} 
	 
	/** 
	 * play a file with given resource id 
	 *  
	 * @param context 
	 * @param resid 
	 */ 
	synchronized public void play(Context context, int resid)
	{ 
		Logger.v("trying to play: " + resid);
7
CodeRank
This code example shows how to use the following methods:setOnCompletionListener, setOnErrorListener, setOnPreparedListener, setWakeMode
     * Makes sure the media player exists and has been reset. This will create the media player 
     * if needed. reset the existing media player if one already exists. 
     */ 
    protected void createMediaPlayerIfNeeded() { 
        if (mPlayer == null) {
            mPlayer = new MediaPlayer();
 
            // make sure the CPU won't go to sleep while media is playing 
            mPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
 
            // the media player will notify the service when it's ready preparing, and when it's done playing 
            mPlayer.setOnPreparedListener(this);
            mPlayer.setOnCompletionListener(this);
            mPlayer.setOnErrorListener(this);
             
        } else { 
            mPlayer.reset();
        } 
    } 
 
7
CodeRank
This code example shows how to use the following methods:prepareAsync, setAudioStreamType, setDataSource, setOnErrorListener, setOnPreparedListener
			Log.i(TAG, "MediaStreamerService.play() - AudioFocus request denied");
			 
			notifyStreamError(AUDIO_FOCUS_DENIED_ERROR);
		} 
		 
		mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnErrorListener(this);
        mMediaPlayer.setOnPreparedListener(this);
         
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
        mMediaPlayer.setVolume(1.0f, 1.0f);
    	try { 
        	mMediaPlayer.setDataSource(this, Uri.parse(urlToStream));
		} catch (Exception e){
    		Log.e(TAG, "MediaStreamerService.play() - Error setting data source for the media player", e);
    		stop(); 
    		notifyStreamError(MEDIA_PLAYER_ERROR);
    		return; 
    	} 
    	try{ 

6
CodeRank
This code example shows how to use the following methods:prepareAsync, setAudioStreamType, setDataSource, setOnCompletionListener, setOnErrorListener
			Log.d("start", "OK I hear you, lets start!");
			m_state = STARTING;
			try { 
				if (m_media_player == null) {
					Log.d("start", "I didn't exist yet so let me get prepared.");
					m_media_player = new MediaPlayer();
					m_media_player.setDataSource(url_string);
					m_media_player.setAudioStreamType(AudioManager.STREAM_MUSIC);
					m_media_player.setOnPreparedListener(this);
					m_media_player.setOnCompletionListener(this);
					m_media_player.setOnErrorListener(this);
					m_media_player.prepareAsync();
 
				} else if (!m_media_player.isPlaying()) {
					Log.d("start", "I exist but i'm not playing? let me fix that...");
					 
					if (m_state != STOPPED) {
						Log.d("start", "Killing current connection...");
						m_media_player.reset();
						m_media_player.release();
6
CodeRank
This code example shows how to use the following methods:prepare, setDataSource, setOnErrorListener
        }    
    } 
     
    private MediaPlayer createMediaPlayer(File mediaFile)
    throws IOException { 
    	MediaPlayer mPlayer = new MediaPlayer();
    	mPlayer.setOnErrorListener(
				new MediaPlayer.OnErrorListener() {
			        public boolean onError(MediaPlayer mp, int what, int extra) {
			        	Log.e(getClass().getName(), "Error in MediaPlayer: (" + what +") with extra (" +extra +")" );
			    		return false; 
			        } 
			    }); 
 
		//  It appears that for security/permission reasons, it is better to pass a FileDescriptor rather than a direct path to the File. 
		//  Also I have seen errors such as "PVMFErrNotSupported" and "Prepare failed.: status=0x1" if a file path String is passed to 
		//  setDataSource().  So unless otherwise noted, we use a FileDescriptor here. 
		FileInputStream fis = new FileInputStream(mediaFile);
		mPlayer.setDataSource(fis.getFD());
		mPlayer.prepare();

6
CodeRank
This code example shows how to use the following methods:setOnCompletionListener, setOnErrorListener, setOnPreparedListener, setWakeMode
     * Makes sure the media player exists and has been reset. This will create the media player 
     * if needed, or reset the existing media player if one already exists. 
     */ 
    void createMediaPlayerIfNeeded() { 
        if (mPlayer == null) {
            mPlayer = new MediaPlayer();
 
            // Make sure the media player will acquire a wake-lock while playing. If we don't do 
            // that, the CPU might go to sleep while the song is playing, causing playback to stop. 
            // 
            // Remember that to use this, we have to declare the android.permission.WAKE_LOCK 
            // permission in AndroidManifest.xml. 
            mPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
 
            // we want the media player to notify us when it's ready preparing, and when it's done 
            // playing: 
            mPlayer.setOnPreparedListener(this);
            mPlayer.setOnCompletionListener(this);
            mPlayer.setOnErrorListener(this);
        } 

7
CodeRank

Related MediaPlayer setOnErrorListener Questions & Answers:

setOnErrorListener Method Overview
Register a callback to be invoked when an error has happened during an asynchronous operation.
See Code Examples for other Android MediaPlayer Methods: