PCM实时音频流怎样播放?
我现在有ffmpeg解码出来的pcm实时音频数据,但是不知道应该怎样播放,请教一下怎么用audioqueue播实时音频流呢?想把ffmpeg解码出来的PCM包直接扔给AudioQueue进行播放    int VoiceErrorCode = 0;
    av_register_all();  //注册所有可解码类型
    AVFormatContext *pInFmtCtx=NULL;    //文件格式
    AVCodecContext *pInCodecCtx=NULL;   //编码格式
    if (av_open_input_file(&pInFmtCtx, [voicePath cStringUsingEncoding:NSASCIIStringEncoding], NULL, 0, NULL)!=0) //获取文件格式
        printf("av_open_input_file error\n");
    if (av_find_stream_info(pInFmtCtx) < 0)  //获取文件内音视频流的信息
        printf("av_find_stream_info error\n");
    unsigned int j;
    // Find the first audio stream
    int audioStream = -1;
    for (j=0; j<pInFmtCtx->nb_streams; j++)   //找到音频对应的stream
    {
        if (pInFmtCtx->streams[j]->codec->codec_type == AVMEDIA_TYPE_AUDIO)
        {
            audioStream = j;
            break;
        }
    }
    if (audioStream == -1)
    {
        printf("input file has no audio stream\n");
        return VoiceErrorCode; // Didn't find a audio stream
    }
    
    printf("audio stream num: %d\n",audioStream);
    pInCodecCtx = pInFmtCtx->streams[audioStream]->codec; //音频的编码上下文
    AVCodec *pInCodec = NULL;
    
    pInCodec = avcodec_find_decoder(pInCodecCtx->codec_id); //根据编码ID找到用于解码的结构体
    if (pInCodec == NULL)
    {
        printf("error no Codec found\n");
        VoiceErrorCode =-1 ; // Codec not found
    }
    
    if(avcodec_open(pInCodecCtx, pInCodec)<0)//将两者结合以便在下面的解码函数中调用pInCodec中的对应解码函数
    {
        printf("error avcodec_open failed.\n");
        VoiceErrorCode= -1; // Could not open codec
    }
    static AVPacket Packet;
    printf(" bit_rate = %d \r\n", pInCodecCtx->bit_rate);
    printf(" sample_rate = %d \r\n", pInCodecCtx->sample_rate);
    printf(" channels = %d \r\n", pInCodecCtx->channels);
    printf(" code_name = %s \r\n", pInCodecCtx->codec->name);
    printf(" block_align = %d\n",pInCodecCtx->block_align);

解决方案 »

  1.   

     [voicePath cStringUsingEncoding:NSASCIIStringEncoding]是rtmp得地址
    打印出来得信息是
     bit_rate = 48000 
     sample_rate = 22050 
     channels = 1 
     code_name = mp3 
     block_align = 0
      

  2.   

    创建缓冲区并绑定给audioqueue,使用start/pause等函数控制播放。主要的工作应该是围绕缓冲区展开,比如填充数据,计算缓冲进度,等待填充等。从解码到播放,这里面有一套交互逻辑要写(比如解码一帧后通知播放器,播放器填充缓冲区,决定是否start等)。
    解码到的pcm,如果放在内存,要考虑到如果解码线程一直在跑而播放可能暂停了,那pcm数据会占很大的内存,需要控制。如果pcm数据写到文件,会方便控制,只是在运行时占用的磁盘空间大一点(启动程序时,可以先删除掉文件)。
      

  3.   

    请问怎么把数据填入缓冲区,函数AudioFileReadPackets 只能从文件中把数据读入缓冲区,有没有其他方法把data 填入 audioQueueBuffer
      

  4.   

    AudioQueueEnqueueBuffer 推进缓冲区