MediaPlayer Service Android

后端 未结 3 1931
南笙
南笙 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:56

    I went through exactly the same thing! It's a long haul to develop even a really great mp3 player app. The answer is long.

    Here are a few resources that really helped me. Android has a article on this very thing in their developer docs:

    http://developer.android.com/guide/components/services.html

    Pay attention to what it says at the bottom of this long article about bound services and running in the foreground.

    Additionally, managing player state is what caused me the most headaches.

    You'll also want to take a look at threading because spawning that new service will still execute everything on the Main UI Thread, sounds crazy but true. Take a look at the ExecutorService for managing thread pools. I wish I could tell you it was easier.

    Unfortunately most of my formal training from all over the web but with android services comes from a paid site:

    http://www.pluralsight.com/training/Courses/TableOfContents/android-services

    It is a good resource for all programmers I think but has great sections about many aspects of android programming that are only covered briefly at other tutorial sites.

    The resources at Vogella are good also, mentioned above.

    0 讨论(0)
  • 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:

     <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.

    0 讨论(0)
  • 2020-12-29 08:57

    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.

    0 讨论(0)
提交回复
热议问题