首页 > 其他分享 >使用libmpg123加alsa实现MP3的播放/暂停,切换,模式选择,C语言3

使用libmpg123加alsa实现MP3的播放/暂停,切换,模式选择,C语言3

时间:2024-09-07 17:53:24浏览次数:6  
标签:libmpg123 song pthread snd C语言 mpg123 mutex MP3 pcm

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(&params);
    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

相关文章

  • 鹏哥课程三子棋 全码C语言
    代码来自在b站上的鹏哥的课程,大部分为手打,少部分直接搬运了csdn博客其他学习者的代码。该三子棋代码内容全免费。这篇文章仅提供给认真上课的学习者参考使用,经过测试该代码能够正常运行使用。前言唯一美中不足的是,在开始游戏界面如果输入下棋坐标,会导致死循环。共包含三篇,......
  • 【C语言基础】赋值、输入与输出
    1语句及赋值语句1.1C语句分类表达式语句:表达式加分号。函数调用语句:函数名及其参数加上分号。如:printf("Helloworld!");空语句:只有分号";"组成。控制语句条件判断语句:if语句,switch语句循环语句:while语句,dowhile语句,for语句转向语句:break语句,continue语句,goto语句,retu......
  • 【C语言基础】数组
    1一维数组1.1定义类型说明符数组名[常量表达式];如:inta[10];这里,a是数组变量名,10表示数组a中包含的元素个数,int是数组a中元素的类型。数组a的元素序号为0,1,2,3,4,5,6,7,8,9,各序号对应的元素为a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]。1.2初始化类......
  • C语言 ——— #define 定义宏
    目录何为宏宏的声明及其使用方式宏中的括号是否多余何为宏#define机制包括了一个规定,允许把参数替换到文本中,这种实现通常称宏宏的声明及其使用方式声明代码演示:#defineMAX(x,y)((x)>(y)?(x):(y))使用代码演示:inta=3;intb=5;intmax=MAX(a,b);代......
  • 重生之霸道C语言爱上我之走入指针(3)
    根据前两篇与指针的初步接触后,我们已经了解到了指针里面最基本的知识,而接下来的文章,将会更注重于指针的深入理解和运用。1.数组名的理解1.1arr,即一个数组的数组名的理解在上一篇文章1.1里面的代码中,我们在用指针访问数组内容的时候,有这样的代码:intarr[10]={1,2,3,4,5,6......
  • C语言常量和字面量
    目录引言1.字面量1.1字符字面量1.2整型字面量1.3浮点字面量2.常量2.1使用预处理器指令#define定义常量2.1.1语法格式2.1.2使用举例2.2使用const关键字定义常量2.3使用#define和const定义常量的区别引言        字面量是直接在代码中......
  • 全国计算机二级考试C语言篇3——选择题
    C语言部分——C语言概述1.程序模块化的优点程序模块化的优点在于它可以使程序的开发、维护和复用变得更简单。下面是一些主要的优点:降低复杂度:模块化可以将复杂的问题分解成更小的、更易管理的部分。可维护性:模块化使得代码更易于维护,因为修改一个模块的影响被限制在该......
  • c语言内存函数
    今天来学习C语言中的内存函数目录1.memcpy代码形式示例运行结果2.memmove代码形式示例运行结果3.memset代码形式示例运行结果4.memcmp形式示例运行结果![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/b7d8d59577b248deaa7b869d014d8b4f.png#pic_center)5......
  • 学习笔记|鹏哥C语言——关键字
    一、 关键字typedeftypedef顾名思义是类型定义,这里应该理解为类型重命名。比如:二、 关键字static在C语言中:static是用来修饰变量和函数的1.修饰局部变量-称为静态局部变量2.修饰全局变量-称为静态全局变量3.修饰函数-称为静态函数三、 修饰局部变量......
  • E31.【C语言】练习:指针运算习题集(上)
    Exercise1求下列代码的运行结果#include<stdio.h>intmain(){ inta[5]={1,2,3,4,5}; int*ptr=(int*)(&a+1); printf("%d",*(ptr-1)); return0;}答案速查:分析:Exercise2 求下列代码的运行结果//在x86环境下//假设结构体的大小是20个字节......