diff --git a/app/src/main/cpp/audio_player.cpp b/app/src/main/cpp/audio_player.cpp index 7964727..41a5394 100644 --- a/app/src/main/cpp/audio_player.cpp +++ b/app/src/main/cpp/audio_player.cpp @@ -28,7 +28,8 @@ extern "C" { #define SLEEP_TIME 1000 * 16 #define MAX_AUDIO_FRAME_SIZE 48000 * 4 -//const char *filter_descr = "superequalizer=6b=4:8b=5:10b=5"; +int filter_again = 0; +const char *filter_desc = "superequalizer=6b=4:8b=5:10b=5"; int init_volume_filter(AVFilterGraph **graph, AVFilterContext **src, AVFilterContext **sink, uint64_t channel_layout, AVSampleFormat inputFormat, int sample_rate) { @@ -204,7 +205,7 @@ AUDIO_PLAYER_FUNC(void, play, jstring input_jstr, jstring filter_jstr) { const char *input_cstr = env->GetStringUTFChars(input_jstr, NULL); LOGI(TAG, "input url=%s", input_cstr); - const char *filter_desc = env->GetStringUTFChars(filter_jstr, NULL); + filter_desc = env->GetStringUTFChars(filter_jstr, NULL); LOGE(TAG, "filter_desc=%s", filter_desc); av_register_all(); AVFormatContext *pFormatCtx = avformat_alloc_context(); @@ -280,42 +281,53 @@ AUDIO_PLAYER_FUNC(void, play, jstring input_jstr, jstring filter_jstr) { //read audio frame while (av_read_frame(pFormatCtx, packet) >= 0) { - if (packet->stream_index == audio_stream_idx) { - ret = avcodec_decode_audio4(codecCtx, frame, &got_frame, packet); + if (packet->stream_index != audio_stream_idx) { + av_packet_unref(packet); + continue; + } + if (filter_again) { + filter_again = 0; + avfilter_graph_free(&audioFilterGraph); + if (init_equalizer_filter(filter_desc, codecCtx, &audioFilterGraph, &audioSrcContext, &audioSinkContext) < 0) { + LOGE(TAG, "init_filter error, ret=%d\n", ret); + goto end; + } + LOGE(TAG, "play again,filter_descr=_=%s", filter_desc); + } + ret = avcodec_decode_audio4(codecCtx, frame, &got_frame, packet); + if (ret < 0) { + break; + } + if (got_frame > 0) { + ret = av_buffersrc_add_frame(audioSrcContext, frame); if (ret < 0) { - break; + LOGE(TAG, "Error add the frame to the filter graph:%d", ret); } - if (got_frame > 0) { - ret = av_buffersrc_add_frame(audioSrcContext, frame); + /* Get all the filtered output that is available. */ + while (1) { + ret = av_buffersink_get_frame(audioSinkContext, filter_frame); + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + break; if (ret < 0) { - LOGE(TAG, "Error add the frame to the filter graph:%d", ret); + LOGE(TAG, "Error get the frame from the filter graph:%d", ret); + goto end; } - /* Get all the filtered output that is available. */ - while (1) { - ret = av_buffersink_get_frame(audioSinkContext, filter_frame); - if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) - break; - if (ret < 0) { - LOGE(TAG, "Error get the frame from the filter graph:%d", ret); - goto end; - } - //convert audio format - swr_convert(swrCtx, &out_buffer, MAX_AUDIO_FRAME_SIZE, - (const uint8_t **) /*frame*/filter_frame->data, /*frame*/filter_frame->nb_samples); - int out_buffer_size = av_samples_get_buffer_size(NULL, out_channel, - /*frame*/filter_frame->nb_samples, out_sample_fmt, 1); + //convert audio format + swr_convert(swrCtx, &out_buffer, MAX_AUDIO_FRAME_SIZE, + (const uint8_t **) /*frame*/filter_frame->data, /*frame*/filter_frame->nb_samples); + int out_buffer_size = av_samples_get_buffer_size(NULL, out_channel, + /*frame*/filter_frame->nb_samples, out_sample_fmt, 1); - jbyteArray audio_sample_array = env->NewByteArray(out_buffer_size); - jbyte *sample_byte_array = env->GetByteArrayElements(audio_sample_array, NULL); - memcpy(sample_byte_array, out_buffer, (size_t) out_buffer_size); - env->ReleaseByteArrayElements(audio_sample_array, sample_byte_array, 0); - //call write method to play - env->CallIntMethod(audio_track, audio_track_write_mid, - audio_sample_array, 0, out_buffer_size); - env->DeleteLocalRef(audio_sample_array); - av_frame_unref(filter_frame); - usleep(SLEEP_TIME); - } + jbyteArray audio_sample_array = env->NewByteArray(out_buffer_size); + jbyte *sample_byte_array = env->GetByteArrayElements(audio_sample_array, NULL); + memcpy(sample_byte_array, out_buffer, (size_t) out_buffer_size); + env->ReleaseByteArrayElements(audio_sample_array, sample_byte_array, 0); + //call write method to play + env->CallIntMethod(audio_track, audio_track_write_mid, + audio_sample_array, 0, out_buffer_size); + env->DeleteLocalRef(audio_sample_array); + av_frame_unref(filter_frame); + usleep(SLEEP_TIME); } } av_packet_unref(packet); @@ -331,4 +343,10 @@ end: avformat_close_input(&pFormatCtx); env->ReleaseStringUTFChars(input_jstr, input_cstr); env->ReleaseStringUTFChars(filter_jstr, filter_desc); +} + +AUDIO_PLAYER_FUNC(void, again, jstring filter_jstr) { + if (!filter_jstr) return; + filter_again = 1; + filter_desc = env->GetStringUTFChars(filter_jstr, NULL); } \ No newline at end of file diff --git a/app/src/main/java/com/frank/ffmpeg/AudioPlayer.java b/app/src/main/java/com/frank/ffmpeg/AudioPlayer.java index f34b7e3..229f154 100644 --- a/app/src/main/java/com/frank/ffmpeg/AudioPlayer.java +++ b/app/src/main/java/com/frank/ffmpeg/AudioPlayer.java @@ -17,6 +17,8 @@ public class AudioPlayer { //using AudioTrack to play public native void play(String audioPath, String filterDesc); + public native void again(String filterDesc); + //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 2aa2bc4..481493b 100644 --- a/app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt +++ b/app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt @@ -30,7 +30,7 @@ class EqualizerActivity : BaseActivity(), OnSeeBarListener { private var filterThread: Thread? = null private var mAudioPlayer: AudioPlayer? = null private var equalizerAdapter: EqualizerAdapter? = null - private var audioPath = Environment.getExternalStorageDirectory().path + "/tiger.mp3" + private var audioPath = Environment.getExternalStorageDirectory().path + "/know_play.mp3" override val layoutId: Int get() = R.layout.activity_equalizer @@ -68,6 +68,10 @@ class EqualizerActivity : BaseActivity(), OnSeeBarListener { } private fun doEqualize() { + doEqualize(0, 0) + } + + private fun doEqualize(index: Int, progress: Int) { // val bandList = arrayListOf() // bandList.add("6b=5") // bandList.add("8b=4") @@ -75,19 +79,19 @@ class EqualizerActivity : BaseActivity(), OnSeeBarListener { // bandList.add("12b=2") // bandList.add("14b=1") // bandList.add("16b=0") - val filter = "superequalizer=6b=4:8b=5:10b=5" if (filterThread == null) { + val filter = "superequalizer=6b=4:8b=5:10b=5" filterThread = Thread(Runnable { mAudioPlayer!!.play(audioPath, filter) }) filterThread!!.start() } else { -// mAudioPlayer!!.again(position) + mAudioPlayer!!.again("superequalizer=8b=4:10b=5:12b=5") } } override fun onProgress(index: Int, progress: Int) { - doEqualize() + doEqualize(index, progress) } override fun onViewClick(view: View) {