屏幕关闭后,Android MediaPlayer继续播放 [英] Android MediaPlayer continues playing after screen is turned off
本文介绍了屏幕关闭后,Android MediaPlayer继续播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试遵循核心应用质量"准则FN-A1,该准则指出屏幕关闭时不应播放音频,因此我对其进行了检查,并且在关闭屏幕后确实可以继续播放,但是我不知道为什么.我不认为我正在使用服务,而且MediaPlayer似乎应该默认停止播放.谁能帮我弄清楚我做错了什么?我已经复制了代码的相关部分.谢谢
I'm trying to follow the Core App Quality guideline FN-A1 that says that audio shouldn't play when the screen is off, so I checked it, and it does continue playing after I turn the screen off, but I'm not sure why. I don't think I'm using a service, and it seems that MediaPlayer should stop playing by default. Can anyone help me figure what I did wrong? I've copied the relevant parts of code. Thanks
public class RecordActivity extends BaseActivity {
MediaPlayer playback;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
listView = (ListView)findViewById(R.id.listView);
dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getString(R.string.app_name));
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
String item = ((TextView) view).getText().toString();
File itemFile = new File(dir + "/" + item + ".wav");
if (itemFile.exists()) {
playback = MediaPlayer.create(getApplicationContext(), Uri.fromFile(itemFile));
try {
playback.start();
} catch (NullPointerException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), R.string.errorFileNull, Toast.LENGTH_SHORT).show();
}
}
else {
Toast.makeText(getApplicationContext(), R.string.errorNotRecorded, Toast.LENGTH_SHORT).show();
}
return true;
}
});
}
}
推荐答案
您应在onPause中停止MediaPlayer:
You should stop the MediaPlayer in onPause:
if (playback != null && playback.isPlaying()) {
playback.pause();
}
在onResume中恢复它:
Resume it in onResume:
if (playback != null && !playback .isPlaying()) {
playback.start();
}
这篇关于屏幕关闭后,Android MediaPlayer继续播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文