简体   繁体   English

Android AudioTrack在某些设备上极慢

[英]Android AudioTrack extremely slow on some devices

I have a system decoding MP3 audio with MediaExtractor , converting it with MediaCodec and playing it with AudioTrack . 我有一个使用MediaExtractor解码MP3音频,使用MediaCodec转换并使用AudioTrack播放的AudioTrack On most devices it works fine, but on the Samsung Galaxy S3 and Samsung Galaxy Tab, some users report extremely slow audio playback. 在大多数设备上,它都能正常工作,但在Samsung Galaxy S3和Samsung Galaxy Tab上,一些用户报告音频播放速度非常慢。 I can't reproduce it on an emulator or physical devices. 我无法在模拟器或物理设备上复制它。

After much trouble, it turns out that the issue was with my use of MediaFormat . 经过很多麻烦之后,事实证明问题出在我对MediaFormat使用上。 On some devices, the MediaFormat returned by MediaCodec is not the same as the one returned by MediaExtractor . 在某些设备上, MediaCodec返回的MediaFormat与MediaExtractor返回的MediaExtractor Frame rates, etc., may change. 帧频等可能会改变。 Instead of instantiating AudioTrack with the MediaFormat returned by MediaExtractor, it was necessary to wait until decoding began. 无需使用AudioTrack返回的MediaFormat实例化AudioTrack ,必须等到解码开始。 At that point, MediaCodec.dequeueOutputBuffer() will return MediaCodec.INFO_OUTPUT_FORMAT_CHANGED . 届时, MediaCodec.dequeueOutputBuffer()将返回MediaCodec.INFO_OUTPUT_FORMAT_CHANGED MediaCodec.getOutputFormat() can then be used return the correct MediaFormat with which to initialize the AudioTrack. 然后可以使用MediaCodec.getOutputFormat()返回用于初始化AudioTrack的正确MediaFormat。 Hope this helps someone. 希望这对某人有帮助。

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

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