最も洗練されたソリューションではないかもしれませんが、ここで共通のアイデアを見つけることができます。これは、プロジェクトにコピー/貼り付けできる完全に機能するコードであり、機能するはずです。もちろん、ファイル名を変更する必要があります。問題がある場合はお知らせください。
#import "MainViewController.h"
#import <AVFoundation/AVFoundation.h>
@interface MainViewController () <AVAudioPlayerDelegate>
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (nonatomic, strong) NSMutableArray *soundsPathsList;
@property (nonatomic, assign) NSInteger curSoundIdx;
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSArray *soundList = [[NSArray alloc] initWithObjects:@"myAudioFile 1", @"myAudioFile 2", @"myAudioFile 3",
@"myAudioFile 4", @"myAudioFile 5", @"myAudioFile 6", @"myAudioFile 7",
@"myAudioFile 8", @"myAudioFile 9", @"myAudioFile 10", nil];
self.soundsPathsList = [NSMutableArray new];
for (NSString *fileName in soundList) {
NSString *pathToFile = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:pathToFile];
[self.soundsPathsList addObject:url];
}
[self playNext];
}
return self;
}
- (void) playNext {
if (self.curSoundIdx >= [self.soundsPathsList count]) {
self.curSoundIdx = 0;
}
// destroy player object
[self.audioPlayer stop];
self.audioPlayer = nil;
// create new player object
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:
[self.soundsPathsList objectAtIndex:self.curSoundIdx] error:nil];
self.audioPlayer.delegate = self;
[self.audioPlayer play];
self.curSoundIdx++;
}
#pragma mark - AVAudioPlayerDelegate
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag {
[self playNext];
}
@end