note:使用多线程的方式MP3实现播放器,其中用到libmpg123,以及asound库,解码用到libmpg123,播放用到alsa,以下为c语言例程源码
#include <alsa/asoundlib.h>
#include <mpg123.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#define PCM_DEVICE "default"
#define MAX_SONGS 3
// 歌曲列表
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: 单曲播放, 1: 单曲循环, 2: 列表循环
int is_paused = 0; // 播放暂停标志
int stop_playback = 0; // 停止播放标志
int is_playing = 0; // 是否正在播放标志
char *music_mode[3]={"单曲模式","单曲循环","列表循环"};
pthread_mutex_t mutex;
pthread_cond_t cond;
mpg123_handle *mh;
snd_pcm_t *pcm_handle;
pthread_t play_thread;
// 清理函数,释放资源
void cleanup() {
if (mh) {
mpg123_close(mh);
mpg123_delete(mh);
}
if (pcm_handle) {
snd_pcm_close(pcm_handle);
}
mpg123_exit();
}
// 设置ALSA设备
int setup_alsa(int rate, int channels) {
int err;
snd_pcm_hw_params_t *params;
if ((err = snd_pcm_open(&pcm_handle, PCM_DEVICE, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
fprintf(stderr, "无法打开PCM设备 %s: %s\n", PCM_DEVICE, 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, "无法设置PCM硬件参数: %s\n", snd_strerror(err));
return -1;
}
if ((err = snd_pcm_prepare(pcm_handle)) < 0) {
fprintf(stderr, "无法准备PCM设备: %s\n", snd_strerror(err));
return -1;
}
return 0;
}
// 播放歌曲的线程函数
void *play_song_thread(void *arg) {
int song_index = *(int *)arg;
free(arg); // 释放分配的内存
int err;
size_t done;
unsigned char *audio;
int channels, encoding;
long rate;
// 打开MP3文件
if (mpg123_open(mh, songs[song_index]) != MPG123_OK) {
fprintf(stderr, "无法打开文件 %s: %s\n", songs[song_index], mpg123_strerror(mh));
is_playing = 0;
return NULL;
}
// 获取MP3格式
if (mpg123_getformat(mh, &rate, &channels, &encoding) != MPG123_OK) {
fprintf(stderr, "无法获取文件格式: %s\n", mpg123_strerror(mh));
mpg123_close(mh);
is_playing = 0;
return NULL;
}
// 配置ALSA
if (setup_alsa(rate, channels) != 0) {
mpg123_close(mh);
is_playing = 0;
return NULL;
}
// 设置输出格式为当前格式
mpg123_format_none(mh);
mpg123_format(mh, rate, channels, encoding);
// 分配音频缓冲区
size_t buffer_size = mpg123_outblock(mh);
audio = (unsigned char*) malloc(buffer_size);
if (!audio) {
fprintf(stderr, "无法分配音频缓冲区\n");
snd_pcm_close(pcm_handle);
mpg123_close(mh);
is_playing = 0;
return NULL;
}
printf("当前播放歌曲是%s ,歌曲模式是%s \r\n",songs[song_index],music_mode[play_mode]);
is_playing = 1;
while (!stop_playback) {
pthread_mutex_lock(&mutex);
while (is_paused && !stop_playback) {
pthread_cond_wait(&cond, &mutex);
}
pthread_mutex_unlock(&mutex);
if (stop_playback) break;
int ret = mpg123_read(mh, audio, buffer_size, &done);
if (ret == MPG123_OK || ret == MPG123_DONE) {
// 写入PCM数据
if ((err = snd_pcm_writei(pcm_handle, audio, done / (channels * 2))) < 0) { // 2 bytes per sample for S16_LE
fprintf(stderr, "snd_pcm_writei error: %s\n", snd_strerror(err));
snd_pcm_prepare(pcm_handle);
}
}
if (ret == MPG123_DONE) {
// 根据播放模式处理
pthread_mutex_lock(&mutex);
if (play_mode == 1) { // 单曲循环
mpg123_seek(mh, 0, SEEK_SET);
pthread_mutex_unlock(&mutex);
continue;
} else if (play_mode == 2) { // 列表循环
current_song = (current_song + 1) % MAX_SONGS;
pthread_mutex_unlock(&mutex);
// 关闭当前句柄,开始下一首
free(audio); // 释放音频缓冲区
snd_pcm_drop(pcm_handle);
mpg123_close(mh);
is_playing = 0; // 确保标志清零,表明当前线程已结束
start_song(current_song);
return NULL; // 退出当前线程
} else { // 单曲播放
pthread_mutex_unlock(&mutex);
break; // 退出循环
}
} else if (ret != MPG123_OK) {
fprintf(stderr, "mpg123_read error: %s\n", mpg123_strerror(mh));
break;
}
}
printf("退出线程了\r\n");
free(audio);
snd_pcm_drain(pcm_handle);
snd_pcm_close(pcm_handle);
mpg123_close(mh);
is_playing = 0;
return NULL;
}
// 开始播放歌曲
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));
if (!arg) {
fprintf(stderr, "无法分配内存\n");
return;
}
*arg = song_index;
// 重置暂停标志
is_paused = 0;
// 创建新的播放线程
if (pthread_create(&play_thread, NULL, play_song_thread, arg) != 0) {
fprintf(stderr, "无法创建播放线程\n");
free(arg);
return;
}
}
// 切换播放/暂停状态
void toggle_play_pause() {
pthread_mutex_lock(&mutex);
if (is_playing) { // 如果当前正在播放
if (is_paused) {
// 恢复播放
is_paused = 0;
pthread_cond_signal(&cond);
printf("继续播放\n");
} else {
// 暂停播放
is_paused = 1;
printf("暂停播放\n");
}
} else {
start_song(current_song); // 如果没有播放,开始播放当前歌曲
}
pthread_mutex_unlock(&mutex);
}
// 播放下一首歌曲
void play_next_song() {
pthread_mutex_lock(&mutex);
current_song = (current_song + 1) % MAX_SONGS;
pthread_mutex_unlock(&mutex);
start_song(current_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);
}
// 切换播放模式
void select_play_mode() {
pthread_mutex_lock(&mutex);
play_mode = (play_mode + 1) % 3;
const char *mode_names[] = {"单曲播放", "单曲循环", "列表循环"};
printf("播放模式: %s\n", mode_names[play_mode]);
pthread_mutex_unlock(&mutex);
}
int main() {
int command;
// 初始化 mpg123
if (mpg123_init() != MPG123_OK) {
fprintf(stderr, "无法初始化 mpg123\n");
return 1;
}
mh = mpg123_new(NULL, NULL);
if (!mh) {
fprintf(stderr, "无法创建 mpg123 句柄\n");
mpg123_exit();
return 1;
}
// 初始化 mutex 和 cond
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
while (1) {
printf("请输入指令 (1: 播放/暂停, 2: 下一首, 3: 上一首, 4: 切换模式): ");
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:
select_play_mode(); // 切换播放模式
break;
default:
printf("无效的指令\n");
break;
}
}
// 实际应用中,需要处理退出信号以调用 cleanup()
cleanup();
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
标签:libmpg123,song,pthread,snd,C语言,mpg123,mutex,MP3,pcm
From: https://blog.csdn.net/hqb_newfarmer/article/details/141999681