Swift Radio Streaming AVPlayer [英] Swift Radio Streaming AVPlayer

查看:121
本文介绍了Swift Radio Streaming AVPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Swift中从Internet流式传输音频,但是还没有找到正确的功能示例.

I want to stream an audio from the Internet in Swift but haven't found a correct functional example just yet.

Objective-C

AVPlayerItem* playerItem =[AVPlayerItem playerItemWithURL:[NSURL URLWithString:streamURL]];
[playerItem addObserver:self forKeyPath:@"timedMetadata" options:NSKeyValueObservingOptionNew context:nil];
music = [AVPlayer playerWithPlayerItem:playerItem];
[music play];

我在 Swift

let playerItem = AVPlayerItem(URL:NSURL(string:url))
        var player:AVPlayer!
        player = AVPlayer(playerItem:playerItem)
        player.rate = 1.0;
        player.play()
//this is not working

我也尝试添加playerItem.addObserver(self,forKeyPath:"timedMetadata",选项:NSKeyValueObservingOptions.New,上下文:nil),但是我遇到了错误

I also tried adding playerItem.addObserver(self, forKeyPath: "timedMetadata", options: NSKeyValueObservingOptions.New, context: nil) but I got an error

'类AVPlayerItem的实例0x16edbd20被释放,而键值观察者仍在注册.当前观察信息:( 上下文:0x0,属性:0x16ea5880> )'

'An instance 0x16edbd20 of class AVPlayerItem was deallocated while key value observers were still registered with it. Current observation info: ( Context: 0x0, Property: 0x16ea5880> )'

关于如何解决此问题的任何想法?

Any ideas on how to solve this?

推荐答案

我迅速创建了一个广播播放器,但是我使用了 MediaPlayer 框架进行播放.

I created a radio player with swift, but i used MediaPlayer framework to play.

var radioPlayer = MPMoviePlayerController(contentURL: "YOUR NSURL OBJECT WITH YOUR CUSTOM URL TO STREAM HERE");
radioPlayer.view.frame = CGRect(x: 0, y: 0, width: 0, height: 0)
radioPlayer.view.sizeToFit()
radioPlayer.movieSourceType = MPMovieSourceType.Streaming
radioPlayer.fullscreen = false;
radioPlayer.shouldAutoplay = true;
radioPlayer.prepareToPlay()
radioPlayer.play()
radioPlayer.controlStyle = MPMovieControlStyle.None;

如果您需要从流中监听元数据,则需要观察者此通知 MPMoviePlayerTimedMetadataUpdatedNotification

If you need listen metada from stream you need observer this notification MPMoviePlayerTimedMetadataUpdatedNotification

NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("metadataUpdated:"), name:MPMoviePlayerTimedMetadataUpdatedNotification, object: nil);

func metadataUpdated(n:NSNotification)
{
    if(radioPlayer.timedMetadata != nil && radioPlayer.timedMetadata.count > 0)
    {
        let firstMeta:MPTimedMetadata = radioPlayer.timedMetadata.first as! MPTimedMetadata;
        let data:String = firstMeta.value as! String;
        println(data); //your metadata here
    }
}

有了这个,您就可以使您的广播播放器成为一个收音机,因为我工作了这个收音机,所以我建立了一个音乐库来获取有关Itunes音乐的信息,好吧,这个音乐库是开放的,而且如果您发现我只想帮个忙,问题,请提供更好的解决方案,修复并推广到所有人.

With this you will can make your radio player, and because this radio i worked, i made one lib to get information about music in Itunes, well, this lib is open and because this I just want one favor, if you find problem, bug our better solution, fix and push to all.

GitHub中的ItunesSearch Lib

这篇关于Swift Radio Streaming AVPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆