停止背景服务音乐

发布于 2024-12-07 16:50:08 字数 1114 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不乱于心 2024-12-14 16:50:08

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

我的项目之一的示例:

在清单文件中:

<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 按钮 并不是一个好主意。但是您可以在 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);

阅读有关 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:

<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>

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)

}

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 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.

Example:

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

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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文