资源简介
因为想弄个基于ffmpeg 和 alsa 的音频播放器,但是网上找了很久都没有这方面的,有也是有些问题的,要不是不能播,就是播出来的声音不对,总之很多限制。所以自己网上找资料,东拼西凑。终于把这个播放器给弄出来了,功能:可以播(ape,wav,mp3,flac(多声道也可以))。播放进度没弄。
代码片段和文件信息
#include
#include
#include
#include
#include
#include
void ffmpeg_fmt_to_alsa_fmt(AVCodecContext *pCodecCtx snd_pcm_t *pcm snd_pcm_hw_params_t *params) //设置位数
{
printf(“fmt:%d\n“pCodecCtx->sample_fmt);
switch(pCodecCtx->sample_fmt) {
case AV_SAMPLE_FMT_U8:
snd_pcm_hw_params_set_format(pcm params SND_PCM_FORMAT_S8);
break;
case AV_SAMPLE_FMT_S16:
snd_pcm_hw_params_set_format(pcm params SND_PCM_FORMAT_S16);
break;
case AV_SAMPLE_FMT_S32:
snd_pcm_hw_params_set_format(pcm params SND_PCM_FORMAT_S32);
break;
default:
snd_pcm_hw_params_set_format(pcm params SND_PCM_FORMAT_S16);
break;
}
}
void display_codec_ctx(AVCodecContext *pCocecCtx)
{
printf(“fmt: %d%d\n“ pCocecCtx->sample_fmtAV_SAMPLE_FMT_S16);
printf(“channels: %d\n“ pCocecCtx->channels);
printf(“time: %d\n“ pCocecCtx->time_base);
}
long vol;
void *thrHdlr(void *arg) {
char c;
snd_mixer_elem_t *elem = (snd_mixer_elem_t *)arg;
while (1) {
c = getchar();
if (c == 119) {
snd_mixer_selem_set_playback_volume_all(elem vol += 2000);
} else if (c == 115) {
snd_mixer_selem_set_playback_volume_all(elem vol -= 2000);
}
}
return ((void *)0);
}
void * convertAV_SAMPLE_FMT_FLTP_TO_S16P(const AVframe *frame) // float 转 16位
{
if(frame == NULL)
{
printf(“frmae== NULL\n“);
return;
}
int in_samples = frame->nb_samples;
short *sample_buffer = (short*)malloc(frame->nb_samples*2*2);
memset(sample_buffer0frame->nb_samples*4);
int i=0;
float* inputChanne10 = (float*)(frame->extended_data[0]);
if(frame->channels == 1)
{
for(i = 0 ; i {
float sample = *inputChanne10++;
if(sample< -1.0f)
sample=-1.0f;
else if(sample >1.0f)
sample = 1.0f;
sample_buffer[i] = (int16_t)(sample*32767.0f);
}
}
else
{
float *inputChannel1 = (float*)(frame->extended_data[1]);
for(i = 0; i< in_samples;i++)
{
float sample1 = *inputChanne10++;
if(sample1< -1.0f)
sample1=-1.0f;
else if(sample1 >1.0f)
sample1 = 1.0f;
float sample2 = *inputChannel1++;
if(sample2< -1.0f)
sample2=-1.0f;
else if(sample2 >1.0f)
sample2 = 1.0f;
sample_buffer[i*2] = (int16_t)(sample1*32767.0f);
sample_buffer[i*2+1] = (int16_t)(sample2 * 32767.0f);
}
}
return sample_buffer;
}
int writeframe(AVframe *decoded_frame AVPacket *pkt AVCodecContext *pInCodecCtx snd_pcm_t* pcm)
{
int got_frame = 0;
int len = 0;
while (pkt->size > 0)
{
if (!decoded_frame)
{
if (!(decoded_frame = av_frame_alloc()))
{
decoded_frame = NULL;
return -1;
- 上一篇:基于stm32成功配置si4438
- 下一篇:2017年下半年 网络工程师 答案详解
评论
共有 条评论