iOS swift流媒体应用程序不在后台模式下播放音乐

Posted

技术标签:

【中文标题】iOS swift流媒体应用程序不在后台模式下播放音乐【英文标题】:iOS swift streaming app does not play music in background mode 【发布时间】:2014-12-03 19:29:09 【问题描述】:

我的应用程序运行良好,但一旦屏幕安全打开或在 iphone 上执行其他操作,流就会停止。我激活了后台模式“正在播放音频”,但它没有帮助。

这是我的 ViewController.swift

import UIKit
import MediaPlayer

class ViewController: UIViewController 
    let player: MPMoviePlayerViewController = MPMoviePlayerViewController(contentURL: NSURL(string: "http://url to my stream"))

    override func viewDidLoad() 
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        player.moviePlayer.movieSourceType = .Streaming
        self.presentViewController(player, animated: true, completion: nil)

        loadAddressURL()
    

    func stop() 
        player.moviePlayer.stop()
    
    @IBAction func Hitplay(sender: AnyObject) 
        player.moviePlayer.play()
    

    @IBAction func Hitpause(sender: AnyObject) 
        player.moviePlayer.stop()
    

    @IBOutlet var Nowplay: UIWebView!
    var URLPath = "http://url to on air now"

    func loadAddressURL() 
        let requestURL = NSURL (string:URLPath)
        let request = NSURLRequest (URL: requestURL!)
        Nowplay.loadRequest(request)
    

这是我的 info.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-      1.0.dtd">
<plist version="1.0">
<dict>
 <key>CFBundleDevelopmentRegion</key>
 <string>en</string>
 <key>CFBundleExecutable</key>
 <string>$(EXECUTABLE_NAME)</string>
 <key>CFBundleIdentifier</key>
<string>com.product name.$(PRODUCT_NAME:rfc1034identifier)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>UIBackgroundModes</key>
<array>
    <string>audio</string>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIMainStoryboardFile</key>
<string>Main</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
    <string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
</array>
</dict>

【问题讨论】:

【参考方案1】:

设置音频背景模式是正确的,但我认为您还需要为音频会话设置音频类别。

尝试将此添加到您的应用委托的didFinishLaunchingWithOptions

var activeError: NSError? = nil
AVAudiosession.sharedInstance().setActive(true, error: &activeError)

if let actError = activeError 
  NSLog("Error setting audio active: \(actError.code)")


var categoryError: NSError? = nil
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: &categoryError)

if let catError = categoryError 
  NSLog("Error setting audio category: \(catError.code)")

【讨论】:

是的,感谢您的帮助,它正在正常工作!

以上是关于iOS swift流媒体应用程序不在后台模式下播放音乐的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3 如何覆盖静音模式并从后台播放音频

如何在后台模式下为 iOS 应用程序创建“重新打开”应用程序按钮? (迅速)

如何在后台为ios音频文件运行Phonegap应用程序

iOS 10 上的 Cordova 媒体插件是不是仍可使用后台播放?

AVPlayer 不在后台转发流媒体轨道

iOS 后台任务 Swift