使用ffmpeg库加上alsa库使用mp3音乐的播放/暂停,切换,歌曲模式选择2

note:使用ffmpeg库解码mp3文件,使用alsa播放解码后的数据。

与上一篇不同的是这个播放/暂停的核心是记录暂停解码的pts,重新播放时从暂停的pts开始解码,暂停就是结束线程,播放就是开始线程,至于其他部分跟上一篇一致,本文不做描述,以下为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>
#include <sys/prctl.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;

// 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(&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, "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;
}

// Decode and play song
void *play_song_thread(void *arg) {
    int song_index = *(int *)arg;
    free(arg);
    prctl(PR_SET_NAME, "test");

    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);
        printf("pause_pts is %ld\r\n",pause_pts);
    }

    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) { // 列表循环
                pause_pts = 0;
                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");
                pause_pts = 0;
                //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;
}

// 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);
}

// 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设备恢复播放
         start_song(current_song);
    } else {
        is_paused = 1;
        AVPacket current_packet;
        // 获取当前正在处理的音频包
        // 假设你在播放循环中已经有获取音频包的逻辑
        av_read_frame(format_ctx, &current_packet);
        pause_pts = current_packet.pts;
        printf("pause_pts is %ld\r\n",pause_pts);
         if (is_playing) {
        stop_playback = 1;
        pthread_cond_signal(&cond);
        pthread_join(play_thread, NULL);
        stop_playback = 0;
    }
     
    }
  }else{
      start_song(current_song);
  }
    //pthread_mutex_unlock(&mutex);
}

// Play next song
void play_next_song() {
    pthread_mutex_lock(&mutex);
    pause_pts = 0;
    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);
    pause_pts = 0;
    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);

    // Start playing the first song by default
  

    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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hqb_newfarmer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值