MediaPlayer Service Android

后端 未结 3 1956
南笙
南笙 2020-12-29 08:04

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 an

3条回答
  •  灰色年华
    2020-12-29 08:57

    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:

     
                
                
     
    

    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.

提交回复
热议问题