AVAudio 在呈现模态视图后不会停止
我有一个显示电影的应用程序。第一个屏幕显示呈现不同模式视图的按钮。这些模态视图内部是播放视频的按钮。
我希望主菜单播放音乐(足够简单),但我希望它在第二个模式视图中持续存在,但在视频开始播放时停止。
当按下电影按钮时,我尝试[audioPlayer stop],但音乐仍在播放。
我尝试停止并播放 viewDidAppear 和 viewDidDisappear 中的音频。这使得音频在呈现第二个视图时重新启动,而不是在视频期间播放。
我希望这是有道理的,我真的只需要知道如何让音频一直播放(在两个或三个模态视图中),除了电影播放期间。
这是我的vewDidAppear
方法:
-(void)viewDidAppear:(BOOL)animated {
NSString *musicPath = [[NSBundle mainBundle] pathForResource: @"Intro Music" ofType:@"mp3"];
if(musicPath) {
NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
[musicURL release];
}
if(!audioPlayer.playing) {
[audioPlayer play]
}
}
I have an application that displays movies. The first screen shows buttons that present different modal views. Inside those modal views are buttons that play videos.
I want the main menu to play music (easy enough), but I want it to persist through the second modal view but stop when the video starts playing.
I've trying [audioPlayer stop] when the buttons for the movies are pressed, but the music keeps playing.
I have tried stopping and playing the audio in viewDidAppear and viewDidDisappear. That has made the audio restart upon presenting the second view and not playing during the video.
I hope this makes sense, I really just need an idea of how to get the audio to play all of the time (throughout two or three modal views) except for during movie playback.
Here's my vewDidAppear
method:
-(void)viewDidAppear:(BOOL)animated {
NSString *musicPath = [[NSBundle mainBundle] pathForResource: @"Intro Music" ofType:@"mp3"];
if(musicPath) {
NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
[musicURL release];
}
if(!audioPlayer.playing) {
[audioPlayer play]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的模态视图控制器正在尝试停止内部不存在的
audioPlayer
。mainMenueViewController
有audioPlayer
,因此它必须是告诉音频播放器停止的那个。您可以为此使用 NSNotifications。每个 NSNotification 通过单例 NSNotificationCenter.以下是适合您情况的基本步骤:
希望这有帮助!
Your modal view controller is trying to stop an
audioPlayer
which does not exists inside of it. ThemainMenueViewController
has theaudioPlayer
, so it has to be the one to tell the audio player to stop.You can use NSNotifications for this. Each NSNotification is sent through the singleton NSNotificationCenter. Here are the basic steps for your situation:
Hope this helps!