Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Android, control (play/pause) media player from external code

I'm very new to Android programming. As a first hobby project, I'd like to write a program to control the media player app, in particular play/pause (or toggle) or even better, fast forward/backward. Is it possible to do this? If yes, are there any tutorials or sample code?

Thanks very much.

Clarification: maybe I was not clear enough in my original question. I don't want to play audio/video inside my app, but I want to control other media player apps (says the default music app) from my app. For example, my app has only one button, if the default media player app is playing some music (in the background) and I push that button, the music is paused.

like image 895
Truong Avatar asked Apr 01 '11 20:04

Truong


People also ask

How do I pause media player in Android programmatically?

To pause MediaPlayer , call pause() . Stopped State: MediaPlayer is in this state when media stops playing. If MediaPlayer is in this state and you want to play the media again, you have to prepare it again by calling prepare() or prepareAsync() .

Is a method for media player in Android?

If the stop() method is called using Mediaplayer instance, then it needs to prepared for the next playback. The MediaPlayer can be moved to the specific time position using seekTo() method so that the MediaPlayer instance can continue playing the Audio or Video playback from that specified position.

What is media player controls?

controls. A Media Player is a Video Control that is used to play a video in the microapp. The Media player comes with all the controls that are present in any generic media player like play/pause, volume, and fullscreen view.


1 Answers

The Following code is paused default Media Player via sendBroadcast:

   AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);    

    if (mAudioManager.isMusicActive()) {

    Intent i = new Intent("com.android.music.musicservicecommand");

    i.putExtra("command", "pause");
    YourApplicationClass.this.sendBroadcast(i);
    }   
like image 100
Ramesh Akula Avatar answered Oct 24 '22 17:10

Ramesh Akula