Tôi đang sử dụng khung AVAudioPlayer và tôi có một số âm thanh phát từng lần một. Khi một âm thanh kết thúc, tôi muốn ứng dụng làm điều gì đó. Tôi đã cố gắng sử dụng audioPlayerDidFinishPlaying để thực hiện tác vụ ở cuối âm thanh đầu tiên, nhưng tôi không thể sử dụng âm thanh thứ hai vì tôi gặp lỗi định nghĩa lại. Tôi có thể sử dụng NSTimeInterval để có được thời lượng của âm thanh không?Thực hiện hành động khi một âm thanh phát xong trong 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];
}
}
Oh! Vâng, tôi hoàn toàn muốn nói audioPlayerDidFinishPlaying, không phải applicationDidFinishLaunching. Lấy làm tiếc. Tôi đặt đại biểu cho bản thân, và nó làm việc cho âm thanh đầu tiên. Nhưng vấn đề không phải là âm thanhPlayerDidFinishPlaying không hoạt động, vấn đề là tôi không biết làm thế nào để làm điều đó một lần nữa cho một âm thanh thứ hai. Tôi có hai âm thanh khác nhau. – Evelyn
Nhân tiện, khi tôi đặt trong [player setDelegate: self], tôi nhận được cảnh báo: "class 'ViewController' không thực hiện giao thức 'AVAudioPlayerDelegate'." Đó có phải là vấn đề không? – Evelyn
Bạn cần đặt mình là đại biểu của cả hai âm thanh. Âm thanh sẽ tự xác định khi nó gửi cho bạn một tin nhắn đại biểu, vì vậy bạn có thể so sánh nó với âm thanh bạn có trong tay. –