【问题标题】:How to pause background music when app is running in background应用程序在后台运行时如何暂停背景音乐
【发布时间】:2017-07-04 14:36:46
【问题描述】:

我创建了一个游戏。我在应用程序中添加了一些背景音乐,现在我想在使用主页按钮退出应用程序时暂停音乐,并且在从多任务处理中打开歌曲时应该继续播放。

package tssll8.coloursmash;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
Import android.os.IBinder;

/**
 * Created by ciddarth on 04/07/17.
 */

public class bgmusic 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.bg);
    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() {

}
}

【问题讨论】:

    标签: android service android-mediaplayer


    【解决方案1】:

    Mediaplayer 有一个 pause() 方法:MediaPlayer#pause()

    所以你可以在按下主页按钮触发的暂停中调用它,就像这样

    public void onPause() {
        player.pause();
    }
    

    并像这样在 onResume 中恢复它:

    public void onResume() {
        player.play();
    }
    

    【讨论】:

      猜你喜欢
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多