如何在应用程序 closed/minimized 时停止 MediaPlayer 声音?
How do I stop MediaPlayer sound while the App is closed/minimized?
在我的秒表应用程序中,开始按钮应该启动声音,暂停按钮应该停止声音。到目前为止,我的程序运行正常。
但在播放声音的过程中,如果我返回或最小化应用程序,声音不会停止。它一直在播放(一直播放,即使设备处于空闲状态)。奇怪的是,当我重新打开应用程序以停止声音时,它永远不会停止。
如何解决?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerValue = (TextView) findViewById(R.id.timerValue);
startButton = (Button) findViewById(R.id.startButton);
mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
mp.start();
mp.setLooping(true);
}
});
pauseButton = (Button) findViewById(R.id.pauseButton);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
if(mp.isPlaying())
{
mp.pause();
}
}
});
resetButton = (Button) findViewById(R.id.reset);
resetButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timerValue.setText("" + 00 + ":"
+ String.format("%02d", 00) + ":"
+ String.format("%03d", 00));
startTime = SystemClock.uptimeMillis();
timeSwapBuff = 0;
}
});
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
您可以使用 Android 生命周期。
我想你可以在 onStop()
和 onDestroy()
上调用 mp.pause();
示例代码:
@Override
protected void onStop() {
super.onPause();
mp.pause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mp.pause();
}
您应该访问同一个 MediaPlayer 对象
成功 public,也许可行
这个解决方案对我有用:创建一个 class 并定义静态 MediaPlayer 对象和静态方法(暂停、停止等)
您也可以覆盖 activity class 中的 onPause 方法并停止 mediaplayer
要停止播放,请直接在 onPause()
函数上调用 mp.pause()
。但是,您的 onStop() 应该看起来更像:
@Override
protected void onStop() {
super.onStop(); // <<-------ENSURE onStop()
mp.stop();
mp.release();
}
如果您的设备中有“菜单”和“主页”按钮,您应该检查您的应用在按下这些按钮后是否也恢复了。
Kf
在我的秒表应用程序中,开始按钮应该启动声音,暂停按钮应该停止声音。到目前为止,我的程序运行正常。
但在播放声音的过程中,如果我返回或最小化应用程序,声音不会停止。它一直在播放(一直播放,即使设备处于空闲状态)。奇怪的是,当我重新打开应用程序以停止声音时,它永远不会停止。 如何解决?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerValue = (TextView) findViewById(R.id.timerValue);
startButton = (Button) findViewById(R.id.startButton);
mp = MediaPlayer.create(getApplicationContext(), R.raw.sound);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
mp.start();
mp.setLooping(true);
}
});
pauseButton = (Button) findViewById(R.id.pauseButton);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
if(mp.isPlaying())
{
mp.pause();
}
}
});
resetButton = (Button) findViewById(R.id.reset);
resetButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timerValue.setText("" + 00 + ":"
+ String.format("%02d", 00) + ":"
+ String.format("%03d", 00));
startTime = SystemClock.uptimeMillis();
timeSwapBuff = 0;
}
});
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
int milliseconds = (int) (updatedTime % 1000);
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs) + ":"
+ String.format("%03d", milliseconds));
customHandler.postDelayed(this, 0);
}
};
您可以使用 Android 生命周期。
我想你可以在 onStop()
和 onDestroy()
mp.pause();
示例代码:
@Override
protected void onStop() {
super.onPause();
mp.pause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mp.pause();
}
您应该访问同一个 MediaPlayer 对象
成功 public,也许可行
这个解决方案对我有用:创建一个 class 并定义静态 MediaPlayer 对象和静态方法(暂停、停止等)
您也可以覆盖 activity class 中的 onPause 方法并停止 mediaplayer
要停止播放,请直接在 onPause()
函数上调用 mp.pause()
。但是,您的 onStop() 应该看起来更像:
@Override
protected void onStop() {
super.onStop(); // <<-------ENSURE onStop()
mp.stop();
mp.release();
}
如果您的设备中有“菜单”和“主页”按钮,您应该检查您的应用在按下这些按钮后是否也恢复了。
Kf