停止后台音乐服务 [英] Stopping Background Service Music

查看:162
本文介绍了停止后台音乐服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

让我们说我有10项活动...我想要的音乐播放,同时我通过他们去,当我接到一通电话,或通过退出pressing主页键的应用程序...音乐停止或至少暂停。 ..我该怎么做呢?

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?

感谢

瓦希德

推荐答案

您需要(在清单不同的意图过滤器对同一serivce)推出独立的服务在 onStartCommand()你检查,即意向的行动,如果该操作有相同的值,你在manifest文件意图操作中指定的一个,如果动作相匹配的意图筛选器操作名称,则只是停止服务。

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:

在清单文件:

<service android:name=".MyPlayerService" android:permission="android.permission.MODIFY_AUDIO_SETTINGS">        
      <intent-filter >
        .... some other filters
        <action android:name="com.my.player.EXIT"/>
        ....
        </intent-filter>   
      </service>

在onStartCommand():
在这里,我们可以看到指定操作名称,它是用来在同一服务中区分许多行动的需要。

In the onStartCommand(): Here we can see the need of specifying action name, which is used to distinguish numerous actions within the same service.

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.i("ON START COMMAND", "Huston, we have a lift off!");

    if(intent.getAction().equals("com.my.player.EXIT")
             { // means that you have envoked action that will has to stop the service
                 MyPlayerService.this.stopSelf();   // See the note below
             }else if(//some other actions that has to be done on the service)

}

请注意:
请注意,在这里你可以简单地停止播放的MediaPlayer或者使用暂停 .stop() .pause() ,或者只是终止服务就像我上面提供的。

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.

现在回到了活动。醒目主页按钮不是好主意。但是,这可以在的onDestroy()方法,这里的活动不是在堆栈中它被摧毁即之前做的。在这里,你只需启动意图将信号服务停止工作。

Now back to the activity. Catching Home button is not good idea. But this you can do in the onDestroy() 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.

例如:

Intent exit = new Intent("com.my.player.EXIT"); //intent filter has to be specified with the action name for the intent
this.startService(exit);

了解更多关于Android开发人员约 stopSelf()
如果您尝试这种方法启动MediaPlayer的良好做法将会使播放的意图过滤器都有自己的操作名称,并做相同的检查,在 onStartCommand()

这篇关于停止后台音乐服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆