AVAudioPlayer - hrát více zvukových souborů, v pořadí

hlasů
5

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

Položena 07/03/2009 v 03:29
zdroj uživatelem
V jiných jazycích...                            


5 odpovědí

hlasů
10

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.

Odpovězeno 23/08/2012 v 12:39
zdroj uživatelem

hlasů
3

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.

Odpovězeno 09/04/2012 v 21:21
zdroj uživatelem

hlasů
3

Použít jeden AVAudioPlayer za zvukem.

Odpovězeno 02/08/2009 v 16:51
zdroj uživatelem

hlasů
1

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.

Odpovězeno 29/04/2016 v 09:35
zdroj uživatelem

hlasů
1

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
Odpovězeno 20/07/2011 v 06:58
zdroj uživatelem

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more