Search code examples
iosswiftchromecastgoogle-cast

iOS Sdk Google Chromecast Subtitles


This is how I have implemented to show subtitles using a Google Chromecast device. But the subtitle doesnt appear. Do I have to make changes in Chromecast API ?

var subtitleName:String = ""
var subtitleLink:String = ""
var subtitleType:String = ""
var subtitleCode:String = ""

if let _ = self.selectedSubtitle
{
  let subtitleIndex: Int = self.selectedSubtitle! - 1
  subtitleName = self.videoObject.subtitles![subtitleIndex].language!
  subtitleLink = self.videoObject.subtitles![subtitleIndex].link!
  subtitleLink = subtitleLink + ".vtt"

  subtitleType = self.videoObject.subtitles![subtitleIndex].type!
  subtitleCode = (self.subtitleLanguages.objectAtIndex(subtitleIndex) as! ICFLanguageObject).iso_639_3! as String

}

print("\n\nName: \(subtitleName),\n Link:\(subtitleLink) \n Type: \(subtitleType)\n Code: \(subtitleCode)\n\n")
//Values Printed on console

//Name: ara,

//Link:http://a**************c.vtt

//Type: subtitles

//Code: ara



    let subtitlesTrack = GCKMediaTrack(identifier: chromeCast_SubtitleID,
    contentIdentifier:subtitleLink,
    contentType: "text/vtt",
    type: GCKMediaTrackType.Text,
    textSubtype: GCKMediaTextTrackSubtype.Captions,
    name: subtitleName,
    languageCode: subtitleCode,
    customData: nil)

// Set Progress
   let time: Double = duration * (value - minValue) / (maxValue - minValue)
   let progress: NSTimeInterval = NSString(format: "%f", (time)).doubleValue


   let textTrackStyle = GCKMediaTextTrackStyle.createDefault()
   textTrackStyle.foregroundColor = GCKColor(CSSString: "#FF000080")
   textTrackStyle.fontFamily = "serif"
   styleChangeRequestID = (mediaControlChannel?.setTextTrackStyle(textTrackStyle))!
   print(styleChangeRequestID)

                   mediaControlChannel?.setActiveTrackIDs([chromeCast_SubtitleID])
                   mediaControlChannel?.setTextTrackStyle(textTrackStyle)
                   deviceManager?.setVolume(0.5)

                    let tracks = [subtitlesTrack]

                       let mediaInformation = GCKMediaInformation(
                       contentID:self.playbackObject.playbackURL(),
                       streamType: GCKMediaStreamType.None,
                       contentType: self.playbackObject.playbacktype(),
                       metadata: metadata,
                       streamDuration: progress,
                       mediaTracks: tracks,
                       textTrackStyle: textTrackStyle,
                       customData: nil
                    )


deviceManager?.setVolume(0.5)

mediaControlChannel!.loadMedia(mediaInformation, autoplay: true, playPosition: progress)

//[END MEDIA]


Solution

  • I was calling wrong function for MediaControlChannel. For Subtitles a different function has to be called. This function was never used In Google APIs documentation or anywhere in sample codes. That would be great Help if Google can update a sample project using GCKMediaTrack for Text , Audio and Videos. Also, post some examples for casting Subtitles stream.

    Heres the function!

    self.mediaControlChannel!.loadMedia(mediaInformation, autoplay: true, playPosition: 0, activeTrackIDs: tracks)