2009-03-07 22 views
5

AVAudioPlayerを使用して、複数のMP3ファイルを順番に(1つずつ順番に)再生したいです。私はそれを試して、それは最初のMP3を再生した後に停止します。しかし、私がデバッガに入ると、それはうまく動作します..任意のアイデア?私はAVAudioPlayerのどこかで、オーディオをバックグラウンドで再生しています。どうすればこのようにするのを防ぐことができますか? VasAVAudioPlayer - 複数のオーディオファイルを順番に再生する

+0

これはiPhoneSDKタグが必要です –

+0

あなたのコードを確認するのに役立ちます。見るべきこと - サウンドごとに1つのAVAudioPlayerを使用していますか? mp3ファイルを再生すると、一度に再生できるのは1つだけなので、オーバーラップすると期待どおりの結果が得られない場合があります。なぜあなたがシミュレータでも意味を持たない限り、デバッガが違いを生むのはなぜか分かりません。たぶん、シミュレータは複数のmp3ファイルを同時に再生することができます(これはiPhoneのmp3デコードハードウェアの結果なので)。さらに詳しい情報は、私たちが解決策を見つけるのを助けるので、somコードを投稿することを自由にしてください。 –

答えて

3

サウンドごとに1つのAVAudioPlayerを使用します。

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 
3

さて、あなたのコードの例の外に動作しませんでした。ちょうどこのような何かをを使用するには

私のための箱。

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開発でわずか1年以上の経験がありますので、気軽に対応してくださいティシズム。

私はiOSの4.1以降(項目のシーケンスを再生) AVQueuePlayerAVPlayerのサブクラス)は、まさにこの仕事をしないと思います
10

http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVQueuePlayer_Class/Reference/Reference.html

私はしかし、それを自分で試していないが、間違いなくそれを試してみます。

+0

この回答は、AVAudioPlayerの使用に代わるものですが、上記のLooper。*コードの例を使用するよりもはるかにエレガントで「組み込み」のソリューションです。 AVAudioPlayerの新しいインスタンスを継続的に割り当てる必要があります。当初はこのアプローチを取っていましたが、AVQueuePlayerをサブクラス化して、シーケンシャルオーディオファイルの再生に必要な機能を私に与えました。はい、これは正解です。 – manderson

1

たとえば、viewDidLoadメソッドなどで、アイテムを初期化して準備しておくことをお勧めします。

イベントが発生したとき、あなたはキューを使用している場合、あなたはまだ音の間にいくつかのギャップがあるかもしれないことに注意してくださいは

queuePlayer.play() 

、その後、スウィフトに取り組ん

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) 
    } 

とされている場合。あなたが質問にHow to do something when AVQueuePlayer finishes the last playeritem

をObjective-Cのバージョンを見つけることができます

はそれがお役に立てば幸いです。

関連する問題