声音时,应用程序在后台的iOS [英] Sound when the app is in the Background iOS

查看:178
本文介绍了声音时,应用程序在后台的iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想播放声音时,我的应用程序是在后台。我使用 AVAudioPlayer ,它正常工作时,应用程序是积极的,但会在后台 AVAudioPlayer 不玩什么我不知道我该怎么办。

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.

我以为我可以使用 UILocalNotification ,但它显示和警报在锁定画面,我不想要它。

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

谢谢您的回答。我还有一个疑问。如果我创建这个audiosession,而我播放音乐,系统将在同一时间都混?

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?.

我该怎么做才能减少ipod音乐音量将我的应用程序播放声音时(3秒),当它已经完成了最喜欢在AppStore中运行的应用程序的再次增加。
(在后台太)

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)

这是在AppDelegate中我的code

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);
    }

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

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

推荐答案

您需要做一些改动的plist文件。

You need to make couple of changes in plist file.


  1. 设置的所需的背景模式的到的应用播放音频

  2. 设置的应用程序不能在后台的运行的

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

然后,你需要编写以下code在你的AppDelegate:

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.

这篇关于声音时,应用程序在后台的iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆