SoundManager,如何停止播放声音
SoundManager, how to stop sound playing
我使用简单的 SoundManager 来播放来自 sounpool 的声音。我的问题是,当我退出应用程序时,声音不会停止播放,即使我使用 mySound.release();
。
否则,一切正常,这是我的代码。
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager {
private Context pContext;
private SoundPool mySound;
private float rate = 1.0f;
private float masterVolume = 1.0f;
private float leftVolume = 1.0f;
private float rightVolume = 1.0f;
private float balance = 0.5f;
// Constructor, setup the audio manager and store the app context
public SoundManager(Context appContext)
{
mySound = new SoundPool(16, AudioManager.STREAM_MUSIC, 100);
pContext = appContext;
}
// Load up a sound and return the id
public int load(int sound_id)
{
return mySound.load(pContext, sound_id, 1);
}
// Play a sound
public void play(int sound_id)
{
mySound.play(sound_id, leftVolume, rightVolume, 1, 0, rate);
}
// Set volume values based on existing balance value
public void setVolume(float vol)
{
masterVolume = vol;
if(balance < 1.0f)
{
leftVolume = masterVolume;
rightVolume = masterVolume * balance;
}
else
{
rightVolume = masterVolume;
leftVolume = masterVolume * ( 2.0f - balance );
}
}
public void unloadAll()
{
mySound.release();
}
}
方法play()
return一个int StreamId
。您需要将此 StreamId 保存在一个变量上并调用停止方法。例如:
声明一个 Stream Id 变量:
private int mStreamId;
然后,在开始新的声音流时保留它:
// Play a sound
public void play(int sound_id)
{
mStreamId = mySound.play(sound_id, leftVolume, rightVolume, 1, 0, rate);
}
所以你可以停止它:
mySound.stop(mStreamId);
现在您可以在活动事件 onStop
、onPause
或您想要的地方停止声音流。
编辑:
释放 SoundPool
资源的最佳方法是调用 release
并将引用设置为空 在停止播放声音后 。例如:
@Override
protected void onPause() {
super.onPause();
mSoundManager.unloadAll();
}
在您的 SoundManager
上,将您的 unloadAll
方法更改为:
public void unloadAll()
{
mySound.release();
mySound = null;
}
Note the following:
onPause – is always called when the activity is about to go into the
background.
release: releases all the resources used by the SoundPool
object null: this SoundPool object can no longer be used (like @DanXPrado
said) so set it to null to help the Garbage Collector identify it as
reclaimable.
有关详细信息,请参阅 this tutorial about SoundPool。
希望这对您有所帮助。
我使用简单的 SoundManager 来播放来自 sounpool 的声音。我的问题是,当我退出应用程序时,声音不会停止播放,即使我使用 mySound.release();
。
否则,一切正常,这是我的代码。
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager {
private Context pContext;
private SoundPool mySound;
private float rate = 1.0f;
private float masterVolume = 1.0f;
private float leftVolume = 1.0f;
private float rightVolume = 1.0f;
private float balance = 0.5f;
// Constructor, setup the audio manager and store the app context
public SoundManager(Context appContext)
{
mySound = new SoundPool(16, AudioManager.STREAM_MUSIC, 100);
pContext = appContext;
}
// Load up a sound and return the id
public int load(int sound_id)
{
return mySound.load(pContext, sound_id, 1);
}
// Play a sound
public void play(int sound_id)
{
mySound.play(sound_id, leftVolume, rightVolume, 1, 0, rate);
}
// Set volume values based on existing balance value
public void setVolume(float vol)
{
masterVolume = vol;
if(balance < 1.0f)
{
leftVolume = masterVolume;
rightVolume = masterVolume * balance;
}
else
{
rightVolume = masterVolume;
leftVolume = masterVolume * ( 2.0f - balance );
}
}
public void unloadAll()
{
mySound.release();
}
}
方法play()
return一个int StreamId
。您需要将此 StreamId 保存在一个变量上并调用停止方法。例如:
声明一个 Stream Id 变量:
private int mStreamId;
然后,在开始新的声音流时保留它:
// Play a sound
public void play(int sound_id)
{
mStreamId = mySound.play(sound_id, leftVolume, rightVolume, 1, 0, rate);
}
所以你可以停止它:
mySound.stop(mStreamId);
现在您可以在活动事件 onStop
、onPause
或您想要的地方停止声音流。
编辑:
释放 SoundPool
资源的最佳方法是调用 release
并将引用设置为空 在停止播放声音后 。例如:
@Override
protected void onPause() {
super.onPause();
mSoundManager.unloadAll();
}
在您的 SoundManager
上,将您的 unloadAll
方法更改为:
public void unloadAll()
{
mySound.release();
mySound = null;
}
Note the following:
onPause – is always called when the activity is about to go into the background.
release: releases all the resources used by the SoundPool
object null: this SoundPool object can no longer be used (like @DanXPrado said) so set it to null to help the Garbage Collector identify it as reclaimable.
有关详细信息,请参阅 this tutorial about SoundPool。
希望这对您有所帮助。