【问题标题】:iOS Swift m3u8 background play doesn't workiOS Swift m3u8后台播放不起作用
【发布时间】:2015-05-30 07:18:25
【问题描述】:

我在按下 HOME 按钮并从前台返回后尝试后台播放 .m3u8。

按下返回按钮时它会停止。 请任何帮助。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        var audioSession = AVAudioSession.sharedInstance()
        audioSession.setCategory(AVAudioSessionCategoryPlayback, error: nil)
        audioSession.setActive(true, error: nil)

        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.


    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.



    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.


    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.


    }


}

视图控制器:

class ViewController: UIViewController {

    var myMoviePlayerView : MPMoviePlayerViewController!

    override func viewDidLoad() {
        super.viewDidLoad()


        let myButton = UIButton()
        myButton.frame.size = CGSize(width: 100, height: 40)
        myButton.layer.position = CGPoint(x: self.view.bounds.width/2, y: self.view.bounds.height/2)
        myButton.layer.masksToBounds = true
        myButton.layer.cornerRadius = 20.0
        myButton.backgroundColor = UIColor.orangeColor()
        myButton.setTitle("Play", forState: .Normal)
        myButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        myButton.setTitleShadowColor(UIColor.grayColor(), forState: .Normal)
        myButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside)
        self.view.addSubview(myButton)

    }



    func onClickMyButton(sender: UIButton) {


        let url = NSURL(string :"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8")

        myMoviePlayerView = MPMoviePlayerViewController(contentURL: url)

        myMoviePlayerView.moviePlayer.movieSourceType = .Streaming

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "moviePlayBackDidFinish:",
            name: MPMoviePlayerPlaybackDidFinishNotification,
            object: myMoviePlayerView.moviePlayer)

        self.presentViewController(myMoviePlayerView, animated: true, completion: nil)
    }

    func moviePlayBackDidFinish(notification: NSNotification) {
        println("moviePlayBackDidFinish:")

        NSNotificationCenter.defaultCenter().removeObserver(self, name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

info.plist

<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>**********************************</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>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

【问题讨论】:

    标签: ios swift background mpmovieplayercontroller


    【解决方案1】:

    我认为您正在使用 MPMoviePlayerViewController 播放音频,这就是引发问题的原因。

    在属性列表 (.plist) 文件中添加一个名为 必需背景模式 的键..

    如下图..

    并将这一行添加到AppDelegateaudioSession

    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()
    

    您可以从this获得更多帮助

    【讨论】:

    • @user818992 我认为您正在使用MPMoviePlayerViewController 播放音频,这就是引发问题的原因。
    • 非常感谢。当我按下电源按钮时,它在 AVPlayer 上运行良好。但是当我按下主页按钮时它不起作用......
    • 你知道如何在按下主页按钮时在后台播放吗?谢谢
    猜你喜欢
    • 2018-12-31
    • 2018-04-19
    • 2022-02-03
    • 1970-01-01
    • 2014-08-17
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多