Swift Radio Streaming AVPlayer

后端 未结 1 1182
孤城傲影
孤城傲影 2021-01-18 23:36

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

In Objective-C

AVPla         


        
1条回答
  •  别那么骄傲
    2021-01-19 00:22

    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;
    

    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
        }
    }
    

    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.

    ItunesSearch Lib in GitHub

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