我正在使用 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
#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];
}
}
当声音播放完毕后,我希望应用程序执行一些操作。
然后将自己设置为代表并回复audioPlayerDidFinishPlaying:successfully:
.
在您显示的代码片段中,您已完成步骤 2,但未完成步骤 1:您没有将自己设置为委托人。
我尝试使用applicationDidFinishLaunching
在第一个声音结束时执行动作...
脑放屁?我认为该方法与播放声音无关。
…,但我无法将其用于第二个声音,因为我遇到了重新定义错误。
啊?您是否实现了该方法两次或其他内容,而不是仅比较方法主体中的玩家对象?
如果您显示收到的确切错误消息,将会有所帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)