Android的背景音乐服务背景音乐、Android

2023-09-11 20:09:56 作者:穿上校服誰敢說我不乖

我在开发Android的娱乐应用程序。我想播放背景音乐,而我想用服务的。应用程序有3个活动和音乐必须在所有的活动中播放。此外,当活动被暂停,音乐必须暂停并销毁时,停了下来。谁能告诉我如何做到这一点?任何链接或例子?

I am developing an entertainment app in android. I want to play background music, and I want to use service for that. App have 3 activities and music must be played across all activities. Also, when activity is paused, music must PAUSE and stopped when destroyed. Can anyone tell me how to do this ? any links or examples ?

感谢您。

推荐答案

做没有服务

http://www.rbgrn.net/content/307-light-racer-20-days-61-64-completion

如果你正如此严重的与服务做使用媒体播放器

If you are so serious about doing it with services using mediaplayer

Intent svc=new Intent(this, BackgroundSoundService.class);
startService(svc);

public class BackgroundSoundService extends Service {
    private static final String TAG = null;
    MediaPlayer player;
    public IBinder onBind(Intent arg0) {

        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        player = MediaPlayer.create(this, R.raw.idil);
        player.setLooping(true); // Set looping
        player.setVolume(100,100);

    }
    public int onStartCommand(Intent intent, int flags, int startId) {
        player.start();
        return 1;
    }

    public void onStart(Intent intent, int startId) {
        // TO DO
    }
    public IBinder onUnBind(Intent arg0) {
        // TO DO Auto-generated method
        return null;
    }

    public void onStop() {

    }
    public void onPause() {

    }
    @Override
    public void onDestroy() {
        player.stop();
        player.release();
    }

    @Override
    public void onLowMemory() {

    }
}