-
大小: 3KB文件類型: .gz金幣: 1下載: 0 次發布日期: 2023-12-26
- 語言: 其他
- 標簽:
資源簡介
因為想弄個基于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 {
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年下半年 網絡工程師 答案詳解
評論
共有 條評論