8

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]

Taimur Ajmal
  • 2,778
  • 6
  • 39
  • 57

2 Answers2

4

In the subtitlesTrack assignment, try setting contentType to "text/vtt", not "subtitles". From the working example declaration in the iOS Sender Guide,

let captionsTrack = GCKMediaTrack(identifier: 1, contentIdentifier:
    "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/" +
    "DesigningForGoogleCast-en.vtt", contentType: "text/vtt", type: GCKMediaTrackType.Text,
    textSubtype: GCKMediaTextTrackSubtype.Captions, name: "English Captions",
    languageCode: "en", customData: nil)

It also appears that the code above does not call setActiveTrackIds, like so:

mediaControlChannel?.setActiveTrackIDs([1])

This function call sets the track with ID 1 to active.

Cassie
  • 43
  • 6
  • Updated my answer with some more information - see if the new solution works. – Cassie Dec 22 '15 at 22:06
  • Thanks a lot. Tried that as well. and It didn't work :( – Taimur Ajmal Dec 23 '15 at 07:05
  • Does the console report any error messages when trying to activate subtitles? – Cassie Dec 28 '15 at 19:19
  • Can you please confirm if we can cast external subtitles on a video stream of type *.m3u8 Movie plays on Chromecast in Multiple audios. Volume , Seek Functionality works too. For Subtitles, There is no error log and the subtitles just don't appear. – Taimur Ajmal Jan 06 '16 at 05:30
  • That shouldn't be an issue. Another thought - the vtt file needs to be served with CORS in addition to the m3u8 file. Is the server configured that way? Also, is there any chance you could share the m3u8 and vtt files so I can attempt to reproduce the error? – Cassie Jan 08 '16 at 17:07
  • All Our vtt files are served with CORS. Subtitles are shown on Android apps. Sharing m3u8 and vtt files in public is not allowed due to copyright restrictions. Let me try and explore it more. Thanks a lot for all the assistance. – Taimur Ajmal Jan 11 '16 at 14:04
  • If you still can't get it working, it could be a bug with the SDK itself. The bug tracker is [here](https://code.google.com/p/google-cast-sdk/issues/list) if you'd like to report it officially. – Cassie Jan 11 '16 at 16:09
  • I'm using opensubtitle db and subtitle URLs return file .srt.gz which means they need to be unzipped. But contentIdentifier only takes url? what to do? – alemac852 May 06 '17 at 08:39
3

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)
Taimur Ajmal
  • 2,778
  • 6
  • 39
  • 57