從0開始做播放器---音頻播放有雜音且音調(diào)異常

我的播放器,音頻像是電視信號(hào)不好,需要?jiǎng)右幌绿炀€,有那種沙沙聲。
明確音頻數(shù)據(jù)問(wèn)題,在get音頻數(shù)據(jù)處找問(wèn)題。

  1. 音調(diào)不對(duì),原因是我用的播放器只能播放 packed類型pcm數(shù)據(jù),所以做了planar-->packed類型轉(zhuǎn)換,將AV_SAMPLE_FMT_FLTP planar的樣本格式轉(zhuǎn)成AV_SAMPLE_FMT_S32 packed 類型的。
  2. 有雜音,原因是音頻原樣本格式是AV_SAMPLE_FMT_FLTP,float類型 32位,而我用的opensl播放,只支持int類型的樣本,所以需要轉(zhuǎn)換成32位的int類型 ,即AVSampleFormat::AV_SAMPLE_FMT_S32。

代碼如下

int YaoAVFrame::getAudioPackedData(unsigned char * data){
    //每個(gè)樣本的字節(jié)數(shù) * 每個(gè)聲道的樣本數(shù) * 聲道數(shù)
    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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容