Android背景音乐服务

时间:2011-11-21 09:45:18

标签: android android-service background-music android-music-player

我正在开发Android的娱乐应用程序。我想播放背景音乐,我想为此服务。应用程序有3个活动,必须在所有活动中播放音乐。此外,当活动暂停时,音乐必须暂停并在销毁时停止。谁能告诉我怎么做?任何链接或示例?

谢谢。

7 个答案:

答案 0 :(得分:53)

没有服务

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

如果您对使用服务使用媒体播放器

非常认真
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() {

    }
}

请在Manifest中调用服务 确保.BackgroundSoundService字符串

末尾没有空格
<service android:enabled="true" android:name=".BackgroundSoundService" />

答案 1 :(得分:9)

对于派对来说太晚了,但我仍然会加上我的0.02美元,谷歌已经发布了一个名为通用音乐播放器的免费样品,您可以学习在所有Android平台(汽车,手表,手机,电视等)上播放音乐。 )它使用服务在后台播放音乐,检查出来非常有帮助。这是项目的链接
https://github.com/googlesamples/android-UniversalMusicPlayer

答案 2 :(得分:8)

@ Synxmax在使用ServiceMediaPlayer类时的答案是正确的,但是您还需要在Manifest中声明Service以使其正常工作,例如这样:

<service
    android:enabled="true"
    android:name="com.package.name.BackgroundSoundService" />

答案 3 :(得分:4)

Theres是关于这个主题的HelloAndroid关于这个主题的优秀教程。事实上,这是我在谷歌上的第一次打击。在问这里之前你应该尝试谷歌搜索,因为这是一种很好的做法。

答案 4 :(得分:4)

我有运行它的问题,我做了一些更改,以使用mp3源运行它。这是BackfrounSoundService.java文件。考虑我的mp3文件在我的手机中的SD卡中。

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();
        Log.d("service", "onCreate");
        player = new MediaPlayer();
        try {
            player.setDataSource(Environment.getExternalStorageDirectory().getAbsolutePath() + "/your file.mp3");
        } catch (IOException e) {
            e.printStackTrace();
        }
        player.setLooping(true); // Set looping
        player.setVolume(100, 100);

    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("service", "onStartCommand");
        try {
            player.prepare();
            player.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
        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() {

    }
}

答案 5 :(得分:2)

使用START_STICKY标志创建前台服务。

@Override
public int onStartCommand(Intent startIntent, int flags, int startId) {
   if (startIntent != null) {
       String action = startIntent.getAction();
       String command = startIntent.getStringExtra(CMD_NAME);
       if (ACTION_CMD.equals(action)) {
           if (CMD_PAUSE.equals(command)) {
               if (mPlayback != null && mPlayback.isPlaying()) {
                   handlePauseRequest();
               }
           } else if (CMD_PLAY.equals(command)) {
               ArrayList<Track> queue = new ArrayList<>();
               for (Parcelable input : startIntent.getParcelableArrayListExtra(ARG_QUEUE)) {
                   queue.add((Track) Parcels.unwrap(input));
               }
               int index = startIntent.getIntExtra(ARG_INDEX, 0);
               playWithQueue(queue, index);
           }
       }
   }

   return START_STICKY;
}

然后可以从任何活动中调用它来播放某些音乐

Intent intent = new Intent(MusicService.ACTION_CMD, fileUrlToPlay, activity, MusicService::class.java)
intent.putParcelableArrayListExtra(MusicService.ARG_QUEUE, tracks)
intent.putExtra(MusicService.ARG_INDEX, position)
intent.putExtra(MusicService.CMD_NAME, MusicService.CMD_PLAY)
activity.startService(intent)

您可以使用bindService绑定到服务,并使服务从相应的活动生命周期方法暂停/停止。

这是关于Playing music in the background on Android

的好教程

答案 6 :(得分:0)

我发现有两个很棒的资源可以共享,如果其他任何人通过Google遇到了这个话题,这可能对他们有帮助(2018)。 其中一个是本视频教程,您将在其中实际了解服务的工作方式,这对初学者来说非常有用。

链接:-https://www.youtube.com/watch?v=p2ffzsCqrs8

其他是该网站,它将真正帮助您使用背景音频播放器。

链接:-https://www.dev2qa.com/android-play-audio-file-in-background-service-example/

祝你好运:)

相关问题