不能在Android上停止AudioTrack 5 [英] Cannot stop AudioTrack on Android 5
问题描述
我无法在一台Nexus 4运行的是Android 5.0.1停止播放的 AudioTrack
的。难道我做错了什么?
I cannot stop playback of an AudioTrack
on a Nexus 4 running Android 5.0.1. Am I doing something wrong?
在code是很简单的(它实际上只是一个测试应用程序),它完美的作品上运行2.3.6和4.4设备。以下是有关部分:
The code is very simple (it's actually just a test app) and it works perfectly on devices running 2.3.6 and 4.4. Here's the relevant portion:
mPlugReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(Intent.ACTION_HEADSET_PLUG.equals(intent.getAction())) {
boolean plugged = intent.getIntExtra("state", 0) == 1;
if(plugged) {
log.debug("audio device plugged");
boolean mic = intent.getIntExtra("microphone", 0) == 1;
if(mic) {
log.debug("microphone detected");
mPowerTone.play();
}
}
}
else if(AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
log.debug("stopping power tone");
mPowerTone.pause();
mPowerTone.flush();
mPowerTone.stop();
}
}
};
在5.0.1,它记录的制动力调但是赛道继续玩!它甚至继续发挥我退出程序之后。有时,几秒钟后停止,有时我不得不强制关闭应用程序。
On 5.0.1, it logs "stopping power tone" but the track continues to play! It even continues to play after I exit the app. Sometimes it stops after a few seconds, and sometimes I have to force close the app.
我都试过有和没有调用暂停()
和的flush()
,都无济于事。它的工作原理而对旧设备的电话。
I tried both with and without the calls to pause()
and flush()
, to no avail. It works without those calls on the older devices.
推荐答案
<一个href="http://stackoverflow.com/questions/24479158/audiotrack-doesnt-stop-with-audiotrack-stop-since-last-android-version">This悬而未决的问题带我到一个解决方案。如果你调用 AudioTrack#停止()
的棒棒糖,即使在实际工作相结合的方法,播放也不会停止!您必须使用一个条件是这样的:
This unanswered question led me to a solution. If you call AudioTrack#stop()
on Lollipop, even in conjunction with the methods that actually work, playback will not stop! You must use a condition like this:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mPowerTone.pause();
mPowerTone.flush();
}
else {
mPowerTone.stop();
}
保持良好的工作,谷歌。
Keep up the good work, Google.
这篇关于不能在Android上停止AudioTrack 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!