Android MediaPlayer - top ranked examples from Open Source projects

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

This code example shows how to use the following methods: release, stop
    public void onCompletion(MediaPlayer mediaplayer) {
        Timer endSound = new Timer();
        endSound.schedule(new TimerTask() {
            public void run() { 
                if (mp != null) {
                    mp.stop();
                    mp.release();
                    mp = null;
                } 
            } 
        }, 200); 
 
    } 
 
    /** 
     * Play sound, either locally (from assets) or remotely via URL. 
     *  
     * @param soundUri 
     *            the sound uri 
     */ 
Full Snippet Info
10
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: isPlaying, release, stop
	} 
 
	@Override 
	protected void onDialogClosed(final boolean positiveResult) {
		if (mMediaPlayer != null) {
			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.stop();
			} 
			mMediaPlayer.release();
			mMediaPlayer = null;
		} 
		if (positiveResult) {
			callChangeListener(mSelectedItem);
		} 
	} 
 
	@Override 
	protected void onPrepareDialogBuilder(final Builder builder) {
		builder.setSingleChoiceItems(getEntries(), mSelectedItem, new OnClickListener() {
 
Full Snippet Info
9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: release, stop
		}  
	} 
	 
	void unloadSound(){ 
		if(player!=null){
			player.release();
			player = null;
		} 
 
		if(soundID!=-1){
			pool.unload(soundID);
		} 
		fileName = null;
		soundID = -1;
		streamID = -1;
		bIsLoaded = false;
		bIsPlaying = false;
	} 
	 
	void play(){ 

9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:
    } 
 
    @Override 
    protected void setUp() throws Exception { 
      super.setUp(); 
      mPlayer = new MediaPlayer();
    } 
 
    @Override 
    protected void tearDown() throws Exception { 
        mPlayer.release();
        super.tearDown(); 
    } 
 
    // Generate a random number, sends it to the ping test player. 
    @Suppress 
    @MediumTest 
    public void testPing() throws Exception { 
        mPlayer.setDataSource("test:invoke_mock_media_player.so?url=ping");
 
Full Snippet Info
10
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: isPlaying, stop
    } 
  } 
   
  @Override 
  public void onPause() { 
    if (player.isPlaying()) {
      player.stop();
    } 
     
    super.onPause(); 
  } 
   
  public void onPrepared(MediaPlayer player) {
    player.start();
  } 
} 
Full Snippet Info
9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: isLooping, isPlaying, pause, release, seekTo
		} 
	} 
	 
	public void stop() throws IOException 
	{ 
		if ( mp.isLooping() || mp.isPlaying() )
		{ 
			state = INVALID;
			mp.pause();
			mp.seekTo(0);
		} 
	} 
	 
	public void loop() throws IOException 
	{ 
		invokePlay( true ); 
	} 
	 
	public void unload() throws IOException 
	{ 

9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: prepare, prepareAsync, setAudioStreamType, setDataSource, setOnPreparedListener
	}	 
	 
	protected void startPlaying(String file) {
		if (isPlaying==false) {
			try { 
				mPlayer = new MediaPlayer();
				isPlaying=true;
				Log.d("Audio startPlaying", "audio: " + file);
				if (isStreaming(file))
				{ 
					Log.d("AudioStartPlaying", "Streaming");
					// Streaming prepare async 
					mPlayer.setDataSource(file);
					mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);  
					mPlayer.prepareAsync();
				} else { 
					Log.d("AudioStartPlaying", "File");
					// Not streaming prepare synchronous, abstract base directory 
					mPlayer.setDataSource("/sdcard/" + file);
					mPlayer.prepare();

9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: prepareAsync, setAudioStreamType, setDataSource, setOnCompletionListener, setOnPreparedListener
    start(); 
  } 
 
  private void start() { 
    if( mediaPlayer != null ) mediaPlayer.release();
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnPreparedListener(this);
 
    String packageName = context.getPackageName();
    Uri dataUri = Uri.parse("android.resource://"+ packageName +"/"+ currentSoundID );
 
    try { 
      mediaPlayer.setDataSource(context, dataUri);
    } catch (IllegalArgumentException e) {
      // TODO Auto-generated catch block 
      e.printStackTrace();
      return; 
    } catch (SecurityException e) {

10
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: isPlaying, setAudioStreamType, setDataSource, setLooping, setOnErrorListener
 
    if (player != null && ringerMode == AudioManager.RINGER_MODE_NORMAL ) {
      audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
      audioManager.setMode(AudioManager.MODE_RINGTONE);
      try { 
        if(!player.isPlaying()) {
          player.prepare();
          player.start();
          Log.d(TAG, "Playing ringtone now...");
        } else { 
          Log.d(TAG, "Ringtone is already playing, declining to restart.");
        } 
      } catch (IllegalStateException e) {
        Log.w(TAG, e);
        player = null;
      } catch (IOException e) {
        Log.w(TAG, e);
        player = null;
      } 
    } else { 
Full Snippet Info
9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods: isPlaying, release, stop
		} 
		return START_STICKY;
	} 
 
	public void onDestroy() { 
		if (mediaPlayer.isPlaying()) {
			mediaPlayer.stop();
		} 
		mediaPlayer.release();
		Log.v("SIMPLESERVICE", "onDestroy");
	} 
 
	public void onCompletion(MediaPlayer _mediaPlayer) {
		stopSelf();
	} 
 
} 
Full Snippet Info
9
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java

Related Android MediaPlayer Questions & Answers:

Other Android MediaPlayer Examples

11:	
12:	            @Override
13:	            public void onCompletion(MediaPlayer mp) {
14:	                // TODO Auto-generated method stub
15:	                mp.release();

Read More

4:		@Override
5:		public void onCompletion(MediaPlayer arg0) {
6:	
7:			// check for repeat is ON or OFF
8:			if(isRepeat){

Read More

21:		private boolean hasFlash;
22:		Parameters params;
23:		MediaPlayer mp;
24:	
25:		@Override
-
144:	
145:	            @Override
146:	            public void onCompletion(MediaPlayer mp) {
147:	                // TODO Auto-generated method stub
148:	                mp.release();

Read More

See Code Examples for Android MediaPlayer Methods: