ARTICLE AD BOX
I have the following ffmpeg code:
AVPacket packet; AVFrame* frame = av_frame_alloc(); std::vector<uint8_t> pcm_buffer; uint8_t* resampled_data; int out_samples = 0; while (av_read_frame(format_ctx, &packet) >= 0) { if (packet.stream_index == audio_stream_index) { if (avcodec_send_packet(codec_ctx, &packet) >= 0) { while (avcodec_receive_frame(codec_ctx, frame) >= 0) { out_samples = swr_get_out_samples(swr_ctx, frame->nb_samples); av_samples_alloc(&resampled_data, nullptr, 2, out_samples, AV_SAMPLE_FMT_S16, 0); int converted_samples = swr_convert(swr_ctx, &resampled_data, out_samples, (const uint8_t**)frame->data, frame->nb_samples); int buffer_size = av_samples_get_buffer_size(nullptr, 2, converted_samples, AV_SAMPLE_FMT_S16, 0); pcm_buffer.insert(pcm_buffer.end(), resampled_data, resampled_data + buffer_size); av_freep(&resampled_data); } } } av_packet_unref(&packet); }And to play the audio I have this code:
int main(int argc, char* argv[]) { if (SDL_Init(SDL_INIT_AUDIO) < 0) { std::cerr << "Error al inicializar SDL: " << SDL_GetError() << std::endl; return 1; } if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) < 0) { std::cerr << "Error al inicializar SDL_mixer: " << Mix_GetError() << std::endl; SDL_Quit(); return 1; } if (!pcm_buffer.empty()) { Mix_Chunk* sound_chunk = Mix_QuickLoad_RAW(pcm_buffer.data(), pcm_buffer.size()); if (sound_chunk == nullptr) { std::cerr << "Error al cargar datos RAW en Mix_Chunk: " << Mix_GetError() << std::endl; } else { // Reproducir el sonido std::cout << "Reproduciendo audio..." << std::endl; Mix_PlayChannel(-1, sound_chunk, 0); // Esperar mientras se reproduce while (Mix_Playing(-1)) { SDL_Delay(100); } // Liberar el chunk Mix_FreeChunk(sound_chunk); } } return 0; }Basically, the audio plays until the entire file is read. My question is, can SDL_Mixer be used to read the audio in real time through ffmpeg?
