当声音在AVAudioPlayer中播放完毕后执行操作? [英] Do an action when a sound has finished playing in AVAudioPlayer?

查看:114
本文介绍了当声音在AVAudioPlayer中播放完毕后执行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AVAudioPlayer框架,我有几个声音,一次玩一个。当一个声音完成播放,我想让应用程序做某事。我试图使用audioPlayerDidFinishPlaying来做第一个声音结束时的行动,但我不能使用第二个声音,因为我有一个重定义错误。我可以使用NSTimeInterval来获取声音的持续时间吗?

  // 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
#importViewController.h

@implementation ViewController

@synthesize begin,player,firstTestWindow,secondTestWindow;

//第一个声音
- (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];

}

//如果用户在声音结束时没有做任何事情,请转到secondWindow
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
successfully:(BOOL)flag {
if(flag == YES){
[secondWindow makeKeyAndVisible];
}
}

//第二声音
- (IBAction)doTapScreen:(id)sender {
//用户触摸屏幕时播放声音或转到下一个窗口
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];

}
}


解决方案


当声音播放完毕后,我希望应用程序执行某些操作。


自己作为委托并响应 audioPlayerDidFinishPlaying:successful:

$



在您显示的代码段中,完成步骤2但不是步骤1:您不是将自己设置为代理。


我试图使用 applicationDidFinishLaunching 在第一个声音结束时执行操作...



...但是我不能使用第二个声音,因为这个方法没有什么可以看到的。我有重新定义错误。


Huh?你是否实现了方法两次或一些东西,而不是只比较在方法体中的玩家对象?



这将有助于如果你显示确切的错误消息。 p>

I am using the AVAudioPlayer framework, and I have several sounds that play one at a time. When a sound is finished playing, I want the application to do something. I tried to use audioPlayerDidFinishPlaying to do the action at the end of the first sound, but I couldn't use that for the second sound because I got a redefinition error. Can I use NSTimeInterval to get the duration of a sound?

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

}
}

解决方案

When a sound is finished playing, I want the application to do something.

Then set yourself as the delegate and respond to audioPlayerDidFinishPlaying:successfully:.

In the code snippet you showed, you've done step 2 but not step 1: You're not setting yourself as the delegate.

I tried to use applicationDidFinishLaunching to do the action at the end of the first sound …

Brain fart? That method has nothing that I can see to do with playing sounds.

…, but I couldn't use that for the second sound because I got a redefinition error.

Huh? Did you implement the method twice or something instead of just comparing the player object in the method body?

It would help if you showed the exact error message you got.

这篇关于当声音在AVAudioPlayer中播放完毕后执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆