Thursday, 3 October 2019

Xcode swift - Background mode - Audio in AVPlayer when locked

I have got an AVPlayer setup but I cant seem to get the video audio to continue to play when the phone is locked or on the home screen. I have edited the info.plist and enabled the audio background mode but I think need to add some code. Please could you help me out. Thanks

Here is my code:

import UIKit
import AVFoundation
import AVKit

class ViewController: UIViewController {

    @IBAction func WarmUp(sender: UIButton)
    {
        WarmUpVideo()
    }

    func WarmUpVideo()
    {
        let filePath = NSBundle.mainBundle().pathForResource("132", ofType: "MOV")
        let videoURL = NSURL(fileURLWithPath: filePath!)

        let player = AVPlayer(URL: videoURL)
        let playerViewController = AVPlayerViewController()

        playerViewController.player = player

        self.presentViewController(playerViewController, animated: true) { () -> Void in playerViewController.player!.play()
        }

    }

    func playExternalVideo()
    {

}


    @IBAction func CoolDown(sender: UIButton)
    {
        CoolDownVideo()
    }

    func CoolDownVideo()
    {
        let filePath = NSBundle.mainBundle().pathForResource("132", ofType: "mp4")
        let videoURL = NSURL(fileURLWithPath: filePath!)

        let player = AVPlayer(URL: videoURL)
        let playerViewController = AVPlayerViewController()

        playerViewController.player = player

        self.presentViewController(playerViewController, animated: true) { () -> Void in playerViewController.player!.play()
        }

    }

}


from Xcode swift - Background mode - Audio in AVPlayer when locked

No comments:

Post a Comment