When I stream a audio-stream from iOS to tvOS it works, but shows the black screen (mirrorring or video screen I think). What do I have to do to tell tvOS that it's audio and therefore it shows the title, artist and image in the left top corner?
I use AVPlayer (this is my ViewDidLoad):
//0. Become first responder
UIApplication.shared.becomeFirstResponder()
//1. Set up the session
radioAudioSession = AVAudioSession.sharedInstance()
NotificationCenter.default.addObserver(self, selector: #selector(AVAudioSessionInterruption(_:)), name: NSNotification.Name.AVAudioSessionInterruption, object: nil)
//2. Choose and set category
do {try radioAudioSession.setCategory(AVAudioSessionCategoryPlayback, mode: AVAudioSessionModeDefault, routeSharingPolicy: .longForm)}
catch {print("An Error occured setting catergory the audio session: \(error)")}
do {try radioAudioSession.setActive(true, with: [])}
catch {print("An Error occured activating the audio session: \(error)")}
//3. Set AVPlayer
radioAVPlayer = AVPlayer.init(playerItem: radioAVPlayerItem)
//4. Handle interruptions -> zie @objc func AVAudioSessionInterruption ...
radioAVPlayerItem.addObserver(self, forKeyPath: "timedMetadata", options: NSKeyValueObservingOptions(), context: nil)
radioAVPlayer.addObserver(self, forKeyPath: "rate", options: NSKeyValueObservingOptions(), context: nil
This is my update-function for NowPlayingInfo:
func updateNowPlayingInfo (withRadiostation: [String], title: String?, artist: String?) {
//bij title en artist = nil dan is er geen songinformatie beschikbaar in de stream
let radioName = withRadiostation[0]
let radioSlogan = withRadiostation[1]
radioURL = URL.init(string: withRadiostation[2])
let radioImage = UIImage(named: withRadiostation[3])!
nowPlaying.text = radioName
let radioArtwork = MPMediaItemArtwork.init(boundsSize: radioImage.size, requestHandler: { (size) -> UIImage in
return radioImage})
if title == nil || artist == nil || (title?.isEmpty)! || (artist?.isEmpty)! {
//MPMediaItemPropertyAssetURL: radioURL!
audioInfo.nowPlayingInfo = [MPMediaItemPropertyTitle: radioName, MPMediaItemPropertyArtist: radioSlogan, MPNowPlayingInfoPropertyIsLiveStream: true, MPMediaItemPropertyMediaType: MPNowPlayingInfoMediaType.audio.rawValue, MPMediaItemPropertyArtwork: radioArtwork]
} else {
//MPMediaItemPropertyAssetURL: radioURL!
audioInfo.nowPlayingInfo = [MPMediaItemPropertyTitle: title as Any, MPMediaItemPropertyArtist: artist as Any, MPNowPlayingInfoPropertyIsLiveStream: true, MPMediaItemPropertyMediaType: MPNowPlayingInfoMediaType.audio.rawValue, MPMediaItemPropertyArtwork: radioArtwork]
}
}
Sorry, didn't see your respons. It works oke now. I think it was a bug in iOS. In the meantime I added some properties to the nowPlayingInfo:
audioInfo.nowPlayingInfo = [MPMediaItemPropertyTitle: artist!, MPMediaItemPropertyArtist: title!, MPMediaItemPropertyAlbumTitle: radioName, MPMediaItemPropertyMediaType: MPNowPlayingInfoMediaType.audio.rawValue, MPMediaItemPropertyAssetURL: radioURL!, MPMediaItemPropertyArtwork: radioArtwork, MPNowPlayingInfoPropertyIsLiveStream: true, MPNowPlayingInfoPropertyPlaybackRate: 1.0]