0

我有一个应用程序,它在启动时会播放介绍剪辑。以下代码在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptionsappDelegate.m 中,它工作得非常好。

NSLog(@"PLAY SOUND CLIP WHILE LOADING APP");
NSURL *clip = [[NSBundle mainBundle] URLForResource: @"intro" withExtension:@"caf"];
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
[self.startupPlayer play];

如果用户在介绍声音结束之前更改视图,它仍会继续播放。我已将此代码放在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions认为它会有所帮助但没有。

-(void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// Stop Sound
[self.startupPlayer stop];
}

我想也许如果我在加载请求之后放置一个 if 语句可能会奏效,但它没有奏效。参见示例:

NSLog(@"PLAY SOUND CLIP WHILE LOADING APP");
NSURL *clip = [[NSBundle mainBundle] URLForResource: @"intro" withExtension:@"caf"];
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
[self.startupPlayer play];

if ([viewDidDisappear == YES]) {
[self.startupPlayer stop];
}

如果用户在该剪辑完成播放之前更改视图,任何会停止介绍声音的建议都会很棒。哦,我已经成功地在应用程序的其他部分使用了“viewWillDisappear”,但在这种情况下,我选择了“viewDidDisappear”,因为后者也不起作用。所以我很难过。提前致谢。

编辑:所以我将 viewWillDisappear 移动到我的 MainViewController 中并调用了委托,但我仍然没有任何运气。再次,我们将不胜感激。

4

1 回答 1

0

我通过将 *startupPlayer 的 @property 声明并将其放在 AppDelegate.h 文件中而不是下面的方式解决了这个问题;

在 AppDelegate.m

@interface AppDelegate () 

@property (nonatomic, strong) AVAudioPlayer *startupPlayer;
@end

然后我仍然在 .m 文件中 @synthesized 它,如下所示,并保持 didFinishLaunchingWithOptions 相同:

@implementation AppDelegate 
@synthesize startupPlayer = _startupPlayer;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

//------ PLAY SOUND CLIP WHILE LOADING APP -----

NSLog(@"PLAY SOUND CLIP WHILE LOADING APP");
NSURL *clip = [[NSBundle mainBundle] URLForResource: @"intro" withExtension:@"caf"];
self.startupPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
[self.startupPlayer play]; }

然后在我的 MainViewController.m

#import "AppDelegate.h"

-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
//stop intro sound
AppDelegate *introClip = (AppDelegate *)[[UIApplication sharedApplication]delegate];
[[introClip startupPlayer]stop];}

Now even if the intro music is still playing through one cycle, the user can switch to another view and stop that music.

于 2012-10-18T17:43:03.140 回答