当一起使用AudioTrack和MediaSync时,我遇到了一个严重的内存泄漏问题。在我看来,问题在于AudioTrack不会释放某些本机资源。结果,该应用只能运行几次,此后,由于不再有可用的曲目,因此无法创建AudioTrack。
下面是一个导致内存泄漏的简短示例。整个项目可以下载此 GitHub上。APK文件可在此处下载。
final MediaSync mediaSync = new MediaSync();
mediaSync.setSurface(mSurface);
final Surface inputSurface = mediaSync.createInputSurface(); // There is no the memory leak if I don't create this input surface.
final AudioTrack audioTrack = new AudioTrack.Builder()
.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MOVIE)
.build())
.setAudioFormat(new AudioFormat.Builder()
.setEncoding(AudioFormat.ENCODING_PCM_16BIT)
.setSampleRate(48000)
.setChannelMask(12)
.build())
.build();
mediaSync.setAudioTrack(audioTrack); // There is no the memory leak if I don't set AudioTrack.
mediaSync.release();
inputSurface.release();
audioTrack.release();
Run Code Online (Sandbox Code Playgroud)
我通过以下方式重现该问题:
Logcat:
2019-03-15 09:19:57.313 239-15387/? E/AudioFlinger: no more track names available
2019-03-15 …Run Code Online (Sandbox Code Playgroud)