0xced / XCDYouTubeKit

YouTube video player for iOS, tvOS and macOS
MIT License
2.92k stars 627 forks source link

How to play a local video. #340

Closed MarkusWu closed 7 years ago

MarkusWu commented 7 years ago

Hi, I am trying to play a local video file with XCDYouTubeVideoPlayerViewController. Is it possible to play a local video, which is saved in the documentDirectory?

MarkusWu commented 7 years ago

I am going to answer my own answer. At the time of writing this answer, XCDYouTubeVideoPlayerViewController does not support local video playing, but you can modify the code a little bit to make it work . In case anyone might be also interested in playing local video, here is the workaround.

Since XCDYouTubeVideoPlayerViewController is a subclass of MPMoviePlayerViewController, you can just use it as MPMoviePlayerViewController to play local video.

[Code to be added in this framework]

in file XCDYouTubeVideoPlayerViewController.m, func - (void) stopWithError:(NSError *)error

add this line at the beginning of the func:

if (self.videoIdentifier == nil) {return;}

[Play local video with this framework]

let fileUrl = //get the file url of the video you wanna play let player = XCDYouTubeVideoPlayerViewController()

You have to make sure videoIdentifier is nil while playing local video. The default value is, nil.

In case of reusing the player, make sure set this to nil.

let player.videoIdentifier = nil

player.moviePlayer.contentURL = fileUrl player.moviePlayer.prepareToPlay()