Soundpool 示例尚未准备好

发布于 2024-10-20 07:19:54 字数 370 浏览 2 评论 0原文

我有一个 .wav 文件,我想在游戏中使用它,目前我正在游戏中每个活动的 onCreate() 中加载声音。

 soundCount = soundpool.load(this,R.raw.count, 1);

活动开始后就会播放声音。

  soundpool.play(soundCount, 0.9f, 0.9f, 1, -1, 1f);

问题是有时我会遇到错误“sample x not ready”。 是否可以在启动游戏时加载 .wav 文件一次并将其保留在内存中并在以后的游戏中使用它?或者是否可以等待 1-2 秒声音加载完成?

I have a .wav file that I'd like to use across my game, currently I am loading the sound in onCreate() of each activity in the game.

 soundCount = soundpool.load(this,R.raw.count, 1);

The sound will be played once the activity starts.

  soundpool.play(soundCount, 0.9f, 0.9f, 1, -1, 1f);

Problem is at times I will hit the error "sample x not ready".
Is it possible to load the .wav file once upon starting the game and keep it in memory and use it later across the game? Or is it possible to wait for 1-2 seconds for the sound to load finish?

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

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

发布评论

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

评论(8

雄赳赳气昂昂 2024-10-27 07:19:54

您需要通过 SoundPool.setOnLoadCompleteListener 添加监听器来等待它完成。

You'll need to wait for it to finish by adding a listener via SoundPool.setOnLoadCompleteListener.

谁人与我共长歌 2024-10-27 07:19:54

由于我的项目与Android 1.5兼容并且我无法使用setOnLoadCompleteListener,所以我决定延迟声音的播放。
我的源代码如下:

    playSound_Delayed(soundId, 100);

// (..)

private void playSound_Delayed (final int soundId, final long millisec) {
    // TIMER
    final Handler mHandler = new Handler();
    final Runnable mDelayedTimeTask = new Runnable() {
    int counter = 0;
    public void run() {
        counter++;

        if (counter == 1) {
            boolean ret = mHandler.postDelayed(this, millisec); 
            if (ret==false) Log.w("playSound_Delayed", "mHandler.postAtTime FAILED!");
       } else {
           playSound(soundId);
       }
    }
    };
    mDelayedTimeTask.run();
}

Since my project is compatible with Android 1.5 and I couldn't use setOnLoadCompleteListener, I resolved making the play of sound delayed.
My source code follows:

    playSound_Delayed(soundId, 100);

// (..)

private void playSound_Delayed (final int soundId, final long millisec) {
    // TIMER
    final Handler mHandler = new Handler();
    final Runnable mDelayedTimeTask = new Runnable() {
    int counter = 0;
    public void run() {
        counter++;

        if (counter == 1) {
            boolean ret = mHandler.postDelayed(this, millisec); 
            if (ret==false) Log.w("playSound_Delayed", "mHandler.postAtTime FAILED!");
       } else {
           playSound(soundId);
       }
    }
    };
    mDelayedTimeTask.run();
}
花落人断肠 2024-10-27 07:19:54

如果可能的话,您应该使用 setOnLoadCompleteListener ...如果没有,请在对“play”的调用周围包裹一个 while 循环。例如:

int waitLimit = 1000;
int waitCounter = 0;
int throttle = 10;
while(soundPool.play(soundId, 1.f, 1.f, 1, 0, 1.f) == 0 && waitCounter < waitLimit)
 {waitCounter++; SystemClock.sleep(throttle);}

这将以 10 毫秒的间隔重试“播放”1000 次。当然,这应该在非 ui 线程上运行,但仍然不理想。但也许比等待任意时间并期望池准备好要强一点。

you should use setOnLoadCompleteListener if possible ... if not, wrap a while loop around your call to 'play'. something like:

int waitLimit = 1000;
int waitCounter = 0;
int throttle = 10;
while(soundPool.play(soundId, 1.f, 1.f, 1, 0, 1.f) == 0 && waitCounter < waitLimit)
 {waitCounter++; SystemClock.sleep(throttle);}

this will retry 'play' 1000 times on a 10ms interval. this should be run on a non-ui thread of course, and is still not ideal. but maybe a little stronger than waiting an arbitrary time and expecting the pool to be ready.

柠檬色的秋千 2024-10-27 07:19:54

SoundPool 库使用 MediaPlayer 服务将音频解码为原始 16 位 PCM 单声道或立体声流,我们可以简单地将其称为流的准备,这需要一些时间,具体取决于声音文件的大小。如果我们尝试在该过程结束之前播放声音,我们会收到“样本 x 未准备好”错误。

对于这个问题有两种解决方案:

  1. setOnLoadCompleteListener 或
  2. 等待任意时间以便准备结束

然后播放
请注意,这种情况没有例外,或者应用程序在没有任何 try-catch 的情况下不会崩溃

The SoundPool library uses the MediaPlayer service to decode the audio into a raw 16-bit PCM mono or stereo stream which we simply can call preparation of Stream, that takes some times depending on size of sound file. And if we try to play the sound before that process is over, we get "sample x not ready" error.

There are two solutions for this

  1. implement setOnLoadCompleteListener or
  2. wait arbitrary amount of time so that preparation is over

and then play it
Note that there is no exception for this condition or application is not crashing without any try-catch

酒绊 2024-10-27 07:19:54
private SoundPool soundPool;
private int my_sound;
boolean loaded = false;

// 在构造函数中

soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
my_sound = soundPool.load(this, R.raw.blast_sound, 1);

soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
    public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {
       loaded = true;
    }
});

// 然后在你想要播放声音的地方输入

if (loaded) {
soundPool.play(my_sound,  0.9f, 0.9f, 1, 0, 1f);
}
private SoundPool soundPool;
private int my_sound;
boolean loaded = false;

// In the constructor

soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
my_sound = soundPool.load(this, R.raw.blast_sound, 1);

soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
    public void onLoadComplete(SoundPool soundPool, int sampleId,int status) {
       loaded = true;
    }
});

// then where ever you want to play the sound, type

if (loaded) {
soundPool.play(my_sound,  0.9f, 0.9f, 1, 0, 1f);
}
鸵鸟症 2024-10-27 07:19:54

我用简单的 do-while cicle 解决了问题。如果成功,方法 play() 返回非零的streamID,如果失败则返回零。因此,检查返回值就足够了。

int streamID = -1;
do {
    streamID = soundPool.play(soundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
} while(streamID==0);

I solved with problem with simple do-while cicle. The method play() return non-zero streamID if successful, zero if failed. So, it's sufficient to check the return value.

int streamID = -1;
do {
    streamID = soundPool.play(soundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
} while(streamID==0);
分开我的手 2024-10-27 07:19:54

这绝对适合你!

public void playWavFile() {

    Thread streamThread = new Thread(new Runnable() {           

        @Override
        public void run() {                 
            SoundPool soundPool;
            final int wav;
            String path = "/mnt/sdcard/AudioRecorder/record.wav";
            soundPool = new SoundPool(5,AudioManager.STREAM_MUSIC, 0);
            wav = soundPool.load(path, 1);
            soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                    // TODO Auto-generated method stub
                    soundPool.play(wav,100, 100, 0, 0, 1f);
                }
            });

        }        
    });

    streamThread.start();
}   

This would work for you definitely !

public void playWavFile() {

    Thread streamThread = new Thread(new Runnable() {           

        @Override
        public void run() {                 
            SoundPool soundPool;
            final int wav;
            String path = "/mnt/sdcard/AudioRecorder/record.wav";
            soundPool = new SoundPool(5,AudioManager.STREAM_MUSIC, 0);
            wav = soundPool.load(path, 1);
            soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                @Override
                public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                    // TODO Auto-generated method stub
                    soundPool.play(wav,100, 100, 0, 0, 1f);
                }
            });

        }        
    });

    streamThread.start();
}   
分開簡單 2024-10-27 07:19:54

这听起来很疯狂,但不要立即播放声音。给您的应用几秒钟的时间来初始化 SoundPool。在我的应用程序中,这正是问题所在;我添加了一个大约 3 秒的假“加载”屏幕,然后一切正常。 (我什至不需要预加载声音。)

This sounds crazy, but don't play sounds right away. Give your app a couple of seconds to initialize the SoundPool. In my app, this was exactly the issue; I added a fake "loading" screen of ~3 seconds, and then everything worked. (I didn't even need to preload the sounds.)

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