首页 > 解决方案 > Playing two different streams (files) simultaneously with proper sync on Android

问题描述

I'm using two mediaPlayer instances to send two different streams to each channel. Half the time, it works fine but sometimes there is lag between left and right channel which is clearly audible. Is there any alternative except soundpool in Android to play multiple audio files simultaneously with sync ? soundpool is not suitable for my application since audiofiles are large (approx 20 MiB each). Audiofile format in question is : FLAC.

标签: androidandroid-mediaplayersoundpool

解决方案


我发现 Android API 没有提供适当的内置混合功能。我最终使用 wav 文件而不是 FLAC 并根据需要动态混合它们。这是我如何实现它的更高级别的描述。

Read both wav files and saving data part in byte array (Don't forget to strip out header bytes)

Mix them byte by byte to generate a unified wav file

在我的用例中,我只需要混合左右声道,但可以根据需要进行各种转换。

Create a temporary file to hold mixed data

Play the temporary file with mediaPlayer

也可以在不将结果字节数组存储到临时文件的情况下进行播放,但由于内置的​​ seekTo 功能,audioTrack我选择使用。mediaPlayer

希望,这种方法是有帮助的。


推荐阅读