iOS 应用程序在后台运行时发出声音

发布于 2024-12-06 22:22:37 字数 1139 浏览 0 评论 0原文

我试图在我的应用程序在后台运行时播放声音。我使用 AVAudioPlayer,当应用程序处于活动状态时它可以正常工作,但在后台 AVAudioPlayer 不播放任何内容,我不知道该怎么办。

我以为我可以使用 UILocalNotification,但它在锁屏中显示并发出警报,但我不想要它。


感谢您的回答。我还有一个疑问。如果我创建此音频会话,并且正在播放音乐,系统会同时混合两者吗?

我怎样才能减少我的应用程序声音播放时的 iPod 音乐音量(3 秒),并在它完成时再次增加,就像 AppStore 中大多数正在运行的应用程序一样。 (也在后台)

这是我在 AppDelegate 中的代码

//Creamos la sesión de audio
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

在我看来,当我想播放声音时:

-(void)avisosound{

   // AudioSessionSetActive(YES);
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.wav", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 0;

    if (audioPlayer == nil){
    }
    else{
        [audioPlayer play];  
    }
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }

但是当我播放声音时,iPod 音乐停止了。

I'm trying to play a sound when my App is in the background. I use AVAudioPlayer, and it works correctly when the app is active, but in the background AVAudioPlayer doesn't play anything and I don't know how can I do.

I have thought that I could use UILocalNotification, but it shows and alert in the lockscreen and I don't want it.


Thanks for your answers. I have another doubt. If I create this audiosession, and I am playing music the system will mix both at the same time?.

How can I do to decrease ipod music volume will my app sound is playing (3 seconds) and increase again when it had finished like most of the running apps in the AppStore.
(In the background too)

This is my code in the AppDelegate

//Creamos la sesión de audio
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

And in my view when I want to play the sound:

-(void)avisosound{

   // AudioSessionSetActive(YES);
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/sound.wav", [[NSBundle mainBundle] resourcePath]]];

    NSError *error;
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
    audioPlayer.numberOfLoops = 0;

    if (audioPlayer == nil){
    }
    else{
        [audioPlayer play];  
    }
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
    }

But when I play the sound, the ipod music stops.

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

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

发布评论

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

评论(4

一向肩并 2024-12-13 22:22:37

您需要在 plist 文件中进行一些更改。

  1. 必需的后台模式设置为应用程序播放音频
  2. 应用程序不在后台运行设置为

然后,您需要在您的 AppDelegate 中编写以下代码:

   NSError *setCategoryErr = nil;
   NSError *activationErr  = nil;
   [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
   [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

现在,您可以在手机屏幕锁定或您的应用程序在后台运行时轻松运行音频。

You need to make couple of changes in plist file.

  1. Set Required background mode to App plays audio
  2. Set Application does not run in background to YES

Then, you need to write the following code in your AppDelegate:

   NSError *setCategoryErr = nil;
   NSError *activationErr  = nil;
   [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
   [[AVAudioSession sharedInstance] setActive:YES error:&activationErr];

Now, you can easily run audio while phone screen locks or your app is in the background.

水晶透心 2024-12-13 22:22:37

以下内容告诉系统即使在 iPhone 屏幕锁定时也继续播放音频:(

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];

大多数情况下,您将其放入 didFinishLaunchingWithOptions 中。)

并告诉系统在您的应用程序时播放音频在后台,添加 UIBackgroundModes 并将其设置为 audio 在你的 plist 中。

(任何需要播放音频的前台应用程序将优先于这些设置和应用程序的声音播放)。

要添加“闪避”,以在播放音频时降低其他音频的音量,您需要实现 kAudioSessionProperty_OtherMixableAudioShouldDuck。请在音频会话编程指南中查找。

The following tells the system to continue playing the audio even when your iPhone screen locks out:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:NULL];

(Most of the time you put this in your didFinishLaunchingWithOptions.)

And to tell the system to play your audio when your app is in the background, add UIBackgroundModes and set it to audio in you plist.

(Any foreground apps that need to play audio will take precedence over these settings and your app's sound playback).

To add "ducking", to lower the volume of other audio while your audio is playing, you need to implement kAudioSessionProperty_OtherMixableAudioShouldDuck. Look for that in the Audio Session programming guide.

朦胧时间 2024-12-13 22:22:37

要在后台播放声音(假设您的应用程序已设置正确的后台模式),您的应用程序必须在前台启动声音而不是停止它。最简单的方法是使用音频队列 API 或 RemoteIO 音频单元。

To play a sound in the background (assuming your app has set up the proper background modes), your app has to start the sound in the foreground and not stop it. The easiest ways to do this are using the Audio Queue API or the RemoteIO Audio Unit.

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