diff --git a/app/src/main/cpp/audio_player_jni.cpp b/app/src/main/cpp/audio_player_jni.cpp index 75c12ef..0f2de9f 100644 --- a/app/src/main/cpp/audio_player_jni.cpp +++ b/app/src/main/cpp/audio_player_jni.cpp @@ -67,3 +67,7 @@ AUDIO_PLAYER_FUNC(void, again, jstring filter_jstr) { audioPlayer->setFilterDesc(desc); } +AUDIO_PLAYER_FUNC(void, release) { + audioPlayer->setExit(true); +} + diff --git a/app/src/main/cpp/ff_audio_player.cpp b/app/src/main/cpp/ff_audio_player.cpp index b8fb658..845c1fa 100644 --- a/app/src/main/cpp/ff_audio_player.cpp +++ b/app/src/main/cpp/ff_audio_player.cpp @@ -146,6 +146,9 @@ int FFAudioPlayer::getSampleRate() const { int FFAudioPlayer::decodeAudio() { int ret; + if (exitPlaying.load()) { + return -1; + } // demux: read a frame(should be demux thread) ret = av_read_frame(formatContext, packet); if (ret < 0) { @@ -209,7 +212,6 @@ int FFAudioPlayer::decodeAudio() { return buffer_size; } - uint8_t *FFAudioPlayer::getDecodeFrame() const { return out_buffer; } @@ -222,6 +224,10 @@ void FFAudioPlayer::setFilterDesc(const char *filterDescription) { filterDesc = filterDescription; } +void FFAudioPlayer::setExit(bool exit) { + exitPlaying = exit; +} + void FFAudioPlayer::close() { if (formatContext) { avformat_close_input(&formatContext); diff --git a/app/src/main/cpp/ff_audio_player.h b/app/src/main/cpp/ff_audio_player.h index 61b1163..1f330cf 100644 --- a/app/src/main/cpp/ff_audio_player.h +++ b/app/src/main/cpp/ff_audio_player.h @@ -2,9 +2,10 @@ // Created by xu fulong on 2022/9/4. // -#ifndef LEARNINGMEDIA_FF_AUDIO_PLAYER_H -#define LEARNINGMEDIA_FF_AUDIO_PLAYER_H +#ifndef FF_AUDIO_PLAYER_H +#define FF_AUDIO_PLAYER_H +#include #include "ffmpeg_jni_define.h" #ifdef __cplusplus @@ -37,8 +38,9 @@ private: AVFrame *filterFrame; uint8_t *out_buffer; - bool filterAgain; const char *filterDesc; + std::atomic_bool filterAgain; + std::atomic_bool exitPlaying; AVFilterGraph *audioFilterGraph; AVFilterContext *audioSrcContext; @@ -59,6 +61,8 @@ public: void setFilterDesc(const char *filterDescription); + void setExit(bool exit); + void close(); }; -#endif //LEARNINGMEDIA_FF_AUDIO_PLAYER_H +#endif //FF_AUDIO_PLAYER_H