私はAVAudioPlayerフレームワークを使用していますが、私は一度に1つずつ再生するいくつかのサウンドを持っています。サウンドの再生が終了すると、アプリケーションは何かをしたい。私はaudioPlayerDidFinishPlayingを使って最初のサウンドの最後にアクションを実行しようとしましたが、再定義エラーが発生したため、2番目のサウンドには使用できませんでした。 NSTimeIntervalを使用してサウンドの持続時間を取得できますか?サウンドの再生が終了するとAVAudioPlayerでサウンドの再生が終了したときにアクションを実行しますか?
// ViewController.h
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVAudioPlayer.h>
@interface ViewController : UIViewController {
UIButton *begin;
UIWindow *firstTestWindow;
UIWindow *secondTestWindow;
AVAudioPlayer *player;
NSTimeInterval duration;
}
@property (nonatomic, retain) IBOutlet UIButton *begin;
@property (nonatomic, retain) IBOutlet UIWindow *firstTestWindow;
@property (nonatomic, retain) IBOutlet UIWindow *secondTestWindow;
@property (nonatomic, retain) AVAudioPlayer *player;
@property (readonly) NSTimeInterval duration;
- (IBAction)begin:(id)sender;
- (IBAction)doTapScreen:(id)sender;
@end
//ViewController.m
#import "ViewController.h"
@implementation ViewController
@synthesize begin, player, firstTestWindow, secondTestWindow;
//first sound
- (IBAction)begin:(id)sender; {
[firstTestWindow makeKeyAndVisible];
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: @"Tone1"
ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
[fileURL release];
self.player = newPlayer;
[newPlayer release];
[player prepareToPlay];
[self.player play];
}
//If user does not do anything by the end of the sound go to secondWindow
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
successfully: (BOOL) flag {
if (flag==YES) {
[secondWindow makeKeyAndVisible];
}
}
//second sound
- (IBAction)doTapScreen:(id)sender {
//When user touches the screen, either play the sound or go to the next window
if (self.player.playing) {
[self.player stop];
[thirdWindow makeKeyAndVisible];
}
else {
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: @"Tone2"
ofType: @"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: nil];
[fileURL release];
self.player = newPlayer;
[newPlayer release];
[player prepareToPlay];
[self.player play];
}
}
ああ!ええ、私は、applicationDidFinishLaunchingではなく、audioPlayerDidFinishPlayingと言っていました。ごめんなさい。 私はデリゲートをselfに設定しました。それは最初のサウンドで機能しました。しかし、問題は、audioPlayerDidFinishPlayingが動作しないということではなく、問題はもう2番目のサウンドのためにそれをやり直す方法を知らないことです。私は2つの異なる音を持っています。 – Evelyn
ちなみに、[player setDelegate:self]を入れると、警告が表示されます。「class 'ViewController'は 'AVAudioPlayerDelegate'プロトコルを実装していません。問題ありますか? – Evelyn
自分自身を両方のサウンドの代理人として設定する必要があります。デリゲートメッセージを送信すると、そのサウンド自体が識別されるため、手元にあるサウンドと比較することができます。 –