安卓背景音乐服务 [英] Android background music service
问题描述
我正在 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 ?
谢谢.
推荐答案
Do it without service
如果您非常认真地通过服务使用媒体播放器
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() {
}
}
请在 Manifest 中调用此服务确保 .BackgroundSoundService 字符串末尾没有空格
Please call this service in Manifest Make sure there is no space at the end of the .BackgroundSoundService string
<service android:enabled="true" android:name=".BackgroundSoundService" />
这篇关于安卓背景音乐服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!