Feature: release audio player

pull/221/head
xufuji456 2 years ago
parent 51960d6815
commit efbf287305
  1. 4
      app/src/main/cpp/audio_player_jni.cpp
  2. 8
      app/src/main/cpp/ff_audio_player.cpp
  3. 12
      app/src/main/cpp/ff_audio_player.h

@ -67,3 +67,7 @@ AUDIO_PLAYER_FUNC(void, again, jstring filter_jstr) {
audioPlayer->setFilterDesc(desc); audioPlayer->setFilterDesc(desc);
} }
AUDIO_PLAYER_FUNC(void, release) {
audioPlayer->setExit(true);
}

@ -146,6 +146,9 @@ int FFAudioPlayer::getSampleRate() const {
int FFAudioPlayer::decodeAudio() { int FFAudioPlayer::decodeAudio() {
int ret; int ret;
if (exitPlaying.load()) {
return -1;
}
// demux: read a frame(should be demux thread) // demux: read a frame(should be demux thread)
ret = av_read_frame(formatContext, packet); ret = av_read_frame(formatContext, packet);
if (ret < 0) { if (ret < 0) {
@ -209,7 +212,6 @@ int FFAudioPlayer::decodeAudio() {
return buffer_size; return buffer_size;
} }
uint8_t *FFAudioPlayer::getDecodeFrame() const { uint8_t *FFAudioPlayer::getDecodeFrame() const {
return out_buffer; return out_buffer;
} }
@ -222,6 +224,10 @@ void FFAudioPlayer::setFilterDesc(const char *filterDescription) {
filterDesc = filterDescription; filterDesc = filterDescription;
} }
void FFAudioPlayer::setExit(bool exit) {
exitPlaying = exit;
}
void FFAudioPlayer::close() { void FFAudioPlayer::close() {
if (formatContext) { if (formatContext) {
avformat_close_input(&formatContext); avformat_close_input(&formatContext);

@ -2,9 +2,10 @@
// Created by xu fulong on 2022/9/4. // Created by xu fulong on 2022/9/4.
// //
#ifndef LEARNINGMEDIA_FF_AUDIO_PLAYER_H #ifndef FF_AUDIO_PLAYER_H
#define LEARNINGMEDIA_FF_AUDIO_PLAYER_H #define FF_AUDIO_PLAYER_H
#include <stdatomic.h>
#include "ffmpeg_jni_define.h" #include "ffmpeg_jni_define.h"
#ifdef __cplusplus #ifdef __cplusplus
@ -37,8 +38,9 @@ private:
AVFrame *filterFrame; AVFrame *filterFrame;
uint8_t *out_buffer; uint8_t *out_buffer;
bool filterAgain;
const char *filterDesc; const char *filterDesc;
std::atomic_bool filterAgain;
std::atomic_bool exitPlaying;
AVFilterGraph *audioFilterGraph; AVFilterGraph *audioFilterGraph;
AVFilterContext *audioSrcContext; AVFilterContext *audioSrcContext;
@ -59,6 +61,8 @@ public:
void setFilterDesc(const char *filterDescription); void setFilterDesc(const char *filterDescription);
void setExit(bool exit);
void close(); void close();
}; };
#endif //LEARNINGMEDIA_FF_AUDIO_PLAYER_H #endif //FF_AUDIO_PLAYER_H

Loading…
Cancel
Save