在AVAudioPlayer中播放/暂停多个音频文件

时间:2014-11-14 06:23:46

标签: ios avaudioplayer avplayer

我正在使用AVAudioPlayer。我想点击播放按钮时,我想连续播放一系列音频文件。此外,当我再次点击播放按钮时,序列播放应该暂停,然后在点击按钮时再次恢复

以下是我的代码:

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

1 个答案:

答案 0 :(得分:0)

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