AVAssetReader和音频队列流媒体问题 [英] AVAssetReader and Audio Queue streaming problem

查看:106
本文介绍了AVAssetReader和音频队列流媒体问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里的AVAssetReader遇到问题,无法从iPod库中获取样本并通过音频队列将其流式传输。我找不到任何这样的示例,所以我尝试实现自己的示例,但似乎以某种方式在音频队列的回调函数处拧紧了AssetReader。具体来说,它在执行copyNextSampleBuffer时失败,即在尚未完成时返回null。我已经确定了指针的存在,因此,如果有人可以提供帮助,它将非常有用。

I have a problem with the AVAssetReader here to get samples from the iPod library and stream it via Audio Queue. I have not been able to find any such example so I try to implement my own but it seems that somehow the AssetReader is "screwed up" at the callback function of audio queue. Specifically it fails when it does the copyNextSampleBuffer ie it returns null when it is not finished yet. I have made sure the pointer exists and such so it will be great if anyone can help.

下面是我使用的回调函数代码。当AudioQueue回调未调用此回调函数时,该函数有效。

Below is the callback function code that I have used. This callback function 'works' when it is not called by the AudioQueue callback.

    static void HandleOutputBuffer (
        void                *playerStateH,
        AudioQueueRef       inAQ,
        AudioQueueBufferRef inBuffer
        ) {

 AQPlayerState *pplayerState = (AQPlayerState *) playerStateH;
 //if (pplayerState->mIsRunning == 0) return;    



 UInt32 bytesToRead = pplayerState->bufferByteSize;
 [[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_callsample object:nil];


 float * inData =(float *) inBuffer->mAudioData;
 int offsetSample = 0;
 //Loop until finish reading from the music data
 while (bytesToRead) {

  /*THIS IS THE PROBLEMATIC LINE*/
  CMSampleBufferRef sampBuffer = [pplayerState->assetWrapper getNextSampleBuffer]; //the assetreader getting nextsample with copyNextSampleBuffer

  if (sampBuffer == nil) {
   NSLog(@"No more data to read from");
 //  NSLog(@"aro status after null %d",[pplayerState->ar status]);
   AudioQueueStop (
       pplayerState->mQueue,
       false
       );
   pplayerState->mIsRunning = NO;

   return;
  }

  AudioBufferList  audioBufferList;
  CMBlockBufferRef blockBuffer;
  CMSampleBufferGetAudioBufferListWithRetainedBlockBuffer(sampBuffer, NULL, &audioBufferList, sizeof(audioBufferList), NULL, NULL, 0, &blockBuffer);
  AudioBuffer audioBuffer = audioBufferList.mBuffers[0];
  memcpy(inData + (2*offsetSample),audioBuffer.mData,audioBuffer.mDataByteSize);
  bytesToRead = bytesToRead - audioBuffer.mDataByteSize;
  offsetSample = offsetSample + audioBuffer.mDataByteSize/8;



 }
 inBuffer->mAudioDataByteSize = offsetSample*8;

 AudioQueueEnqueueBuffer ( 
        pplayerState->mQueue,
        inBuffer,
        0,
        0
        );


}


推荐答案

我也遇到了同样令人迷惑的错误。果然,设置音频会话可以使错误消失。这是我设置音频会话的方式。

I was getting this same mystifying error. Sure enough, "setting up" an audio session made the error go away. This is how I set up my audio session.

- (void)setupAudio {
    [[AVAudioSession sharedInstance] setDelegate:self];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
    NSError *activationError = nil;
    [[AVAudioSession sharedInstance] setActive: YES error:&activationError];

    NSLog(@"setupAudio ACTIVATION ERROR IS %@", activationError);
    [[AVAudioSession sharedInstance] setPreferredIOBufferDuration:0.1 error:&activationError];
    NSLog(@"setupAudio BUFFER DURATION ERROR IS %@", activationError);
}

这篇关于AVAssetReader和音频队列流媒体问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆