note:使用ffmpeg库对mp3文件进行解码,然后使用alsa库进行播放解码后的数据。
暂停/播放的核心是使用互斥锁加上条件变量阻塞进程,从而实现暂停/播放的效果,至于执行相应的歌曲模式选择在循环中判断读取数据是否完毕,完毕则执行相应的歌曲操作。以下为c语言例程源码。
#include <alsa/asoundlib.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#define PCM_DEVICE "default"
#define MAX_SONGS 3
// Song playlist
char *songs[MAX_SONGS] = {
"/mnt/hgfs/share/talk_anymore.mp3",
"/mnt/hgfs/share/poxiao.mp3",
"/mnt/hgfs/share/attention.mp3"
};
int current_song = 0;
int play_mode = 0; // 0: Single play, 1: Repeat single, 2: Repeat playlist
int is_paused = 0;
int stop_playback = 0;
int is_playing = 0;
//int64_t pause_pts = 0; // Track the position where playback was paused
pthread_mutex_t mutex;
pthread_cond_t cond;
pthread_t play_thread;
snd_pcm_t *pcm_handle;
AVFormatContext *format_ctx = NULL;
AVCodecContext *codec_ctx = NULL;
struct SwrContext *swr_ctx = NULL;
int audio_stream_index = -1;
AVPacket packet;
void *play_song_thread(void *arg);
// Cleanup resources
void cleanup() {
if (swr_ctx) {
swr_free(&swr_ctx);
}
if (codec_ctx) {
avcodec_free_context(&codec_ctx);
}
if (format_ctx) {
avformat_close_input(&format_ctx);
}
if (pcm_handle) {
snd_pcm_close(pcm_handle);
}
av_packet_unref(&packet);
}
// Setup ALSA
int setup_alsa(int rate, int channels) {
snd_pcm_hw_params_t *params;
int err;
if ((err = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf(stderr, "Unable to open PCM device: %s\n", snd_strerror(err));
return -1;
}
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(pcm_handle, params);
snd_pcm_hw_params_set_access(pcm_handle, params, SND_PCM_ACCESS_RW_INTERLEAVED);
snd_pcm_hw_params_set_format(pcm_handle, params, SND_PCM_FORMAT_S16_LE);
snd_pcm_hw_params_set_channels(pcm_handle, params, channels);
snd_pcm_hw_params_set_rate(pcm_handle, params, rate, 0);
if ((err = snd_pcm_hw_params(pcm_handle, params)) < 0) {
fprintf(stderr, "Unable to set PCM hardware parameters: %s\n", snd_strerror(err));
return -1;
}
if ((err = snd_pcm_prepare(pcm_handle)) < 0) {
fprintf(stderr, "Unable to prepare PCM device: %s\n", snd_strerror(err));
return -1;
}
return 0;
}
// Start playing song
void start_song(int song_index) {
if (is_playing) {
stop_playback = 1;
pthread_cond_signal(&cond);
pthread_join(play_thread, NULL);
stop_playback = 0;
}
int *arg = malloc(sizeof(int));
*arg = song_index;
is_paused = 0;
pthread_create(&play_thread, NULL, play_song_thread, arg);
}
// Decode and play song
void *play_song_thread(void *arg) {
int song_index = *(int *)arg;
free(arg);
AVFrame *frame = av_frame_alloc();
AVPacket packet;
uint8_t *output_buffer = NULL;
int output_buffer_size, err;
int got_frame = 0;
// Open MP3 file
if (avformat_open_input(&format_ctx, songs[song_index], NULL, NULL) != 0) {
fprintf(stderr, "Unable to open file %s\n", songs[song_index]);
return NULL;
}
if (avformat_find_stream_info(format_ctx, NULL) < 0) {
fprintf(stderr, "Unable to find stream information\n");
return NULL;
}
audio_stream_index = av_find_best_stream(format_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
if (audio_stream_index < 0) {
fprintf(stderr, "Unable to find audio stream\n");
return NULL;
}
AVStream *audio_stream = format_ctx->streams[audio_stream_index];
AVCodec *codec = avcodec_find_decoder(audio_stream->codecpar->codec_id);
codec_ctx = avcodec_alloc_context3(codec);
avcodec_parameters_to_context(codec_ctx, audio_stream->codecpar);
avcodec_open2(codec_ctx, codec, NULL);
swr_ctx = swr_alloc_set_opts(NULL,
AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, codec_ctx->sample_rate,
av_get_default_channel_layout(codec_ctx->channels), codec_ctx->sample_fmt, codec_ctx->sample_rate,
0, NULL);
swr_init(swr_ctx);
// Setup ALSA with MP3 parameters
setup_alsa(codec_ctx->sample_rate, codec_ctx->channels);
is_playing = 1;
// Allocate output buffer
output_buffer_size = av_samples_get_buffer_size(NULL, 2, codec_ctx->frame_size, AV_SAMPLE_FMT_S16, 1);
output_buffer = av_malloc(output_buffer_size);
// Seek to the paused position if playback was paused
// if (pause_pts > 0) {
// av_seek_frame(format_ctx, audio_stream_index, pause_pts, AVSEEK_FLAG_BACKWARD);
// pause_pts = 0; // Reset the paused position
// }
while (!stop_playback) {
if (is_paused) {
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
}
if (av_read_frame(format_ctx, &packet) < 0) {
// 根据播放模式处理
pthread_mutex_lock(&mutex);
if (play_mode == 1) { // 单曲循环
avformat_seek_file(format_ctx, -1, INT64_MIN, 0, INT64_MAX, 0);
pthread_mutex_unlock(&mutex);
printf("单曲循环\r\n");
continue;
} else if (play_mode == 2) { // 列表循环
current_song = (current_song + 1) % MAX_SONGS;
pthread_mutex_unlock(&mutex);
// 关闭当前句柄,开始下一首
av_frame_free(&frame);
av_free(output_buffer);
// snd_pcm_drain(pcm_handle);
snd_pcm_close(pcm_handle);
avformat_close_input(&format_ctx);
is_playing = 0;
printf("列表循环\r\n");
start_song(current_song);
return NULL; // 退出当前线程
} else { // 单曲播放
printf("单曲播放\r\n");
pthread_mutex_unlock(&mutex);
break; // 退出循环
}
}
if (packet.stream_index == audio_stream_index) {
err = avcodec_send_packet(codec_ctx, &packet);
if (err < 0) {
fprintf(stderr, "Error sending packet for decoding\n");
break;
}
while (err >= 0) {
err = avcodec_receive_frame(codec_ctx, frame);
if (err == AVERROR(EAGAIN) || err == AVERROR_EOF) {
break;
} else if (err < 0) {
fprintf(stderr, "Error decoding audio frame\n");
break;
}
swr_convert(swr_ctx, &output_buffer, output_buffer_size, (const uint8_t **)frame->extended_data, frame->nb_samples);
snd_pcm_writei(pcm_handle, output_buffer, frame->nb_samples);
}
}
av_packet_unref(&packet);
}
av_frame_free(&frame);
av_free(output_buffer);
snd_pcm_drain(pcm_handle);
snd_pcm_close(pcm_handle);
avformat_close_input(&format_ctx);
is_playing = 0;
return NULL;
}
// Toggle play/pause
void toggle_play_pause() {
pthread_mutex_lock(&mutex);
if(is_playing){
if (is_paused) {
is_paused = 0;
pthread_cond_signal(&cond);
snd_pcm_prepare(pcm_handle); // 准备ALSA设备恢复播放
} else {
is_paused = 1;
// pause_pts = codec_ctx->frame_number; // Store the paused position
}
}else{
start_song(current_song);
}
pthread_mutex_unlock(&mutex);
}
// Play next song
void play_next_song() {
pthread_mutex_lock(&mutex);
current_song = (current_song + 1) % MAX_SONGS;
pthread_mutex_unlock(&mutex);
start_song(current_song);
}
// Play previous song
void play_previous_song() {
pthread_mutex_lock(&mutex);
current_song = (current_song - 1 + MAX_SONGS) % MAX_SONGS;
pthread_mutex_unlock(&mutex);
start_song(current_song);
}
// Switch play mode
void switch_play_mode() {
pthread_mutex_lock(&mutex);
play_mode = (play_mode + 1) % 3;
const char *mode_str[] = {"Single play", "Repeat single", "Repeat playlist"};
printf("Play mode: %s\n", mode_str[play_mode]);
pthread_mutex_unlock(&mutex);
}
int main() {
int command;
// av_register_all();
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
while (1) {
printf("Enter command (1: Play/Pause, 2: Next, 3: Previous, 4: Switch mode): ");
scanf("%d", &command);
switch (command) {
case 1:
toggle_play_pause();
break;
case 2:
play_next_song();
break;
case 3:
play_previous_song();
break;
case 4:
switch_play_mode();
break;
default:
printf("Invalid command\n");
break;
}
}
cleanup();
return 0;
}