gpt4 book ai didi

iphone - 当声音在 AVAudioPlayer 中播放完毕时执行操作吗?

转载 作者:行者123 更新时间:2023-12-03 18:20:14 24 4
gpt4 key购买 nike

我正在使用 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];

}
}

最佳答案

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

然后将自己设置为委托(delegate)并响应 audioPlayerDidFinishPlaying:successively:

在您显示的代码片段中,您已完成步骤 2,但未完成步骤 1:您没有将自己设置为委托(delegate)人。

I tried to use applicationDidFinishLaunching 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.

嗯?您是否实现了该方法两次或其他内容,而不是仅比较方法主体中的玩家对象?

如果您显示收到的确切错误消息,将会有所帮助。

关于iphone - 当声音在 AVAudioPlayer 中播放完毕时执行操作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1111691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com