如何正确使用线程/运行循环并播放声音的Soundpool? [英] How to properly use a thread/run loop and play sounds with SoundPool?

查看:405
本文介绍了如何正确使用线程/运行循环并播放声音的Soundpool?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建,在选定的速度(BPM)的音乐播放顺序的应用程序。我目前使用一个线程来触发一个函数每一个节拍,然后使用的Soundpool播放声音。然而,更多的声音,一次慢节奏变得玩!

I'm building an app that plays musical sequences at a chosen tempo (bpm). I'm currently using a thread to fire a function every beat which then plays sounds using SoundPool. However, the more sounds that play at once the slower the tempo becomes!

这是因为它需要播放声音的时间被添加到playingThread睡觉的时候?

Is this because the time it takes to play a sound is added onto the time the playingThread sleeps?

应该soundPool.play是在后台线程而非UI线程?

Should soundPool.play be in a background thread rather than the UI thread?

我也越来越不同步的问题在那里随意加快和减慢,如果两个声音都同时发挥他们不完全相同的时间总是玩。这不是那么糟糕了Android 2.3.4,但出于某种原因,与Android 4.2.2非常糟糕的。

I'm also getting sync issues where it speeds up randomly and slows down and if two sounds are played at the same time they don't always play at exactly the same time. It wasn't so bad with Android 2.3.4 but for some reason really bad with Android 4.2.2.

什么是实现一致的节奏和同步的最佳方式?

What's the best way to achieve a consistent tempo and sync?

下面是我的主题:

public void playingThread()
{
    //Time interval in ms for thread sleep
    final long intervalInMs = (long) ((1 / (float) theBpm) * 60 * 250);

    Thread thread = new Thread() {
        @Override
        public void run() {
            while(playing == true) {
                beat();
                try {
                    Thread.sleep((long)(intervalInMs));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };
    thread.start();
}

然后调用这个函数,里面有很多额外的逻辑来计算这听起来播放和停止等:

Then it calls this function, which has a lot of additional logic to calculate which sounds to play and stop etc:

public void beat()
{
    currentBeat++;

    //Play click sound every four beats
    if(currentBeat % 4 == 0) {
        soundPool.play(clickSoundId, 0.5f, 0.5f, 1, 0, 1f);
    }

    //ADDITIONAL LOGIC TO DETERMINE WHICH SOUNDS TO PLAY...

    int streamId = soundPool.play(soundIds[i], 1f, 1f, 1, 0, 1f);
}

感谢

推荐答案

音乐永远是时间关键。的Soundpool不用于precision由音乐所要求的水平。的Soundpool被设计为反应的输入,当触发发生时,有时往往例如产生声音一个游戏。当产生音乐的时候,你知道声音将要发生时,这样可以更好地安排他们什么。

Music is always time-critical. SoundPool isn't designed for the level of precision demanded by music. SoundPool is designed to be reactive to input, generating sounds when triggers occur, sometimes often e.g. a game. When generating music often you know what sounds are going to occur when so you can schedule them better.

我建议使用AudioTrack并直接写入到音频缓冲区。这是有当声音有关播放给对方完全控制的唯一途径。你仍然在Android操作系统硬件,当谈到对延迟实施的摆布,但至少每个节拍将稳中!

I'd suggest using AudioTrack and writing directly to the audio buffers. That's the only way to have full control over when the sounds are played in relation to each other. You're still at the mercy of the Android OS and the hardware implementation when it comes to latency, but at least each beat will be steady!

这篇关于如何正确使用线程/运行循环并播放声音的Soundpool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆