Getting Timed Metadata in Swift ios 8 From M3U8 Streaming Video

后端 未结 1 735
离开以前
离开以前 2021-02-04 21:05

I\'m trying to duplicate this https://jmacmullin.wordpress.com/2010/11/03/adding-meta-data-to-video-in-ios/ in swift.

Here is a video of Jake\'s code in action...

<
1条回答
  •  灰色年华
    2021-02-04 21:09

    So I got this to work, but ended up ditch MPMovieController opting for AVPlayer. Here is my code, and the link to another post that helped me get it functional.

    Timed Metadata with AVPlayer

    import UIKit
    
    import MediaPlayer
    import AVFoundation
    
    var youtubeRequest: NSURLRequest! = nil
    var player : AVPlayer? = nil
    
    var url:NSString!
    
    
    class ViewController: UIViewController {
    
    
    var moviePlayer: MPMoviePlayerController?
    var movieItem: AVPlayerItem!
    var Player: AVPlayer!
    
    var playerLayer : AVPlayerLayer? = nil
    var asset : AVAsset? = nil
    var playerItem: AVPlayerItem!
    
    var youtubeRequest: NSURLRequest! = nil
    
    @IBOutlet var playButtonOutlet: UIBarButtonItem!
    
    @IBAction func playButtonAction(sender: AnyObject) {player?.play()
        playButtonOutlet.enabled = false
    
    }
    
    @IBOutlet var videoView: UIView!
    @IBAction func loadURL(sender: AnyObject) {player?.pause()
        playButtonOutlet.enabled = true
    }
    @IBOutlet var urlButton: UIButton!
    override func viewDidLoad() {
    
        super.viewDidLoad()
    
    
    
        // Do any additional setup after loading the view, typically from a nib.
    
        urlButton.enabled = false
    
        let videoURL = NSURL(string: "http://path/to/video.m3u8")
        playButtonOutlet.enabled = false
    
        movieItem = AVPlayerItem(URL: NSURL(string: "http://path/to/video.m3u8"))
        movieItem.addObserver(self, forKeyPath: "timedMetadata", options: nil, context: nil)
        Player = AVPlayer(playerItem: movieItem)
    
        var metaArray: Array = [moviePlayer?.timedMetadata]
    
    
    
    
    
    
    
        asset = AVAsset.assetWithURL(videoURL) as? AVAsset
        playerItem = AVPlayerItem(asset: asset)
        playerItem = AVPlayerItem(URL: NSURL(string: "http://path/to/video.m3u8"))
        playerItem.addObserver(self, forKeyPath: "timedMetadata", options: nil, context: nil)
        playerItem.addObserver(self, forKeyPath: "presentationSize", options: nil, context: nil)
        player = AVPlayer(playerItem: playerItem)
    
    
    
        playerLayer = AVPlayerLayer(player: player)
        playerLayer!.frame = videoView.bounds
    
        videoView.layer.addSublayer(self.playerLayer)
    
        player!.play()
    
        NSNotificationCenter.defaultCenter().addObserver(
    
            self,
    
            selector: "rotated",
    
            name: UIDeviceOrientationDidChangeNotification,
    
            object: nil)
    
    
    }
    
        override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer) -> Void {
    
            if keyPath != "timedMetadata" { return }
    
            var data: AVPlayerItem = object as AVPlayerItem
    
            var urlError = false
    
            for item in data.timedMetadata as [AVMetadataItem] {
    
                println(item.stringValue)
                var metaArray: Array = [playerItem?.timedMetadata]
                println("Total objects in array \(metaArray[0])")
    
    
                var data = item.stringValue
    
              url = NSString(string: data) as NSString!
    
    
    
                if url != nil {
    
                 urlButton.enabled = true
    
                    println("The url is \(url)")
    
    
    
    
                } else {
    
                    urlError = true
    
                }
    
    
                var urlRedirect = NSURL(fileURLWithPath: "\(url)")
    
    
                println("The url is \(urlRedirect)")
    
            }
    
    
        }
    
    override func didReceiveMemoryWarning() {
    
        super.didReceiveMemoryWarning()
    
        // Dispose of any resources that can be recreated.
    
    }
    
    func rotated()
    {
        if(UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation))
        {
         self.navigationController?.navigationBarHidden = true
            playerLayer?.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)
            playerLayer?.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height)
    
            println("landscape")
        }
    
        if(UIDeviceOrientationIsPortrait(UIDevice.currentDevice().orientation))
        {
            self.navigationController?.navigationBarHidden = false
            playerLayer!.frame = videoView.bounds
    
            println("portraight")
        }
    
     }
    
    
    }
    

    0 讨论(0)
提交回复
热议问题