AVAudioPlayer – 按顺序播放多个audio文件

我想用AVAudioPlayer按顺序播放多个MP3文件。 我试了一下,然后在播放第一首MP3后停下来。 但是,如果我进入debugging器,它工作正常..任何想法? 我在某处读AVAudioPlayer在后台播放audio..我如何防止它做到这一点? 沃什

我认为AVQueuePlayerAVPlayer子类)完成这个工作(播放一系列的项目),因为iOS 4.1: http : //developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVQueuePlayer_Class/Reference/Reference html的

但我没有尝试过,但肯定会尝试一下。

每个声音使用一个AVAudioPlayer。

那么,你的代码示例并不适合我。 所以,我想我会用一个固定的版本回应:

Looper.h:

 #import <Foundation/Foundation.h> #import <AVFoundation/AVFoundation.h> @interface Looper : NSObject <AVAudioPlayerDelegate> { AVAudioPlayer* player; NSArray* fileNameQueue; int index; } @property (nonatomic, retain) AVAudioPlayer* player; @property (nonatomic, retain) NSArray* fileNameQueue; - (id)initWithFileNameQueue:(NSArray*)queue; - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag; - (void)play:(int)i; - (void)stop; @end 

Looper.m:

 #import "Looper.h" @implementation Looper @synthesize player, fileNameQueue; - (id)initWithFileNameQueue:(NSArray*)queue { if ((self = [super init])) { self.fileNameQueue = queue; index = 0; [self play:index]; } return self; } - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { if (index < fileNameQueue.count) { [self play:index]; } else { //reached end of queue } } - (void)play:(int)i { self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[fileNameQueue objectAtIndex:i] ofType:nil]] error:nil]; [player release]; player.delegate = self; [player prepareToPlay]; [player play]; index++; } - (void)stop { if (self.player.playing) [player stop]; } - (void)dealloc { self.fileNameQueue = nil; self.player = nil; [super dealloc]; } @end 

以下是我将如何称呼它:

  Looper * looper = [[Looper alloc] initWithFileNameQueue:[NSArray arrayWithObjects: audioFile, audioFile2, nil ]]; 

我只使用Objective-C的iPhone / iPad开发有一年多的经验,所以随时可以给予批评。

我已经实现了一个类来处理这个问题。

要使用只是做这样的事情:

 [looper playAudioFiles:[NSArray arrayWithObjects: @"add.mp3", [NSString stringWithFormat:@"%d.mp3", numeral1.tag], @"and.mp3", [NSString stringWithFormat:@"%d.mp3", numeral2.tag], nil ]]; 

Looper.m

 #import "Looper.h" @implementation Looper @synthesize player, fileNameQueue; - (id)initWithFileNameQueue:(NSArray*)queue { if ((self = [super init])) { self.fileNameQueue = queue; index = 0; [self play:index]; } return self; } - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { if (index < fileNameQueue.count) { [self play:index]; } else { //reached end of queue } } - (void)play:(int)i { self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] pathForResource:[fileNameQueue objectAtIndex:i] ofType:nil]] error:nil]; [player release]; player.delegate = self; [player prepareToPlay]; [player play]; index++; } - (void)stop { if (self.player.playing) [player stop]; } - (void)dealloc { self.fileNameQueue = nil; self.player = nil; [super dealloc]; } @end 

Looper.h

 #import <Foundation/Foundation.h> @interface Looper : NSObject <AVAudioPlayerDelegate> { AVAudioPlayer* player; NSArray* fileNameQueue; int index; } @property (nonatomic, retain) AVAudioPlayer* player; @property (nonatomic, retain) NSArray* fileNameQueue; - (id)initWithFileNameQueue:(NSArray*)queue; - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag; - (void)play:(int)i; - (void)stop; @end 

初始化,准备项目并提前排队是一个好主意,例如在viewDidLoad方法上。

如果你正在使用Swift,

  override func viewDidLoad() { super.viewDidLoad() let item0 = AVPlayerItem.init(URL: NSBundle.mainBundle().URLForResource("url", withExtension: "wav")!) let item1 = AVPlayerItem.init(URL: NSBundle.mainBundle().URLForResource("dog", withExtension: "aifc")!) let item2 = AVPlayerItem.init(URL: NSBundle.mainBundle().URLForResource("GreatJob", withExtension: "wav")!) let itemsToPlay:[AVPlayerItem] = [item0, item1, item2] queuePlayer = AVQueuePlayer.init(items: itemsToPlay) } 

然后当事件发生时,

  queuePlayer.play() 

请注意,如果您使用队列,则声音之间可能还有一些间隙。

你可以在问题中findObjective-C版本, 当AVQueuePlayer完成最后一个播放器项目的时候如何做一些事情

希望能帮助到你。