Anjali jariwala
Anjali jariwala

Reputation: 445

Play youtube video inside tableviewCell using 'YoutubePlayer Swift SDK'

I am integrating 'YouTubePlayer Swift SDK' You can check in this link Swift-YouTube-Player

All things working properly, but I face one issue i.e When I playing video, I always played in fullScreen. I want to play video inside tableviewCell only like we can see in Facebook, Instagram.

Same thing is possible in 'youtube-ios-player-helper SDK' by assigning playerVars. Like this,

NSDictionary *playerVars = @{
                          @"playsinline"    : @1
                        };

Is there anyOne who having idea to play videos in swift? Please share code if possible

Thanks in advance :)

Upvotes: 0

Views: 963

Answers (1)

Anjali jariwala
Anjali jariwala

Reputation: 445

Finally I got solution by spending 4 Hours :)

If you guys need to get SDK work same as objc Specially for parameters

Please go to 'YouTubePlayer.swift' file which inside your sdk and just replace 'serializedJSON' function with this:

fileprivate func serializedJSON(_ object: AnyObject) -> String? {

    var dict = object as! NSDictionary
    var dictTemp = dict.mutableCopy() as! NSMutableDictionary
    var dictTempPlayerVar = dictTemp.value(forKey: "playerVars") as! NSDictionary
    var dictTempPlayerVar1 = dictTempPlayerVar.mutableCopy() as! NSMutableDictionary
    dictTempPlayerVar1.setValue("1", forKey: "playsinline")
    dictTemp.setValue(dictTempPlayerVar1, forKey: "playerVars")

    do {
        // Serialize to JSON string
        let jsonData = try JSONSerialization.data(withJSONObject: dictTemp as! AnyObject, options: JSONSerialization.WritingOptions.prettyPrinted)

        // Succeeded
        return NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue) as? String

    } catch let jsonError {

        // JSON serialization failed
        print(jsonError)
        printLog("Error parsing JSON")

        return nil
    }
}

It works :)

Upvotes: 3

Related Questions