代码之家  ›  专栏  ›  技术社区  ›  Chowlett

iPhone OS 4.0:NSFileHandleDataAvailableNotification在文件结尾不提供回调

  •  1
  • Chowlett  · 技术社区  · 15 年前

    我对iPhone的开发有点陌生,所以要温柔一点!我支持一个应用程序,它从URL文件流加载wav文件,并通过音频队列播放。

    我们在另一个线程中运行一个连续循环,如果检测到队列没有使用缓冲区,并且输入文件流已到达其末端,则停止该队列。反过来,我们检测文件流是否在 waitForDataInBackgroundAndNotify 上的回调 NSFileHandleDataAvailableNotification 对于流,通过检查 availableData 长度为0。

    这在iOS 3.0下工作-我们在文件末尾收到0个可用数据的通知-但是在iOS 4.0上,我们似乎没有在文件结尾收到回调。这在OS4.0设备上发生,与目标OS版本无关。

    希望相关代码:

    数据可用回调:

    - (void)readFileData:(NSNotification *)notification
    {
      @try
      {
        NSData *data = [[notification object] availableData];
    
        if ([data length] == 0 && self.audioQueueState != AQS_END)
        {
          /***********************************************************************/
          /* We've hit the end of the data but it's possible that more may be    */
          /* appended to the file (if we're still downloading it) so we need to  */
          /* wait for the availability of more data.                             */
          /***********************************************************************/
          [self setFileStreamerState:FSS_END];
          [[notification object] waitForDataInBackgroundAndNotify];
        }
        else if (self.audioQueueState == AQS_END)
        {
          TRC_DBG(@"ignore read data as ending");
        }
        else
        {
          NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
          TRC_DBG(@"Read %d bytes", [data length]);
    
          [self setFileStreamerState:FSS_DATA];
    
          if (discontinuous)
          {
            TRC_DBG(@"AudioFileStreamParseBytes %d bytes, discontinuous", [data length]);
            err = AudioFileStreamParseBytes(audioFileStream, [data length], [data bytes], kAudioFileStreamParseFlag_Discontinuity);
            discontinuous = NO;
          }
          else
          {
            TRC_DBG(@"AudioFileStreamParseBytes %d bytes, continuous", [data length]);
            err = AudioFileStreamParseBytes(audioFileStream, [data length], [data bytes], 0);
          }
    
          /***********************************************************************/
          /* If error then get out, otherwise wait again for more data.          */
          /***********************************************************************/
          if (err != 0)
          {
            [self failWithErrorCode:AS_FILE_STREAM_PARSE_BYTES_FAILED];
          }
          else
          {
            [[notification object] waitForDataInBackgroundAndNotify];
          }
    
          [pool release];
        }
      }
      @catch (NSException *exception)
      {
        TRC_ERR(@"Exception: %@", exception);
        TRC_ERR(@"Exception reason: %@", [exception reason]);
        //[self failWithErrorCode:AS_FILE_AVAILABLE_DATA_FAILED];
      }
    }
    
    0 回复  |  直到 15 年前