【问题标题】:How to handle background audio playing while iOS device is locked or on another application?如何在 iOS 设备被锁定或在另一个应用程序上处理背景音频播放?
【发布时间】:2012-05-12 20:09:08
【问题描述】:

使用 OpenFrameworks 为 iOS 设计一个生成音乐系统,我需要提供一种模式,让用户可以在以下情况下聆听应用程序生成的音乐:

  • 设备已锁定
  • 用户使用其他应用程序

一些应用程序,如 BLOOM 或闹钟,就是这样工作的,并向用户建议启用/禁用此功能的开关。

有什么建议吗?

【问题讨论】:

    标签: ios audio background multitasking locked


    【解决方案1】:

    播放背景音频

    连续播放或录制音频的应用程序(即使应用程序在 在后台运行)可以注册以在 背景。您从背景模式部分启用音频支持 Xcode 项目中的 Capabilities 选项卡。 (您也可以启用 通过在音频中包含 UIBackgroundModes 键来支持此功能 应用程序的 Info.plist 文件中的值。)播放音频内容的应用程序 背景必须播放有声内容而不是静音。

    Apple reference "Playing and Recording Background Audio"

    Ensuring That Audio Continues When the Screen Locks

    为了启用/禁用此功能,我找到了Activating and Deactivating Your Audio Session,我自己没有尝试过,但它看起来像你需要的。

    【讨论】:

    • 您好 Danich,非常感谢您的回答。有没有办法在运行时更改它?
    • 我为这个特定点打开了另一个线程:stackoverflow.com/questions/10504432/…
    • 抱歉,没有注意到您关于启用/禁用的问题。我认为新线程不是一个好主意。更新了我的答案。如果您认为有用,请接受。
    • 这个答案的第二个链接是404,我觉得应该指向这里:developer.apple.com/library/ios/qa/qa1626/_index.html
    • 你的第二个链接是黄金!非常感谢。
    【解决方案2】:

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

    即 1) 将必需的后台模式设置为应用播放音频

    2) 设置应用程序不在后台运行为NO。

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

    那么,你需要在 AppDelegate 中写这么多代码

    现在,您可以在手机屏幕锁定或进入后台时轻松播放音频。

    【讨论】:

    • “应用程序不在后台运行”应设置为“NO”
    • 这太好了,谢谢。我还需要设置 Capabilities > Background Modes > Audio & AirPlay
    【解决方案3】:

    在 xCode 项目设置和代码中进行以下更改。

    步骤 1) 在 Xcode 的导航器中选择您的项目文件。然后,从 Capabilities 部分,打开 Background Modes 子部分。在为您提供背景模式列表后,勾选 Audio & Airplay 开关。

    stp 2) 使用以下 swift 代码,基本上你需要为你的应用设置音频会话。

    var audioPlayer : AVAudioPlayer!
    
    @IBAction func playButtonClicked(sender : AnyObject){
    
        let dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
        dispatch_async(dispatchQueue, {
    
            if let data = NSData(contentsOfFile: self.audioFilePath())
            {
                do{
                    let session = AVAudioSession.sharedInstance()
    
                    try session.setCategory(AVAudioSessionCategoryPlayback)
                    try session.setActive(true)
    
                    self.audioPlayer = try AVAudioPlayer(data: data)
                    //self.audioPlayer.delegate = self
                    self.audioPlayer.prepareToPlay()
                    self.audioPlayer.play()
                }
                catch{
                    print("\(error)")
                }
            }
        });
    }
    
    func audioFilePath() -> String{
    
        let filePath = NSBundle.mainBundle().pathForResource("mySong", ofType: "mp3")!
        return filePath
    }
    

    此音频播放会话将播放您的应用程序播放,即使应用程序处于后台或手机处于静音模式或设备已锁定。

    【讨论】:

      【解决方案4】:

      看看这个教程。它有一些后台服务示例

      http://www.raywenderlich.com/29948/backgrounding-for-ios

      【讨论】:

        【解决方案5】:

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

        1) 将必需的后台模式设置为应用播放音频

        2) 设置应用程序不在后台运行为NO。

        let dispatchQueue = DispatchQueue.global()
                    dispatchQueue.async(execute: {
                            do{
                                let session = AVAudioSession.sharedInstance()
        
                                try session.setCategory(AVAudioSessionCategoryPlayback)
                                try session.setActive(true)
        
                                self.isObjectAllocate = true
                                if self.isPlayed == false{
                                    self.playSound(soundName: "http://radio.zahraun.com:8000/live.m3u")
                                    self.isPlayed = true
                                    self.btnPlayAudio.setImage(#imageLiteral(resourceName: "pause") , for: .normal)
        
                                }else{
                                    self.audioPlayer.pause()
                                    self.isPlayed = false
                                    self.btnPlayAudio.setImage(#imageLiteral(resourceName: "audioPlay"), for: .normal)
                                }
                            }
                            catch{
                                print("\(error)")
                            }
                    });
        

        【讨论】:

          【解决方案6】:

          你也可以使用这个代码:

          AVAudioSession *session = [AVAudioSession sharedInstance];
          [session setCategory:AVAudioSessionCategoryPlayback error:NULL];
          
          AVAudioSession *backgroundMusic = [AVAudioSession sharedInstance];
          
          [backgroundMusic setCategory:AVAudioSessionCategoryPlayback error:NULL];
          

          【讨论】:

            猜你喜欢
            • 2016-08-30
            • 2021-10-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-03-03
            • 1970-01-01
            • 2019-05-27
            • 2020-09-23
            相关资源
            最近更新 更多