From d754d442751b5e14e27b3c2ca8157e12903b8c2c Mon Sep 17 00:00:00 2001 From: xufuji456 <839789740@qq.com> Date: Thu, 29 Jul 2021 09:10:52 +0800 Subject: [PATCH] release AudioPlayer when coming destroy --- app/src/main/cpp/audio_player.cpp | 10 +++++++++- app/src/main/cpp/video_filter.c | 4 +++- app/src/main/java/com/frank/ffmpeg/AudioPlayer.java | 2 ++ .../com/frank/ffmpeg/activity/EqualizerActivity.kt | 9 +++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/app/src/main/cpp/audio_player.cpp b/app/src/main/cpp/audio_player.cpp index 41a5394..d85ac40 100644 --- a/app/src/main/cpp/audio_player.cpp +++ b/app/src/main/cpp/audio_player.cpp @@ -29,6 +29,7 @@ extern "C" { #define MAX_AUDIO_FRAME_SIZE 48000 * 4 int filter_again = 0; +int filter_release = 0; const char *filter_desc = "superequalizer=6b=4:8b=5:10b=5"; int init_volume_filter(AVFilterGraph **graph, AVFilterContext **src, AVFilterContext **sink, @@ -280,7 +281,7 @@ AUDIO_PLAYER_FUNC(void, play, jstring input_jstr, jstring filter_jstr) { } //read audio frame - while (av_read_frame(pFormatCtx, packet) >= 0) { + while (av_read_frame(pFormatCtx, packet) >= 0 && !filter_release) { if (packet->stream_index != audio_stream_idx) { av_packet_unref(packet); continue; @@ -343,10 +344,17 @@ end: avformat_close_input(&pFormatCtx); env->ReleaseStringUTFChars(input_jstr, input_cstr); env->ReleaseStringUTFChars(filter_jstr, filter_desc); + filter_again = 0; + filter_release = 0; + LOGE(TAG, "audio release..."); } AUDIO_PLAYER_FUNC(void, again, jstring filter_jstr) { if (!filter_jstr) return; filter_again = 1; filter_desc = env->GetStringUTFChars(filter_jstr, NULL); +} + +AUDIO_PLAYER_FUNC(void, release) { + filter_release = 1; } \ No newline at end of file diff --git a/app/src/main/cpp/video_filter.c b/app/src/main/cpp/video_filter.c index aa783d1..5070fb9 100644 --- a/app/src/main/cpp/video_filter.c +++ b/app/src/main/cpp/video_filter.c @@ -384,7 +384,9 @@ end: audio_track_write_mid = NULL; ANativeWindow_release(nativeWindow); (*env)->ReleaseStringUTFChars(env, filePath, file_name); - LOGE(TAG, "do release..."); + again = 0; + release = 0; + LOGE(TAG, "video release..."); return ret; } diff --git a/app/src/main/java/com/frank/ffmpeg/AudioPlayer.java b/app/src/main/java/com/frank/ffmpeg/AudioPlayer.java index 229f154..87572ed 100644 --- a/app/src/main/java/com/frank/ffmpeg/AudioPlayer.java +++ b/app/src/main/java/com/frank/ffmpeg/AudioPlayer.java @@ -19,6 +19,8 @@ public class AudioPlayer { public native void again(String filterDesc); + public native void release(); + //using OpenSL ES to play public native void playAudio(String audioPath); diff --git a/app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt b/app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt index 1dd2a33..75b9a87 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt @@ -109,4 +109,13 @@ class EqualizerActivity : BaseActivity(), OnSeeBarListener { override fun onSelectedFile(filePath: String) { audioPath = filePath } + + override fun onDestroy() { + super.onDestroy() + if (filterThread != null) { + mAudioPlayer!!.release() + filterThread?.interrupt() + filterThread = null + } + } }