2012-11-02 102 views
1

我正在爲我的音頻項目使用alexoca的Novocaine NovocaineNovocaine - 如何循環播放文件? (iOS)

我正在玩這裏的示例代碼來閱讀文件。 該文件播放沒有問題。我想循環這個錄音與循環之間的差距 - 任何建議我怎麼能這樣做?

謝謝。

碼頭。

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

}]; 

回答

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; 
} 
0

,你還可以修改這樣的AudioFileReader.mm:

- (void)bufferNewAudio 

這將導致:

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

中發現的在循環文件讀取器中,無需重新編譯它。

壞處是,你必須修改框架文件之一...

希望它能幫助!