简体   繁体   English

在Android上可靠地顺序播放音频

[英]Reliable sequential audio playback on Android

I am writing an android game for teaching kids to count. 我正在编写一个用于教孩子数数的android游戏。 The instructions are read to the player through sound clips that are put together to form sentences (for instance "Place", "one", "cow", "in the", "barn". This requires a certain amount of reliability when it comes to latency so that the flow of the instructions sounds natural. 通过组合成句子的声音片段(例如“ Place”,“ one”,“ cow”,“ in the”,“ barn”)将指令读取给播放器,这要求一定的可靠性。说到等待时间,使指令的流程听起来自然。

Currently I am using MediaPlayer, playing each sound in a OnCompletionListener. 当前,我正在使用MediaPlayer,在OnCompletionListener中播放每种声音。 Each sound has it's own MediaPlayer that is created and prepared before playback of any sound starts (to reduce latency) - but still I get a significant delay before each sound the first time it is played (the second time it seems some sort of caching has taken place and it works fine). 每个声音都有它自己的MediaPlayer,它是在开始播放任何声音之前创建和准备的(以减少延迟)-但是在第一次播放每个声音之前,我仍然会遇到很大的延迟(第二次似乎是某种缓存了)发生,并且工作正常)。

The sounds are not many and very short and it should probably work better with SoundPool, but SoundPool has no way of knowing when an audio is complete and thus not an option. 声音不是很多而且很短,它可能与SoundPool一起使用会更好,但是SoundPool无法知道音频何时完成,因此无法选择。

Does anyone have any experience with similar problems and a viable solution? 有没有人有类似的问题,一个可行的解决方案的经验吗?

I have used handler with OnCompletionListener and it worked fine for me to give delay between two sounds. 我在OnCompletionListener使用了handler ,它对我来说可以很好地在两种声音之间提供延迟。

this way, 这条路,

CommonMethod.player.setOnCompletionListener(new OnCompletionListener() {

                            @Override
                            public void onCompletion(MediaPlayer mp) {
                                // /will use count and loop as per number of
                                // repetition chosen.
                                handler.postDelayed(new Runnable() {

                                    public void run() {
                                        if (counter >= limit) {
                                            CommonMethod.player.stop();
                                        } else {
                                            CommonMethod.player.start();
                                        }
                                        counter++;
                                    }
                                }, 3000);
                            }
                        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM