update equalizer when seeking progress

dev
xufuji456 3 years ago
parent b9b2d4aab9
commit 1255b906a8
  1. 26
      app/src/main/cpp/audio_player.cpp
  2. 2
      app/src/main/java/com/frank/ffmpeg/AudioPlayer.java
  3. 12
      app/src/main/java/com/frank/ffmpeg/activity/EqualizerActivity.kt

@ -28,7 +28,8 @@ extern "C" {
#define SLEEP_TIME 1000 * 16 #define SLEEP_TIME 1000 * 16
#define MAX_AUDIO_FRAME_SIZE 48000 * 4 #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, int init_volume_filter(AVFilterGraph **graph, AVFilterContext **src, AVFilterContext **sink,
uint64_t channel_layout, AVSampleFormat inputFormat, int sample_rate) { 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); const char *input_cstr = env->GetStringUTFChars(input_jstr, NULL);
LOGI(TAG, "input url=%s", input_cstr); 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); LOGE(TAG, "filter_desc=%s", filter_desc);
av_register_all(); av_register_all();
AVFormatContext *pFormatCtx = avformat_alloc_context(); AVFormatContext *pFormatCtx = avformat_alloc_context();
@ -280,7 +281,19 @@ AUDIO_PLAYER_FUNC(void, play, jstring input_jstr, jstring filter_jstr) {
//read audio frame //read audio frame
while (av_read_frame(pFormatCtx, packet) >= 0) { while (av_read_frame(pFormatCtx, packet) >= 0) {
if (packet->stream_index == audio_stream_idx) { 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); ret = avcodec_decode_audio4(codecCtx, frame, &got_frame, packet);
if (ret < 0) { if (ret < 0) {
break; break;
@ -317,7 +330,6 @@ AUDIO_PLAYER_FUNC(void, play, jstring input_jstr, jstring filter_jstr) {
usleep(SLEEP_TIME); usleep(SLEEP_TIME);
} }
} }
}
av_packet_unref(packet); av_packet_unref(packet);
} }
end: end:
@ -332,3 +344,9 @@ end:
env->ReleaseStringUTFChars(input_jstr, input_cstr); env->ReleaseStringUTFChars(input_jstr, input_cstr);
env->ReleaseStringUTFChars(filter_jstr, filter_desc); 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);
}

@ -17,6 +17,8 @@ public class AudioPlayer {
//using AudioTrack to play //using AudioTrack to play
public native void play(String audioPath, String filterDesc); public native void play(String audioPath, String filterDesc);
public native void again(String filterDesc);
//using OpenSL ES to play //using OpenSL ES to play
public native void playAudio(String audioPath); public native void playAudio(String audioPath);

@ -30,7 +30,7 @@ class EqualizerActivity : BaseActivity(), OnSeeBarListener {
private var filterThread: Thread? = null private var filterThread: Thread? = null
private var mAudioPlayer: AudioPlayer? = null private var mAudioPlayer: AudioPlayer? = null
private var equalizerAdapter: EqualizerAdapter? = 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 override val layoutId: Int
get() = R.layout.activity_equalizer get() = R.layout.activity_equalizer
@ -68,6 +68,10 @@ class EqualizerActivity : BaseActivity(), OnSeeBarListener {
} }
private fun doEqualize() { private fun doEqualize() {
doEqualize(0, 0)
}
private fun doEqualize(index: Int, progress: Int) {
// val bandList = arrayListOf<String>() // val bandList = arrayListOf<String>()
// bandList.add("6b=5") // bandList.add("6b=5")
// bandList.add("8b=4") // bandList.add("8b=4")
@ -75,19 +79,19 @@ class EqualizerActivity : BaseActivity(), OnSeeBarListener {
// bandList.add("12b=2") // bandList.add("12b=2")
// bandList.add("14b=1") // bandList.add("14b=1")
// bandList.add("16b=0") // bandList.add("16b=0")
val filter = "superequalizer=6b=4:8b=5:10b=5"
if (filterThread == null) { if (filterThread == null) {
val filter = "superequalizer=6b=4:8b=5:10b=5"
filterThread = Thread(Runnable { filterThread = Thread(Runnable {
mAudioPlayer!!.play(audioPath, filter) mAudioPlayer!!.play(audioPath, filter)
}) })
filterThread!!.start() filterThread!!.start()
} else { } else {
// mAudioPlayer!!.again(position) mAudioPlayer!!.again("superequalizer=8b=4:10b=5:12b=5")
} }
} }
override fun onProgress(index: Int, progress: Int) { override fun onProgress(index: Int, progress: Int) {
doEqualize() doEqualize(index, progress)
} }
override fun onViewClick(view: View) { override fun onViewClick(view: View) {

Loading…
Cancel
Save