Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaPlayer Service Android

I am new to Android. I am creating service for Media Player so that it can continue to play song even if i close the application. I have created activity for Media Player and it is having all the functionality like play , pause , next , previous , seekbar and also includes oncompletionlistener . All works excellent. But Now i want that all should be managed by service.

I have Created MyService Class :

public class MyService extends Service {

    public static MediaPlayer mp;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        mp = new MediaPlayer();     
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {  
        return START_STICKY;
    }

But in my player activity i have created ArrayList for Songlist from which i am taking currentsongIndex and through it i am maintaining all the functionality like next , previous and all.. Now in service how do i get songlist which is also required in my activity ?? Where should i create MediaPlayer object mean in service or activity ??

for MediaPlayer I have reffered http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/ . For my media player code you can refer this site. Thanks. Pleaze clear my doubt. I am so confused. Reply me soon..

like image 222
Zankhna Avatar asked Jan 04 '13 05:01

Zankhna


People also ask

What is a media player used for?

Media players provide most or all of the following features. They allow users to organize their multimedia collection, play songs and movies, rip CD tracks to MP3 and other audio formats, burn CDs, listen to Internet radio, download content from online music stores and stream content from the Internet.

What is Android media player?

MediaPlayer Class in Android is used to play media files. Those are Audio and Video files. It can also be used to play audio or video streams over the network.

How do I turn off Media Player on Android?

The feature is as simple as tapping and holding on the media card of any app then choosing Dismiss.

How can I stop media player in another activity?

You can't destroy one activity from another activity. But you can pause the media player by creating a new instance of MediaPlayer in the second activity and callind the 'stop' method !


2 Answers

You are on the right track. I have adapted from the SDK Samples; this is how I do it and it works great. From your ArrayList (in your activity NOT from the Service) call

onListItemClick

and start an intent that starts the music service:

startService(new Intent(MusicService.ACTION_PLAY));

In your manifest you will need to add:

 <intent-filter>
            <action android:name="com.blah.blah.action.PLAY" />
           <xxx xxx> 
 </intent-filter>

And of course in your Music Service you need to receive the Intent:

public int onStartCommand(Intent intent, int flags, int startId) {
    String action = intent.getAction();
    if (action.equals(ACTION_PLAY))
        processPlayRequest();
  }

Be sure to add Intents for skip, rewind, stop etc. Let me know if this helps.

like image 106
midiwriter Avatar answered Sep 29 '22 00:09

midiwriter


Getting the app to run in background should be taken care of by the 'Service' itself.
Try following this example http://www.vogella.com/articles/AndroidServices/article.html
A service is designed to work in the background.

like image 45
Rahul Bisht Avatar answered Sep 29 '22 00:09

Rahul Bisht