lppier
lppier

Reputation: 1987

Novocaine - How to loop file playback? (iOS)

I'm using Novocaine by alexbw Novocaine for my audio project.

I'm playing around with the example code here for file reading. The file plays back with no problem. I would like to loop this recording with the gap between the loops - any suggestion as to how I can do so?

Thanks.

Pier.

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

 }];

Upvotes: 1

Views: 1446

Answers (2)

Maximilian Körner
Maximilian Körner

Reputation: 894

you can also modify the AudioFileReader.mm like this:

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

found in:

- (void)bufferNewAudio

This will result in a looping filereader without the need to re-init it.

Downside is, that you have to modify one of the frameworks files...

Hope it helps!

Upvotes: 0

Badjano
Badjano

Reputation: 416

This worked for me:

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

Upvotes: 1

Related Questions