How to play youtube/vimeo video within the applica

2019-01-30 05:46发布

I am building an application where I want to play video from url like Youtube, Vimeo, Direct url. I am making custom player using AVPlayer to play a video from a direct url of video clip(like www.abc/play.mp4). But later I faced a huge issue to play youtube & vimeo video. And after searching a lot I found these link where it says without using UIWebview I can't play a Youtube link:

Play YouTube videos with MPMoviePlayerController instead of UIWebView

Playing video from youtube link without UIWebView

So I just used this code:

NSString *youTubeVideoHTML = @"<html><head><style>body{margin:0;}</style></head> <body> <div id=\"player\"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); var player; function onYouTubePlayerAPIReady() { player = new YT.Player('player', { width:\'100%%\', height:'200px', videoId:\'%@\', events: { 'onReady': onPlayerReady } }); } function onPlayerReady(event) { event.target.playVideo(); } </script> </body> </html>";

NSString *html = [NSString stringWithFormat:youTubeVideoHTML, videoId];
self.embedded_player_view.mediaPlaybackRequiresUserAction = NO;

[self.embedded_player_view loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];

Now when I click on youtube/Vimeo video link from tableview its playing the video with the default player i.e. quicktime player. Its not running the video within the UIWebview frame itself. But I want to show the video in the 1st half of the screen i.e my UIWebview frame. Is that possible?

In my app I can see this:

when clicking on red play button I can see the video in full screen in quicktime player like this:

enter image description here enter image description here

But I want to show the video within the same webView frame not via quick time player. It should play like this:

enter image description here

Same does in MusicTube and PlayTube.

Or is there any other way to achieve the same? Any help will be appreciated. Thanks in advance.

8条回答
甜甜的少女心
2楼-- · 2019-01-30 05:53

I used the following method to solve the same problem successfully.

Use youtube-ios-player-helper and add the code:

[_playerView loadWithVideoId:@"DmTzboEqfNk" playerVars:@{
                                                         @"playsinline" : @1
                                                         }];

You can find more vars in Youtube > IFrame API.

查看更多
We Are One
3楼-- · 2019-01-30 05:55

enter image description hereenter image description here

I've used this class just for that.

The video you see inside the UIViewController is playable in its current size.

That's the only code I've used:

UIView *videoContainerView = [[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 200)];
    [self.view addSubview:videoContainerView];        
    XCDYouTubeVideoPlayerViewController *videoPlayerViewController = [[XCDYouTubeVideoPlayerViewController alloc] initWithVideoIdentifier:@"_OBlgSz8sSM"];
    [videoPlayerViewController presentInView:videoContainerView];
    [videoPlayerViewController.moviePlayer play];
查看更多
Luminary・发光体
4楼-- · 2019-01-30 06:00

For Vimeo player you can check this link https://github.com/lilfaf/YTVimeoExtractor it plays the video in real player and if you want to run the video in uiwebview,here is the code for that https://stackoverflow.com/a/15918011/1865424.

Pass you URL of YouTube Video in “urlStr”.

//In .h file
UIWebView *videoView;
// In .m file

videoView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 385)];

[self embedYouTube :urlStr  frame:CGRectMake(0, 0, 320, 385)];

[self.view addSubview:videoView];

// methos to embed URL in HTML & load it to UIWebView

- (void)embedYouTube:(NSString*)url frame:(CGRect)frame

{

NSString* embedHTML = @”\

<html><head>\

<style type=\”text/css\”>\

body {\

background-color: transparent;\

color: white;\

}\

</style>\

</head><body style=\”margin:0\”>\

<embed id=\”yt\” src=\”%@\” type=\”application/x-shockwave-flash\” \

width=\”%0.0f\” height=\”%0.0f\”></embed>\

</body></html>”;



NSString* html = [NSString stringWithFormat:embedHTML, url, frame.size.width, frame.size.height];

if(videoView == nil) {

videoView = [[UIWebView alloc] initWithFrame:frame];

[self.view addSubview:videoView];

}

[videoView loadHTMLString:html baseURL:nil];

}

Courtsey :- http://nanostuffs.com/Blog/?p=641

Hope This Will Help You Out. If this doesn't help you out please check out these links:-

http://blog.softwareispoetry.com/2010/03/how-to-play-youtube-videos-in-your.html

https://gist.github.com/darkredz/5334409

http://maniacdev.com/2012/02/open-source-library-for-easily-playing-a-youtube-video-in-an-mpmovieplayer

查看更多
看我几分像从前
5楼-- · 2019-01-30 06:03

Use this

NSMutableString *html = [[NSMutableString alloc] initWithCapacity:1] ;
[html appendString:@"<html><head>"];
[html appendString:@"<style type=\"text/css\">"];
[html appendString:@"body {"];
[html appendString:@"background-color: transparent;"];
[html appendString:@"color: white;"];
[html appendString:@"}"];
[html appendString:@"</style>"];
[html appendString:@"</head><body style=\"margin:0\">"];
[html appendString:@"<iframe src=\"//player.vimeo.com/video/84403700?autoplay=1&amp;loop=1\" width=\"1024\" height=\"768\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>"];
[html appendString:@"</body></html>"];


[viewWeb loadHTMLString:html baseURL:urlMovie];
查看更多
甜甜的少女心
6楼-- · 2019-01-30 06:05

I definitely suggest youtube_ios_player_helper, "sponsored by" Google.

import youtube_ios_player_helper

class YTViewController: YTPlayerViewDelegate {

    @IBOutlet weak var player: YTPlayerView!

    // MARK: - Public properties -

    var videoID = "k_okcNVZqqI"

    // MARK: - View life cycle -

    override func viewDidLoad() {
        super.viewDidLoad()
        self.player.delegate = self
    }

    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.load()
        self.player.fadeOut()
    }

    /**
     Play video with the relative youtube identifier.
     */
    func load() {
        Utils.showHUD(self.view)
        let options = ["playsinline" : 1]
        self.player.loadWithVideoId(self.videoID, playerVars: options)
    }

    /**
     Stop video playing.
     */
    func stop() {
    }

    // MARK: - YOUTUBE video player delegate -

    func playerViewDidBecomeReady(playerView: YTPlayerView) {
        self.player.playVideo()
    }

    func playerView(playerView: YTPlayerView, didChangeToState state: YTPlayerState) {
        switch state {
        case .Playing:
            self.player.fadeIn(duration: 0.5)
            Utils.hideHUD(self.view)
            print("Started playback")
            break;
        case .Paused:
            print("Paused playback")
            break;
        default:
            break;
        }
    }
}
查看更多
Luminary・发光体
7楼-- · 2019-01-30 06:06

If you wanna play youtube, here's a link to youtube player project on github, It's really helpful. and yes it is possible to play it in your uiwebview, just give the webview the url and tell it to load, it shouldn't open in the default player, as far as i believe at least.

查看更多
登录 后发表回答