2014-12-03 17 views
5

我的應用程序運行良好,但一旦屏幕安全啓動或iPhone上的其他操作停止。我激活了背景模式「正在播放音頻」,但它沒有幫助。iOS快速流式傳輸應用程序不會在後臺模式下播放音樂

這是我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> 

回答

7

設置背景音頻模式是正確的,但我認爲你還需要設置音頻類別用於音頻會話。

嘗試添加此應用程式委派的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)") 
} 
+0

是的,感謝您的幫助這是工作,因爲它應該! – 2014-12-08 18:08:15

相關問題