停止背景服务音乐
package com.falling.inairproandmark;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
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.bgmusic);
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TODO
}
public IBinder onUnBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onStop() {
}
public void onPause() {
}
@Override
public void onDestroy() {
player.stop();
player.release();
}
@Override
public void onLowMemory() {
}
}
我不太懂编码... 但我想做的是在整个活动中播放音乐。
假设我有 10 个活动...我希望在浏览这些活动时以及当我接到电话或按 Home 键退出应用程序时播放音乐...音乐停止或至少暂停...我该怎么办那?
谢谢
瓦希德
package com.falling.inairproandmark;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
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.bgmusic);
player.setVolume(100,100);
}
public int onStartCommand(Intent intent, int flags, int startId) {
player.start();
return 1;
}
public void onStart(Intent intent, int startId) {
// TODO
}
public IBinder onUnBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onStop() {
}
public void onPause() {
}
@Override
public void onDestroy() {
player.stop();
player.release();
}
@Override
public void onLowMemory() {
}
}
I don't understand coding much...
But what I'm trying to do is play the music thru-out the activities.
Let's say I have 10 activities... I want the music to play while i go through them and when I get a call or exit the application by pressing Home key... Music stops or at least pauses... how do i do that?
Thanks
Wahid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在
onStartCommand()
中启动单独的服务(清单中针对同一服务的不同意图过滤器),您将检查意图的操作,即该操作是否与您的操作具有相同的值为清单文件中的意图操作指定,如果该操作与意图过滤器操作名称匹配,则停止服务。我的项目之一的示例:
在清单文件中:
在 onStartCommand() 中:
这里我们可以看到需要指定操作名称,用于区分同一服务中的多个操作。
笔记:
请注意,在这里您可以简单地使用
.stop()
或.pause()
停止 MediaPlayer 播放或暂停它,或者按照我上面提供的那样终止服务。现在回到活动。抓住
Home 按钮
并不是一个好主意。但是您可以在 onDestroy() 方法中执行此操作,其中活动不在堆栈中,即在其被销毁之前。在这里,您只需启动意图,该意图将指示服务停止工作。示例:
阅读有关 Android Dev 的更多内容
stopSelf()
如果您正在尝试这种启动 MediaPlayer 的方法,那么好的做法是让播放在意图过滤器中具有自己的操作名称,并在
onStartCommand()
中执行相同的检查You need to launch separate service (different intent filter in the Manifest for the same serivce) in
onStartCommand()
you'll check the action of the intent i.e if the action has the same value as the one you specified for intent action in the manifest file and if the action matches the intent filter action name then just stop the service.Example from one of my projects:
In the manifest file:
In the onStartCommand():
Here we can see the need of specifying action name, which is used to distinguish numerous actions within the same service.
Note:
Note that here you can simply stop the MediaPlayer from playing or pause it using
.stop()
or.pause()
,or just terminate the service as I provided above.Now back to the activity. Catching
Home button
is not good idea. But this you can do in theonDestroy()
method, where the activity is not in the stack i.e just before it is destroyed. Here you just launch intent that will signal the service to stop working.Example:
Read more on Android Dev about
stopSelf()
If you are trying this approach starting the MediaPlayer good practice will be to make the playing has its own action name in the intent filter, and do the same checking in the
onStartCommand()