2009-07-10 28 views
16

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]; 

} 
} 

Trả lời

24

Khi phát âm thanh xong, tôi muốn ứng dụng làm điều gì đó.

Sau đó, hãy đặt mình làm đại biểu và trả lời audioPlayerDidFinishPlaying:successfully:.

Trong đoạn mã bạn đã hiển thị, bạn đã thực hiện bước 2 nhưng không thực hiện bước 1: Bạn không tự đặt mình làm đại biểu.

Tôi cố gắng để sử dụng applicationDidFinishLaunching để làm các hành động ở phần cuối của âm thanh đầu tiên ...

Brain rắm? Phương pháp đó không có gì mà tôi có thể thấy để làm với âm thanh chơi.

…, nhưng tôi không thể sử dụng cho âm thanh thứ hai vì tôi gặp lỗi định nghĩa lại.

Huh? Bạn đã thực hiện phương thức hai lần hay gì đó thay vì chỉ so sánh đối tượng trình phát trong thân phương thức?

Sẽ hữu ích nếu bạn hiển thị thông báo lỗi chính xác mà bạn nhận được.

+0

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

+0

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

+0

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. –

1

Swift 3:

func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) { 
     print("sound file finished") 
    } 
Các vấn đề liên quan