Novocaine - 如何循环播放文件? (IOS)

时间:2012-11-02 04:19:39

标签: ios core-audio

我正在使用alexbw Novocaine的Novocaine作为我的音频项目。

我正在玩这里的示例代码进行文件读取。 该文件播放没有问题。我想循环这个记录与循环之间的差距 - 任何建议我怎么能这样做?

感谢。

码头。

// AUDIO FILE READING OHHH YEAHHHH
// ========================================

NSArray *pathComponents = [NSArray arrayWithObjects:
                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                           @"testrecording.wav",
                           nil];
NSURL *inputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
NSLog(@"URL: %@", inputFileURL);

fileReader = [[AudioFileReader alloc]
              initWithAudioFileURL:inputFileURL
              samplingRate:audioManager.samplingRate
              numChannels:audioManager.numOutputChannels];

[fileReader play];
[fileReader setCurrentTime:0.0]; 
//float duration = fileReader.getDuration;

[audioManager setOutputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels)
 {
     [fileReader retrieveFreshAudio:data numFrames:numFrames numChannels:numChannels];         
     NSLog(@"Time: %f", [fileReader getCurrentTime]);

 }];

2 个答案:

答案 0 :(得分:1)

这对我有用:

[audioManager setOutputBlock:^(float *data, UInt32 numFrames, UInt32 numChannels)
 {
     if( ![fileReader playing] ){

//      [fileReader release]; // for some reason this is causing an error, but I assume if you don´t do it, it will eventually cause a memory problem
//      fileReader = nil;

        [self playSound];
     } else {
         [fileReader retrieveFreshAudio:data numFrames:numFrames numChannels:numChannels];         
         NSLog(@"Time: %f", [fileReader getCurrentTime]);
    }
 }];

- (void) playSound
{
    NSArray *pathComponents = [NSArray arrayWithObjects:
                           [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
                           @"testrecording.wav",
                           nil];
    NSURL *inputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
    NSLog(@"URL: %@", inputFileURL);

    fileReader = [[AudioFileReader alloc]
                  initWithAudioFileURL:inputFileURL
                  samplingRate:audioManager.samplingRate
                  numChannels:audioManager.numOutputChannels];

    [fileReader play];
    [fileReader setCurrentTime:0.0]; 
    //float duration = fileReader.getDuration;
}

答案 1 :(得分:0)

您也可以像这样修改AudioFileReader.mm:

if ((self.currentFileTime - self.duration) < 0.01 && framesRead == 0) {
    [self setCurrentTime:0.0];
}

发现于:

- (void)bufferNewAudio

这将导致循环文件阅读器,而无需重新启动它。

缺点是,您必须修改其中一个框架文件......

希望它有所帮助!