AVAssetReader 和音频队列流问题

发布于 2024-10-13 11:44:52 字数 1974 浏览 4 评论 0 原文

我在使用 AVAssetReader 时遇到问题,无法从 iPod 库获取样本并通过音频队列进行流式传输。我无法找到任何这样的示例,因此我尝试实现自己的示例,但似乎 AssetReader 在音频队列的回调函数处“搞砸了”。具体来说,当它执行 copyNextSampleBuffer 时,它会失败,即,当它尚未完成时,它会返回 null。我已经确保指针存在,因此如果有人可以提供帮助,那就太好了。

下面是我使用过的回调函数代码。当 AudioQueue 回调未调用此回调函数时,该函数会“起作用”。

    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 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.

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
        );


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦毁影碎の 2024-10-20 11:44:52

我也遇到了同样的神秘错误。果然,“设置”音频会话使错误消失了。这就是我设置音频会话的方式。

- (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);
}

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);
}
奢欲 2024-10-20 11:44:52

来自音频会话编程指南,在 AVAudioSessionCategoryAmbient 下:

此类别允许在您的应用程序播放音频时播放来自 iPod、Safari 和其他内置应用程序的音频。

使用 AVAssetReader 可能会使用 iOS 的硬件解码器,这会阻止 AudioQueue 的使用。设置 AVAudioSessionCategoryAmbient 意味着音频在软件中呈现,允许两者同时工作 - 但是,这会对性能/电池寿命产生影响。 (请参阅音频会话编程指南下的“类别如何影响编码和解码”)。

From the Audio Session Programming Guide, under AVAudioSessionCategoryAmbient:

This category allows audio from the iPod, Safari, and other built-in applications to play while your application is playing audio.

Using an AVAssetReader probably uses iOS' hardware decoder, which blocks the use of the AudioQueue. Setting AVAudioSessionCategoryAmbient means the audio is rendered in software, allowing both to work at the same time - however, this would have an impact on performance/battery life. (See Audio Session Programming Guide under "How Categories Affect Encoding and Decoding").

盗梦空间 2024-10-20 11:44:52

好吧,我已经以某种方式解决了这个奇怪的错误...显然这是因为音频会话设置不正确。谈论缺乏这方面的文档......

Ok I have somehow solved this weird error... Apparently it is because of the audio session not properly set. Talk about lack of documentation on this one...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文