android - 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.
解决方案
我发现 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
希望,这种方法是有帮助的。
推荐阅读
- java - 将字符串转换为 JAVA 中的可调用方法
- c++ - 是否有一个函数可以将我创建的类中的对象显示到 QTextBrowser 中?
- selenium - 如何通过 winappdriver 启动我的应用程序?
- bash - 文件中缺少最后一行
- google-app-engine - GAE:如何从@classmethod 中访问属性值
- javascript - 在 Node.js 中使用 Multer 的语法问题
- reactjs - 如何修复 d3.forceLink 上的“错误:缺失:0”
- javascript - React hooks 警告我缺少依赖项
- php - 对 Instagram 内容的 file_get_contents() 调用响应时间很长(超过 10 秒)
- unity3d - 光线投射点不在平面图层上(奇怪的位置)?