AVAudioPlayer - játék több audió fájlokat, hogy sorrendben

szavazat
5

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

A kérdést 07/03/2009 03:29
a forrás felhasználó
Más nyelveken...                            


5 válasz

szavazat
10

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.

Válaszolt 23/08/2012 12:39
a forrás felhasználó

szavazat
3

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.

Válaszolt 09/04/2012 21:21
a forrás felhasználó

szavazat
3

Használja AVAudioPlayer per hangot.

Válaszolt 02/08/2009 16:51
a forrás felhasználó

szavazat
1

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.

Válaszolt 29/04/2016 09:35
a forrás felhasználó

szavazat
1

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
Válaszolt 20/07/2011 06:58
a forrás felhasználó

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