Szeretnék játszani több MP3 fájlok, sorrendben (az egyik a másik után), a AVAudioPlayer. Kipróbáltam, és megáll a játék után az első MP3. Azonban, ha bemegy debugger, ez jól működik .. valami ötleted? Olvastam valahol AVAudioPlayer játszik hangot a háttérben .. hogyan tudom megakadályozni ebben? Vas
AVAudioPlayer - játék több audió fájlokat, hogy sorrendben
Azt hiszem, a AVQueuePlayer(alosztálya AVPlayer) pontosan ezt a munkát (játszani sorozata példány), mivel iOS 4.1:
http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVQueuePlayer_Class/Reference/Reference.html
Nem próbálja meg magam azonban de biztosan ad egy esélyt, hogy azt.
Nos, a kód például nem működött a dobozból nekem. Soooo, gondoltam válaszolni fix 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
És itt van, hogyan hívnám:
Looper * looper = [[Looper alloc] initWithFileNameQueue:[NSArray arrayWithObjects: audioFile, audioFile2, nil ]];
Már csak alig több mint egy év tapasztalat az iPhone / iPad fejlesztés Objective-C, így nyugodtan válaszul további kritika.
Használja AVAudioPlayer per hangot.
Ez egy jó ötlet, hogy inicializálja, előkészíti a terméket, és sorban idő előtt, mint például a viewDidLoad módszer.
Ha dolgozik a 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)
}
majd amikor egy esemény bekövetkezik,
queuePlayer.play()
Figyeljük meg, hogy ha használja a sorba, azt továbbra is vannak hiányosságok a hangok között.
Megtalálható az Objective-C változata a kérdést hogyan kell csinálni valamit, ha AVQueuePlayer befejezi az utolsó playeritem
Remélem ez segít.
Már végre egy osztály, hogy kezelni ezt.
Ahhoz, hogy használni csak nem valami ilyesmi:
[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













