Vaibhav Jhaveri
Vaibhav Jhaveri

Reputation: 1605

Play/Pause multiple audio files in AVAudioPlayer

I am working with AVAudioPlayer. I want to play a Sequence of audio files back to back when I tap on play button. Also, when I again Tap on play button, the sequence playing should pause and then again resume when button is tapped

Following is my code :

NSTimeInterval totalTime, currentTime;
isPaused = NO;
isFirst = YES;

-(void)btnPlay:(id)sender
{
    if (isFirst)
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSMutableArray *audioList = [defaults objectForKey:@"AudioArray"];
        [defaults synchronize];

        NSLog(@"Audio to be Played : %@", audioList);

        for(int i = 0; i < [audioList count]; i++)
        {
            NSLog(@"Current Audio : %@", [audioList objectAtIndex:i]);

            NSString *path = [[NSBundle mainBundle] pathForResource:@"Alarm_1" ofType:@"mp3"];
            NSError *error = nil;
            NSURL *url = [NSURL fileURLWithPath:path];
            [audioPlayer setDelegate:self];
            audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];

            [audioPlayer play];
            isFirst = NO;

            totalTime = audioPlayer.duration;
            NSLog(@"Total Time : %f", totalTime);
        }
    }
    else if (!isFirst)
    {
        if (isPaused)
        {
            [audioPlayer setCurrentTime:currentTime];
            [audioPlayer play];
            isPaused = NO;
        }
        else if (!isPaused)
        {
            currentTime = audioPlayer.currentTime;

            [audioPlayer pause];
            isPaused = YES;
        }
    }
}

Upvotes: 0

Views: 630

Answers (1)

Vaibhav Jhaveri
Vaibhav Jhaveri

Reputation: 1605

isPlaying = NO;
if (isFirst)
{
    defaults = [NSUserDefaults standardUserDefaults];
    audioList = [defaults objectForKey:@"AudioArray"];
    [defaults synchronize];

    if ([audioList count] != 0)
    {
        NSLog(@"Audio to be Played : %@", audioList);

        NSLog(@"Current Audio : %@", audioList[currentID]);

        NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", audioList[currentID]] ofType:@"wav"];
        NSError *error = nil;
        NSURL *url = [NSURL fileURLWithPath:path];
        audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        [audioPlayer setDelegate:self];

        [audioPlayer setVolume:currentVolume];
        [audioPlayer play];
        [btnPlay setImage:[UIImage imageNamed:@"CameraViewPauseBlack"] forState:UIControlStateNormal];
        isFirst = NO;
        isPlaying = YES;

        totalTime = audioPlayer.duration;
        currentID = currentID + 1;
    }
    else
    [MNMToast showWithText:@"Please select an Audio file to Play" autoHidding:YES priority:MNMToastPriorityHigh completionHandler:nil tapHandler:nil];

}
else if (!isFirst)
{
    if (!isPlaying)
    {
        [audioPlayer setCurrentTime:currentTime];
        [audioPlayer setVolume:currentVolume];
        [audioPlayer play];
        [btnPlay setImage:[UIImage imageNamed:@"CameraViewPauseBlack"] forState:UIControlStateNormal];
        isPlaying = YES;
    }
    else if (isPlaying)
    {
        currentTime = audioPlayer.currentTime;

        [audioPlayer pause];
        [btnPlay setImage:[UIImage imageNamed:@"CameraViewPlayBlack"] forState:UIControlStateNormal];
        isPlaying = NO;
    }
}

Upvotes: 0

Related Questions