这是杰克的代码在行动中的视频……
Objective C Timed Metadat in HLS stream
这是类似的东西的附加链接……
http://cloudfields.net/blog/metadata-audiostream-mpmovieplayercontroller/
当我的视频正在播放定时元数据时,应该更新按钮以在单击时重定向到webvIEw中的特定youtube网址.我的视频大约15分钟,有6个定时元数据网址.
我无法在Swift中找到任何关于如何实现此目的的代码或文档.我已经设法将Jake的Objective C代码转换为他的Notification调用.
// Register for Meta-data notificationsNSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addobserver:self selector:@selector(MetadataUpdate:) @R_419_6889@:MPMovIEPlayerTimedMetadataUpdatednotification object:nil];
到Swift Code
NSNotificationCenter.defaultCenter().addobserver( self,selector: "MetadataUpdated",@R_419_6889@: MPMovIEPlayerTimedMetadataUpdatednotification,object: nil)
杰克的功能
Actor *actor = [[Actor alloc] init]; if ([player timedMetadata]!=nil && [[player timedMetadata] count] > 0) {for (MPTimedMetadata *Metadata in [player timedMetadata]) {if ([[Metadata.allMetadata valueForKey:@"key"] isEqualToString:@"TPE1"]) { [actor set@R_419_6889@:[Metadata.allMetadata objectForKey:@"value"]]; } if ([[Metadata.allMetadata valueForKey:@"key"] isEqualToString:@"WXXX"]) { NSURL *url = [NSURL URLWithString:[Metadata.allMetadata objectForKey:@"value"]]; [actor setProfileURL:url]; } }}// display some UI element for the user to interact with
到斯威夫特
func MetadataUpdated (notification: NSNotification!) { if movIEPlayer?.timedMetadata != nil && movIEPlayer?.timedMetadata.count > 0 { for MPTimedMetadata in [movIEPlayer?.timedMetadata] { if MPTimedMetadata?.description == ("TPE1") { let @R_419_6889@ = ("value") } if MPTimedMetadata?.description == ("WXXX") { var url = NSURL.observeValueForKeyPath("value") } } }println("Things are kind of working")}}
对于我的生活,虽然我无法弄清楚如何将元数据转换为任何可 *** 作的代码.我真的需要创建一个重定向到元数据中携带的URL的按钮,但不能将其转换为String.任何帮助是极大的赞赏.这是我到目前为止所拥有的.
import UIKitimport MediaPlayerclass VIEwController: UIVIEwController {var movIEPlayer: MPMovIEPlayerController?var youtube = ""overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() // Do any additional setup after loading the vIEw,typically from a nib. let url = NSURL(string: "http://path/to/vIDeo.m3u8") movIEPlayer = MPMovIEPlayerController(contentURL: url) if let player = movIEPlayer { player.vIEw.frame = CGRect(x: 0,y: 0,wIDth: self.vIEw.frame.size.wIDth,height: self.vIEw.frame.size.height) player.vIEw.sizetoFit() player.scalingMode = MPMovIEScalingMode.None player.movIESourceType = MPMovIESourceType.Streaming //player.repeatMode = MPMovIERepeatMode.One player.play() self.vIEw.addSubvIEw(player.vIEw) NSNotificationCenter.defaultCenter().addobserver( self,selector: "MetadataUpdated:",object: nil) }}overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() // dispose of any resources that can be recreated.}func MetadataUpdated (notification: NSNotification!) { if movIEPlayer?.timedMetadata != nil && movIEPlayer?.timedMetadata.count > 0 { for MPTimedMetadata in [movIEPlayer?.timedMetadata] { if MPTimedMetadata?.description == ("TPE1") { let @R_419_6889@ = ("value") } if MPTimedMetadata?.description == ("WXXX") { var url = NSURL.observeValueForKeyPath("value") } }}println("Things are kind of working")}}所以我得到了这个工作,但最终沟渠MPMovIEController选择AVPlayer.这是我的代码,以及帮助我实现其功能的另一篇帖子的链接.
Timed Metadata with AVPlayer
import UIKitimport MediaPlayerimport AVFoundationvar youtubeRequest: NSURLRequest! = nilvar player : AVPlayer? = nilvar url:Nsstring!class VIEwController: UIVIEwController {var movIEPlayer: MPMovIEPlayerController?var movIEItem: AVPlayerItem!var Player: AVPlayer!var playerLayer : AVPlayerLayer? = nilvar asset : AVAsset? = nilvar 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<Any> = [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,context: nil) playerItem.addobserver(self,forKeyPath: "presentationSize",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",@R_419_6889@: UIDeviceOrIEntationDIDChangeNotification,object: nil)} overrIDe func observeValueForKeyPath(keyPath: String,ofObject object: AnyObject,change: [NSObject : AnyObject],context: UnsafeMutablePointer<VoID>) -> 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<Any> = [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,self.vIEw.frame.size.wIDth,self.vIEw.frame.size.height) playerLayer?.frame = CGRectMake(0,self.vIEw.frame.size.height) println("landscape") } if(UIDeviceOrIEntationIsPortrait(UIDevice.currentDevice().orIEntation)) { self.navigationController?.navigationbarHIDden = false playerLayer!.frame = vIDeoVIEw.bounds println("portraight") } }}总结
以上是内存溢出为你收集整理的从M3U8流视频获取Swift ios 8中的定时元数据全部内容,希望文章能够帮你解决从M3U8流视频获取Swift ios 8中的定时元数据所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)