Chci hrát více MP3 souborů, v pořadí (jeden po druhém), pomocí AVAudioPlayer. Zkoušel jsem to, a to se zastaví po přehrání první MP3. Nicméně, když půjdu do ladicího, to funguje dobře .. nějaké nápady? Někde jsem četl, AVAudioPlayer hraje zvuk na pozadí .. Jak mohu zabránit tomu, aby se dělá? Vas
AVAudioPlayer - hrát více zvukových souborů, v pořadí
Myslím, že AVQueuePlayer(podtřída AVPlayer) dělá přesně tuto práci (hrát pořadí položek) od iOS 4.1:
http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVQueuePlayer_Class/Reference/Reference.html
Nesnažil jsem se to sám však ale určitě zkusit to.
No, váš kód příklad nefungoval po vybalení z krabice pro mě. Soooo, myslel jsem si, že reagují s pevnou verzi:
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
A tady je, jak bych to nazval:
Looper * looper = [[Looper alloc] initWithFileNameQueue:[NSArray arrayWithObjects: audioFile, audioFile2, nil ]];
Mám jen o něco více než jeden rok zkušeností s vývojem iPhone / iPad pomocí Objective-C, takže neváhejte a reagovat s dalšími kritiky.
Použít jeden AVAudioPlayer za zvukem.
Je to dobrý nápad, aby se inicializovat, připravit položky, a fronty dopředu, například na metodě viewDidLoad.
Pokud pracujete na 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)
}
a pak, když dojde k nějaké události,
queuePlayer.play()
Všimněte si, že pokud používáte frontu, stále můžete mít nějaké mezery mezi zvuky.
Můžete najít verzi Objective-C v otázce , jak to udělat něco, když AVQueuePlayer dokončí poslední playeritem
Doufám, že to pomůže.
Jsem implementoval třídu zvládnout.
Chcete-li používat jen udělat něco takového:
[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













