【问题标题】:Stopping Background Service Music停止后台服务音乐
【发布时间】:2011-10-01 14:15:04
【问题描述】:
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 键退出应用程序时...音乐停止或至少暂停...如何我要这样做吗?

谢谢

瓦希德

【问题讨论】:

    标签: android background playback


    【解决方案1】:

    您需要在onStartCommand() 中启动单独的服务(Manifest 中针对同一服务的不同意图过滤器),您将检查意图的操作,即该操作是否与您为意图操作指定的值相同在清单文件中,如果操作与意图过滤器操作名称匹配,则停止服务。

    来自我的一个项目的示例:

    在清单文件中:

    <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() 中: 在这里我们可以看到需要指定动作名称,用于区分同一个服务中的多个动作。

    @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)
    
    }
    

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

    现在回到活动。捕捉Home button 不是一个好主意。但是您可以在onDestroy() 方法中执行此操作,其中活动不在堆栈中,即在它被销毁之前。在这里,您只需启动将指示服务停止工作的意图。

    例子:

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

    阅读更多关于stopSelf()的Android Dev 如果您正在尝试这种方法来启动 MediaPlayer,那么好的做法是让播放在意图过滤器中有自己的动作名称,并在 onStartCommand()

    中进行相同的检查

    【讨论】:

    • 嘿,我不明白如何添加它...嗯,我可以编辑任何示例代码吗?
    • 哈哈,我的意思是我可以打开任何 .rar 示例代码,看看 java 类和 xml 清单是如何设置的?
    • 嗨 Nikola Despotoski 你能解决我的问题吗stackoverflow.com/questions/14544848/…
    猜你喜欢
    • 1970-01-01
    • 2014-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多