前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >从0开始做播放器---音频播放有杂音且音调异常

从0开始做播放器---音频播放有杂音且音调异常

作者头像
瑶瑶
发布2020-07-29 15:41:59
1.4K0
发布2020-07-29 15:41:59
举报

我的播放器,音频像是电视信号不好,需要动一下天线,有那种沙沙声。 明确音频数据问题,在get音频数据处找问题。

  1. 音调不对,原因是我用的播放器只能播放 packed类型pcm数据,所以做了planar-->packed类型转换,将AV_SAMPLE_FMT_FLTP planar的样本格式转成AV_SAMPLE_FMT_S32 packed 类型的。
  2. 有杂音,原因是音频原样本格式是AV_SAMPLE_FMT_FLTP,float类型 32位,而我用的opensl播放,只支持int类型的样本,所以需要转换成32位的int类型 ,即AVSampleFormat::AV_SAMPLE_FMT_S32。

代码如下

int YaoAVFrame::getAudioPackedData(unsigned char * data){
    //每个样本的字节数 * 每个声道的样本数 * 声道数
    int bufferSize =  getPerSampleSize() * getNBSamples() * getChannels();
    if(data == nullptr){
        return bufferSize;
    }

    // 判断是 Packed 还是 Plane
    int isPanar = av_sample_fmt_is_planar((AVSampleFormat)imp->frame->format);
    if(isPanar){
        //EyerLog("Planar\n");
        SwrContext * swrCtx = swr_alloc_set_opts(
                NULL,
                imp->frame->channel_layout,
                // av_get_packed_sample_fmt((AVSampleFormat)imp->frame->format),
                AVSampleFormat::AV_SAMPLE_FMT_S32,
                imp->frame->sample_rate,

                imp->frame->channel_layout,
                (AVSampleFormat)imp->frame->format,
                imp->frame->sample_rate,

                0,
                NULL
        );


        swr_init(swrCtx);

        int ret = swr_convert(swrCtx, &data, imp->frame->nb_samples, (const uint8_t **)imp->frame->data, imp->frame->nb_samples);

        swr_free(&swrCtx);
    }
    else{
        //EyerLog("Packed\n");
        memcpy(data, imp->frame->data[0], bufferSize);
    }

    return 0;
}

完整代码:https://github.com/yinhuiyao11/YaoPlayerAndroid.git

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档