iphone sdk出现alertview时如何播放声音?

发布于 2024-08-09 00:15:14 字数 62 浏览 1 评论 0原文

我想在 Alertview 出现时播放声音文件并持续播放,直到用户单击“确定”或“取消”。我该如何执行此操作?

i want to play a sound file when alertview appears and plays continuously till user clicks on ok or cancel.how do i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

以往的大感动 2024-08-16 00:15:14

正如 Zoul 所说,您在调用 [myAlert show] 时设置并播放声音,并在警报视图回调中取消声音。您的代码将如下所示:

  AVAudioPlayer *myPlayer;

  // ...

  // create an alert...

  NSError *error;
  myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:mySoundFileURL error:&error];
  // handle errors here.
  [myPlayer setNumberOfLoops:-1];  // repeat forever
  [myPlayer play];
  [myAlert show];

  // ...

  // in alert callback.
  [myPlayer stop];
  [myPlayer release];

As Zoul says, you set-up and play your sound as you call [myAlert show] and cancel the sound in the alert view callback. Your code will look something like this:

  AVAudioPlayer *myPlayer;

  // ...

  // create an alert...

  NSError *error;
  myPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:mySoundFileURL error:&error];
  // handle errors here.
  [myPlayer setNumberOfLoops:-1];  // repeat forever
  [myPlayer play];
  [myAlert show];

  // ...

  // in alert callback.
  [myPlayer stop];
  [myPlayer release];
妞丶爷亲个 2024-08-16 00:15:14

由于您已经调用 show 方法来显示对话框,为什么不直接开始播放声音并在 警报视图回调?对于声音本身,您可以使用 AVAudioPlayer

As you already call the show method to display the dialog, why don’t you simply start playing the sound there and stop in the alert view callback? For the sound itself you can use AVAudioPlayer.

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