【发布时间】:2016-08-17 11:32:10
【问题描述】:
我有一个音乐服务,可以在应用程序的后台播放音乐。我希望音乐在应用程序的所有活动中继续播放,但在应用程序在后台运行时停止(即当用户转到另一个应用程序或按下主页按钮而不从正在运行的应用程序中删除应用程序时)
这是我的 MusicService 代码:
public class MusicService extends Service {
public static MediaPlayer player;
public IBinder onBind(Intent arg0) {
return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {
player= MediaPlayer.create(this,R.raw.music1);
player.start();
player.setLooping(true);
return super.onStartCommand(intent,flags,startId);
}
}
这是我清单中与音乐服务相关的部分:
<service android:name=".MusicService" android:stopWithTask="true" />
编辑:如果有人知道如何在没有服务的情况下播放背景音乐,那也没关系,只要在应用打开和按下主页按钮时关闭的整个过程中播放音乐。
【问题讨论】:
标签: android background android-service audio-player android-music-player