How to add/use GCKMediaQueue in Swift?

2019-07-29 12:32发布

问题:

So I have managed to play a video on Chromecast. But only one at a time. I've been trying to figure how to programmatically add to the queue. The idea is to keep playing videos all day. In the code below "playthisvideo()" randomly returns a string that contain an http://.....mp4 . I've look at Google's documentation, it's either too vague or I just don't understand it. And I can't seem to find any examples that would lead the way for me to follow.

func castthevideo() {
    let metadata = GCKMediaMetadata()
    metadata.setString("Los Simpsons", forKey: kGCKMetadataKeyTitle)
    metadata.setString ("Barista: ¿Cómo tomas tu café? " +
        " Yo: Muy, muy en serio.",
                        forKey: kGCKMetadataKeySubtitle)
    metadata.addImage(GCKImage(url: URL(string: "https://m.media-amazon.com/images/M/MV5BYjFkMTlkYWUtZWFhNy00M2FmLThiOTYtYTRiYjVlZWYxNmJkXkEyXkFqcGdeQXVyNTAyODkwOQ@@._V1_.jpg")!,
                               width: 480,
                               height: 360))
    let PTV = playthisvideo()
    let url = URL.init(string: PTV)
    print ("******  ", PTV)
    guard let mediaURL = url else {
        print("******  invalid mediaURL")
        return }
    //let mediaInfoBuilder = GCKMediaInformationBuilder.init(contentURL: mediaURL)
    let mediaInfoBuilder = GCKMediaInformationBuilder.init(contentURL: mediaURL)
    mediaInfoBuilder.streamType = GCKMediaStreamType.none;
    mediaInfoBuilder.contentType = "video/mp4"
    mediaInfoBuilder.metadata = metadata;
    let  mediaInformation = mediaInfoBuilder.build()
    if let request = sessionManager.currentSession?.remoteMediaClient?.loadMedia(mediaInformation) { request.delegate = self }

    GCKCastContext.sharedInstance().presentDefaultExpandedMediaControls()

}

func castanthor(byAppending appending: Bool) {

    let PTV = playthisvideo()
    let url = URL.init(string: PTV)
    guard let mediaURL = url else {
        print("invalid mediaURL")
        return
    }

    myNSNumber = (1 as NSNumber)
    if let remoteMediaClient = GCKCastContext.sharedInstance().sessionManager.currentCastSession?.remoteMediaClient {
        let builder = GCKMediaQueueItemBuilder()
        builder.mediaInformation = selectedItem.mediaInfo
        builder.autoplay = true
        builder.preloadTime = 3
        let item = builder.build
        if remoteMediaClient.mediaStatus != nil, appending {
            let request = remoteMediaClient.queueInsert(item(), beforeItemWithID: kGCKMediaQueueInvalidItemID)
            request.delegate = self
        } else {
            let options = GCKMediaQueueLoadOptions()
            options.repeatMode = remoteMediaClient.mediaStatus?.queueRepeatMode ?? .off
            let request = castSession.remoteMediaClient?.queueLoad([item()], with: options)
            request?.delegate = self
        }
    }}

回答1:

var mediaItems = [GCKMediaQueueItem]()
var urls = // Array of only audio and videos
for index in 0..<urls.count {
    let builder = GCKMediaQueueItemBuilder()
    let mediaInfoBuilder = GCKMediaInformationBuilder.init(contentURL: urls[i])
    mediaInfoBuilder.streamType = GCKMediaStreamType.none;
    mediaInfoBuilder.contentType = "video/mp4"
    mediaInfoBuilder.metadata = metadata;
    let mediaInformation = mediaInfoBuilder.build()
    builder.mediaInformation = mediaInformation
    builder.autoplay = true
    builder.preloadTime = 3
    let item = builder.build
    mediaItems.append(item)
}

if let remoteMediaClient = GCKCastContext.sharedInstance().sessionManager.currentCastSession?.remoteMediaClient {
    let loadOptions = GCKMediaQueueLoadOptions()
    loadOptions.repeatMode = .all
    loadOptions.startPosition = 0
    remoteMediaClient.queueLoadItems(mediaItems, withOptions:loadOptions)
}