如何防止 SoundPool 截断声音

发布于 2024-11-30 20:19:14 字数 1389 浏览 2 评论 0原文

我有一个音板应用程序,我使用 SoundPool 来控制声音的播放。一切都很顺利,直到我尝试添加大约 30 秒长的声音。当我单击该声音的按钮时,它只播放几秒钟然后停止。有没有办法使用 SoundPool 来防止这种情况发生?这是我的代码。谢谢!

public class SoundManager {

private  SoundPool mSoundPool; 
private  HashMap<Integer, Integer> mSoundPoolMap; 
private  AudioManager  mAudioManager;
private  Context mContext;


public SoundManager()
{

}

public void initSounds(Context theContext) { 
     mContext = theContext;
     mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); 
     mSoundPoolMap = new HashMap<Integer, Integer>(); 
     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);         
} 

public void addSound(int Index,int SoundID)
{
    mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));

}

public void playSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}

public void playLoopedSound(int index)
{
    float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}
}

I have a soundboard app and I use SoundPool to control the playback of my sounds. Everything worked great until I tried to add a sound that was about 30 seconds long. When I click the button for that sound it only plays for a few seconds then stops. Is there a way to prevent this from happening using SoundPool? Here is my code. Thanks!

public class SoundManager {

private  SoundPool mSoundPool; 
private  HashMap<Integer, Integer> mSoundPoolMap; 
private  AudioManager  mAudioManager;
private  Context mContext;


public SoundManager()
{

}

public void initSounds(Context theContext) { 
     mContext = theContext;
     mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); 
     mSoundPoolMap = new HashMap<Integer, Integer>(); 
     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);         
} 

public void addSound(int Index,int SoundID)
{
    mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));

}

public void playSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}

public void playLoopedSound(int index)
{
    float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柏拉图鍀咏恒 2024-12-07 20:19:14

我在开发音乐应用程序时收集了一些 Soundpool 信息。以下是一些基于声音长度限制的原始文档中未涵盖的指南:

   Length of Sound              Khz
--------------------      ---------------
     1-5 Secs.             44khz - Stereo
    6-11 Secs.             22khz - Stereo
   12-21 Secs.             11khz - Mono
     21+ Secs.              8khz - Mono 

因此,如果您有任何被 SoundPool 截断的音频文件,请使用它进行转换。当然,当转换为 11khz 或更低时,音频听起来质量有点低,但总比没有好。我用来转换的程序是:Free MP3 WMA OGG Converter。在处理 Soundpool 时我总是使用 ogg 文件。希望这对大家有帮助。

There are some Soundpool information I pick up while developing my music app. Here are some guide lines that not covered in the original documentation base on limitations with length of sound:

   Length of Sound              Khz
--------------------      ---------------
     1-5 Secs.             44khz - Stereo
    6-11 Secs.             22khz - Stereo
   12-21 Secs.             11khz - Mono
     21+ Secs.              8khz - Mono 

So if you have any audio files that are getting cut off by SoundPool, convert using this. Of course when converting to 11khz or below, the audio start to sound a bit low quality, but better than nothing. The program I use to convert is: Free MP3 WMA OGG Converter. I always use ogg files when dealing with Soundpool. Hope this help folks.

不奢求什么 2024-12-07 20:19:14

SoundPool 的内部内存限制为 1(一)Mb。
如果您的声音质量非常高,您可能会遇到这个问题。
如果您有很多声音并且达到了此限制,只需创建更多声音池,并将声音分布在它们之间即可。

SoundPool has an internal memory limitation of 1 (one) Mb.
You might be hitting this if your sound is very high quality.
If you have many sounds and are hitting this limit, just create more soundpools, and distribute your sounds across them.

怪我太投入 2024-12-07 20:19:14

SoundPool 是一个轻量级播放器,它只会解码前 1Mb 数据,但如果您想使用更高质量或更长的音频文件,您需要:

* Use MediaPlayer interface instead of SoundPool. MediaPlayer is more complicated mechanism so the playback startup time longer than SoundPool.
* Use MediaCodec API to decode data firstly then call AudioTrack API to playback PCM data as requested.This solution will decode the data firstly then do PCM playback directly, so the delay will be the same as SoundPool but it's more complicated for programming.

SoundPool is a lightweight player which will only decode the first 1Mb data, but if you want to use a more high quality or long audio file, you need to:

* Use MediaPlayer interface instead of SoundPool. MediaPlayer is more complicated mechanism so the playback startup time longer than SoundPool.
* Use MediaCodec API to decode data firstly then call AudioTrack API to playback PCM data as requested.This solution will decode the data firstly then do PCM playback directly, so the delay will be the same as SoundPool but it's more complicated for programming.
我要还你自由 2024-12-07 20:19:14

它仅用于播放短声音,请阅读 SDK 页面。

it is only meant for playing short sounds, read the SDK page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文