Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Stop Audio Playback At Certain Time (AVAudioPlayer End Time)

I'm wanting to play just part of an audio file. This audio file contains 232 spoken words, I have a dictionary that contains the start time of each word. So I have the start and stop times I just can't find a way to stop at a given time or play the file for a certain duration. Any advice and/or sample code to help me would be much appreciated, thanks Jason.

So I've found a solution, there's a problem with how I get endTime but I sure I can fix it.

//Prepare to play
[audioPlayer prepareToPlay];

//Get current time from the array using selected word
audioPlayer.currentTime = [[wordsDict objectForKey:selectedWord] floatValue]; 

//Find end time  - this is a little messy for now
int currentIndex = 0;
int count = 0;

for (NSString* item in wordsKeys) {
    if ([item isEqualToString: selectedWord]) {
        currentIndex = count+1;
    }
    count++;
}

//Store found end time
endTime  = [[wordsDict objectForKey:[wordsKeys objectAtIndex:currentIndex]] floatValue];

//Start Timer
NSTimer * myAudioTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
                                                             target:self
                                                           selector:@selector(checkCurrentTime)
                                                           userInfo:nil
                                                            repeats:YES]
//Now play audio
[audioPlayer play];



//Stop at endTime
- (void) checkCurrentTime {
if(audioPlayer.playing && audioPlayer.currentTime >= endTime)
    [audioPlayer stop];
}
like image 291
dciso Avatar asked Aug 18 '11 10:08

dciso


2 Answers

This should do what you want. you don't need a repeating timer that's thrashing the player.

NSString *myAudioFileInBundle = @"words.mp3";
NSTimeInterval wordStartsAt = 1.8;
NSTimeInterval wordEndsAt = 6.5;
NSString *filePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:myAudioFileInBundle];
NSURL *myFileURL = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:myFileURL error:nil];
if (myPlayer) 
{
    [myPlayer setCurrentTime:wordStartsAt];
    [myPlayer prepareToPlay];
    [myPlayer play];
    [NSTimer scheduledTimerWithTimeInterval:wordEndsAt-wordStartsAt target:myPlayer.autorelease selector:@selector(stop) userInfo:nil repeats:NO];

}
like image 128
unsynchronized Avatar answered Nov 05 '22 11:11

unsynchronized


Try using AVPlayer instead and make use of

addBoundaryTimeObserverForTimes:queue:usingBlock:

What it does: Requests invocation of a block when specified times are traversed during normal playback.

https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVPlayer_Class/index.html#//apple_ref/occ/instm/AVPlayer/addBoundaryTimeObserverForTimes:queue:usingBlock:

like image 5
Pnar Sbi Wer Avatar answered Nov 05 '22 11:11

Pnar Sbi Wer