From 4fccf3f6a6e2983bafee811fdb15a87b013fae91 Mon Sep 17 00:00:00 2001 From: xufuji456 <839789740@qq.com> Date: Fri, 30 Jul 2021 23:26:36 +0800 Subject: [PATCH] release audioTrack when exit playing --- app/src/main/cpp/audio_player.cpp | 2 ++ app/src/main/java/com/frank/ffmpeg/AudioPlayer.java | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/src/main/cpp/audio_player.cpp b/app/src/main/cpp/audio_player.cpp index d85ac40..6807b4b 100644 --- a/app/src/main/cpp/audio_player.cpp +++ b/app/src/main/cpp/audio_player.cpp @@ -344,6 +344,8 @@ end: avformat_close_input(&pFormatCtx); env->ReleaseStringUTFChars(input_jstr, input_cstr); env->ReleaseStringUTFChars(filter_jstr, filter_desc); + jmethodID releaseMethod = env->GetMethodID(player_class, "releaseAudioTrack", "()V"); + env->CallVoidMethod(thiz, releaseMethod); filter_again = 0; filter_release = 0; LOGE(TAG, "audio release..."); diff --git a/app/src/main/java/com/frank/ffmpeg/AudioPlayer.java b/app/src/main/java/com/frank/ffmpeg/AudioPlayer.java index 87572ed..be7fecf 100644 --- a/app/src/main/java/com/frank/ffmpeg/AudioPlayer.java +++ b/app/src/main/java/com/frank/ffmpeg/AudioPlayer.java @@ -14,6 +14,8 @@ public class AudioPlayer { System.loadLibrary("media-handle"); } + private AudioTrack mAudioTrack; + //using AudioTrack to play public native void play(String audioPath, String filterDesc); @@ -46,7 +48,15 @@ public class AudioPlayer { int bufferSizeInBytes = AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat); - return new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, audioFormat, + mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, audioFormat, bufferSizeInBytes, AudioTrack.MODE_STREAM); + return mAudioTrack; + } + + public void releaseAudioTrack() { + if (mAudioTrack != null) { + mAudioTrack.release(); + mAudioTrack = null; + } } }