AVAudio 在呈现模态视图后不会停止

发布于 2024-10-26 05:03:56 字数 781 浏览 6 评论 0原文

我有一个显示电影的应用程序。第一个屏幕显示呈现不同模式视图的按钮。这些模态视图内部是播放视频的按钮。

我希望主菜单播放音乐(足够简单),但我希望它在第二个模式视图中持续存在,但在视频开始播放时停止。

当按下电影按钮时,我尝试[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2024-11-02 05:03:57

您的模态视图控制器正在尝试停止内部不存在的 audioPlayermainMenueViewControlleraudioPlayer,因此它必须是告诉音频播放器停止的那个。

您可以为此使用 NSNotifications。每个 NSNotification 通过单例 NSNotificationCenter.以下是适合您情况的基本步骤:

  1. 主菜单准备就绪后会注册通知。
  2. 您的主菜单具有在通知触发时调用的方法。
  3. 当需要关闭音频时,您的子菜单会触发一个方法。
  4. 当您的主菜单准备关闭时,它会取消注册。

希望这有帮助!

Your modal view controller is trying to stop an audioPlayer which does not exists inside of it. The mainMenueViewController has the audioPlayer, 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:

  1. Your main menu registers for notifications when it is ready.
  2. Your main menu has methods to call when the notifications fire.
  3. Your submenu fires a method when it needs to turn off the audio.
  4. Your main menu unregisters when it's ready to close.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文