Code examples for Playing audio and video

Codota analyzed 7,668,838 open source code samples to find the most common, credible and clear examples.
You can now enable Codota on your own code to easily search and navigate your Java codebase.
			mp.start(); 
			state = PLAYING; 
		} 
		else if ( state == PENDING_LOOP ) 
		{ 
			mp.setLooping(true); 
			mp.seekTo(0); 
			mp.start(); 
			state = LOOPING; 
		} 
		else 
		{ 
			state = PREPARED; 
			mp.seekTo(0); 
		} 
	} 
	 
	public void onCompletion(MediaPlayer mPlayer) 
	{ 
		if (state != LOOPING) 
Full Snippet Info
10
CodeRank
    public void setListener(RecognizerApiListener listener) { 
    	this.mListener = listener; 
    } 
 
    public static void play(Activity activity, String file, PlaybackExceptionHandler handler) { 
        MediaPlayer mediaPlayer = new MediaPlayer(); 
 
        try { 
            mediaPlayer.setDataSource(file); 
            mediaPlayer.prepare(); 
            mediaPlayer.start(); 
        } catch (Exception e) { 
        	handler.playbackFailed(file); 
        } 
    } 
 
    private AACEncoder encoder = new AACEncoder(); 
    private long speechStarted = 0; 
    private SpeechRecognizer sr; 
    private ProgressDialog speakPd; 
Full Snippet Info
10
CodeRank
                Settings.System.MODE_RINGER_STREAMS_AFFECTED, 0); 
 
        if ((silentModeStreams & (1 << AudioManager.STREAM_ALARM)) != 0) { 
            mPlayer.setAudioStreamType(silentModeStreams); 
        } else { 
            mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); 
        } 
        try { 
            mPlayer.setDataSource(this, url); 
            mPlayer.prepare(); 
            mPlayer.setLooping(true); 
            mPlayer.start(); 
        } catch (IllegalArgumentException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (SecurityException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } catch (IllegalStateException e) { 
            // TODO Auto-generated catch block 
Full Snippet Info
8
CodeRank
		return duration; 
	} 
 
	public void onPrepared(MediaPlayer mPlayer) { 
		if (isPlaying) { 
			mPlayer.setOnCompletionListener(this); 
			mPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() 
			{ 
				public void onBufferingUpdate(MediaPlayer mPlayer, int percent) 
				{ 
					/* TODO: call back, e.g. update outer progress bar */ 
					Log.d("AudioOnBufferingUpdate", "percent: " + percent);  
				} 
			}); 
			mPlayer.start(); 
		} 
	} 
 
	public boolean onError(MediaPlayer mPlayer, int arg1, int arg2) { 
		Log.e("AUDIO onError", "error " + arg1 + " " + arg2); 
Full Snippet Info
8
CodeRank
 
    } 
 
    public void audioPlayback(String filePath) { 
        try { 
            MediaPlayer mp = new MediaPlayer(); 
            mp.setDataSource(filePath); 
            mp.prepare(); 
            mp.start(); 
            Thread.sleep(200000); 
            mp.stop(); 
            mp.release(); 
        } catch (Exception e) { 
            Log.v(TAG, e.toString()); 
            assertTrue("MP3 Playback", false); 
        } 
    } 
 
    // A very simple test case which start the audio player. 
    // Power measurment will be done in other application. 
Full Snippet Info
8
CodeRank
         
        public void onPrepared(MediaPlayer mp) { 
            mAudioSrvState = PLAYING; 
            Time duration = new Time(mp.getDuration()); 
            Log.v(TAG, "Prepared mediaplayer for track of duration =["+duration+"]"); 
            mMediaPlayer.start(); 
        } 
         
        public void onBufferingUpdate(MediaPlayer mp, int percent) { 
            Log.v(TAG, "Buffered additional=["+percent+"%]"); 
            mAudioSrvState = BUFFERING; 
        } 
         
        public void onCompletion(MediaPlayer mp) { 
            Log.i(TAG, "Completed playback"); 
            mMediaPlayer.stop(); 
            mMediaPlayer.reset(); 
            mAudioSrvState = NOT_PLAYING; 
        } 
         
Full Snippet Info
8
CodeRank
		return player; 
	} 
 
	private static void playFile(final MediaPlayer player, final AssetFileDescriptor file) throws IllegalArgumentException, IllegalStateException, IOException { 
	    // When the beep has finished playing, rewind to queue up another one. 
		player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
	      @Override 
	      public void onCompletion(MediaPlayer player) { 
	        player.release(); 
	      } 
	    }); 
	    player.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); 
	    file.close(); 
	    player.setVolume(0.2f, 0.2f); 
	    player.prepare(); 
	    player.start(); 
	} 
} 
Full Snippet Info
8
CodeRank
 
    //Load midi file from resources 
    public static boolean resourcesPlayback(AssetFileDescriptor afd, int expectedDuration){ 
        int duration = 0; 
        try{ 
            MediaPlayer mp = new MediaPlayer(); 
            mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength()); 
            mp.prepare(); 
            mp.start(); 
            duration = mp.getDuration(); 
            Thread.sleep(5000); 
            mp.release(); 
        }catch (Exception e){ 
            Log.v(TAG,e.getMessage()); 
        } 
        if (duration > expectedDuration) 
            return true; 
        else 
            return false; 
    } 
Full Snippet Info
8
CodeRank
    } 
     
    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; 
Full Snippet Info
8
CodeRank
    } 
  } 
 
  void playBeepSoundAndVibrate() { 
    if (playBeep && mediaPlayer != null) { 
      mediaPlayer.start(); 
    } 
    if (vibrate) { 
      Vibrator vibrator = (Vibrator) activity.getSystemService(Context.VIBRATOR_SERVICE); 
      vibrator.vibrate(VIBRATE_DURATION); 
    } 
  } 
 
  private static boolean shouldBeep(SharedPreferences prefs, Context activity) { 
    boolean shouldPlayBeep = prefs.getBoolean(PreferencesActivity.KEY_PLAY_BEEP, true); 
    if (shouldPlayBeep) { 
      // See if sound settings overrides this 
      AudioManager audioService = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE); 
      if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) { 
        shouldPlayBeep = false; 
Full Snippet Info
8
CodeRank