AVAudioPlayer - 按顺序播放多个音频文件

时间:2009-03-07 02:29:57

标签: iphone ios audio avaudioplayer

我想使用AVAudioPlayer依次播放多个MP3文件(一个接一个)。我试了一下,播放完第一张MP3后就停止了。但是,如果我进入调试器,它工作正常..任何想法?我在某处读到AVAudioPlayer在后台播放音频..我该如何阻止它这样做? 输精管

5 个答案:

答案 0 :(得分:10)

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

我自己并没有尝试,但肯定会尝试一下。

答案 1 :(得分:4)

每个声音使用一个AVAudioPlayer。

答案 2 :(得分:3)

嗯,你的代码示例对我来说没有开箱即用。 Soooo,我想我会回复一个固定的版本:

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开发经验,所以请随时回复批评。

答案 3 :(得分:1)

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

使用这样的事情:

[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

答案 4 :(得分:1)

最好初始化,准备项目,并提前排队,例如在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()

请注意,如果您使用队列,则声音之间仍可能存在间隙。

您可以在问题How to do something when AVQueuePlayer finishes the last playeritem

中找到Objective-C版本

希望它有所帮助。