iOS embed video, video still plays when in another view - ios

So lets say I have a UIWebView on my view, embed with a YouTube video. The view is connected to a navigation view controller. This means that when i click a button it pushes a view on to the stack. But when I play the youtube video and go to the next view. The video is still playing. Is there anyway to handle stopping an embed youtube video when going to another view?
What if I have multiple cells with a UIWebView embed with a YouTube video in a UICollectionView? I don't have reference to all the UIWebViews to set to nil. (Sorry for the confusion previously above)

What I do is just set the UIWebView's content to nil when I'm moving to another page, which removes the video player completely, but if you just want to pause the video so the user can resume when they come back, you'll have to talk to the youtube player using javascript. See How to Pause Media Playback in UIWebView for one approach.

If you use Youtube Javascript api to play the video - you can pause it when leaving current view with
-(void)viewWillDisapper:(BOOL)animated
{
[super viewWillDisappear:animated];
[webView stringByEvaluatingJavaScriptFromString: #"player.pauseVideo();"];
}

You need to load a blank page into the UIWebView to stop the video playing. Here's a quick way to do it:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated]
[yourWebView loadHTMLString:nil baseURL:nil];
}

Related

YTPlayerView customize buttons to start stop

I am using YTPlayerView to play YouTube videos inside my app. I need to do some customization that instead of using YTPlayerView button I want to create my own buttons to control the playing view and also I want to handle it by my self that when the video will go to full screen.
I know that we can also use UIWebView. I there is some way that I can do the same thing using UIWebView then I can also do that.
Is there any way that I can do this or YouTube is providing a common way to play videos which we see in many other applications.
So what exactly is your question? Are you looking to create you own buttons to start the video, stop the video, and make the video full screen?
YTPlayerView *playerView = ...
// ...
- (IBAction)playButtonPressed:(id)sender {
[playerView playVideo];
}
- (IBAction)pauseButtonPressed:(id)sender {
[playerView pauseVideo];
}
- (IBAction)stopButtonPressed:(id)sender {
[playerView playVideo];
}
There is a discussion on the GitHub page about playing a video full screen programmatically: https://github.com/youtube/youtube-ios-player-helper/issues/35

How to prevent UIWebView video from getting remote control events

I am using UIWebView in an iOS app to play YouTube videos but to provide native experience, I've implemented playback controls using UIKit. So the UIWebView is only used to display video.
I've also implemented -remoteControlReceivedWithEvent: to allow control from Control Center and controller buttons on earphones. But it seems that UIWebView automatically handles remote control events from the earphones. This is a problem because when you toggle play/pause, my code would pause the video and then UIWebView will toggle it again to play the video.
Is there anyway to prevent this from happening?
Related issue is that UIWebView tries to set "Now Playing" information to MPNowPlayingInfoCenter which is also done by my code.
I encountered same kind of issue with my app which playbacks audio using AVAudioPlayer.
This app displays current audio info in MPNowPlayingInfoCenter during playback.
My requirement was to display an html5 video advert (with audio) inside a webview on top my player.
First i used only UIWebView as i needed to support iOS7 but i met a lot of issues, one of them was MPNowPlayingInfoCenter that displays url of ad video in place of current native audio playback.
I tried several solutions such as method swizzling on UIWebView without any success.
I found only one solution that works for me: use WKWebView by default instead of UIWebView web container. HTML5 video playback will not interact anymore with MPNowPlayingInfoCenter, i had to support also iOS7 so i created a wrapper class to switch between UIWebView (with still the issue) on iOS7 and WKWebView from iOS8 and more.
Hope this helps.
I ran into this question while trying to solve somewhat the reverse problem: I added a UIWebView to an app and remote control events were not working when the UIWeb view was on display, but worked elsewhere in the app.
In my case the solution was to add these 3 methods, which are present on all other view controllers in my app, to the controller of my new UIWebView:
- (void) viewDidAppear:(BOOL)animated {
[self becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self resignFirstResponder];
}
-(void)remoteControlReceivedWithEvent:(UIEvent *)event {
// Logic to handle remote control events
}
From this, I suspect that you can prevent your UIWebView from handling remote control events by doing one of two things:
1. Include logic in the remoteControlReceivedWithEvent to ignore the remote control events you don't want handled.
2. Have your UIWebView resign being the first responder by calling resignFirstResponder in the viewDidAppear method of the controller.

Global Audio Player for tabbed App

after days of research I came to the conclusion that there is no other way than to ask here directly.
I have an tabbed application with several UITableViewControllers inside. When I click on a cell in one of these TableViewControllers there should be played some audio streams. This is working so far using MPMoviePlayerViewController. But when I click on "Done" in the player, the audio stops to play.
What I need is to be able to start the audio track, and when I click "done" it should not stop. Also, there should be a way to get back to the player.
What I was thinking is to make something like a "global view" with an integrated player and all other views in the app should use this global view to play the selected audio.
So, I created an UIView which has a method playAudio that starts playing in a MPMoviePlayerViewController. But what is the way to go to this view from another. Right now I am just calling this method directly and before that I added
[self.navigationController pushViewController:mediaPlayer animated:YES];
but I think I am on the wrong way here. What I want to achieve is to play audio in a player, that stays in background when clicking on "Done" in the player. And this player should be reached from every view (e.g. through a UIButton in each tab bar View) - just like most radio applications and music apps do.
Would appreciate any help.
For this purpose, you should not use MPMoviePlayerViewController. You can create your own audio player with view and controls, or use third party libraries (search on Google there are plenty of them). Good Luck!
You should not use MPMoviePlayerViewController to play audio because it's designed to play a movie. I recommend using AVPlayer to play audio (if you only care about local audio and you're not streaming from the Internet, AVAudioPlayer is OK too.)
When you play your audio with AVPlayer, it will continue to play even when you app goes into the background.
The view hierarchy of your app should look something like this:
Root View Controller
Tab Bar controller
Your view controllers go here
A "Now Playing" view controller
A "now playing" view controller will be a view controller that you can put audio controls and display what is playing to your user. You can make your now playing view controller a property in the app delegate or in your root view controller so that you can access it easily. For instance:
id<UIApplicationDelegate> appDelegate = [[UIApplication sharedApplication] delegate];
[self.navigationController presentViewController:appDelegate.nowPlayingViewController animated:YES completion:nil];
You can add this code to a button for any of your view controllers that needs to display the Now Playing screen.

UINavigationView - Stop a video from playing when back pressed

I have a UINavigationView where I push on a ViewController containing a WebView with a YouTube video. When the video is run, then the back button is pressed on the UINavigationView, the sound of the video continues to play.
How do I stop the video playing in the WebView when it is no longer visible?
Make sure your web view is getting deallocated when the view controller goes away.

Tell video loaded in MPMoviePlayerViewController to stop playing

I've got a navigation view with a table view in it, listing some videos. When a row is selected, it loads MPMoviePlayerViewController and inits it with a video from file URL. When I go back to the table view, the movie is still playing. I tried getting the underlying MPMoviePlayerController and giving it a "pause" message in the viewDidDisappear method, but this doesn't seem to ever get called (NSLog statement in method never appears). So I'm sure there's a simple way to tell MPMoviePlayerController via MPMoviePlayerViewController to stop playing it's movie programmatically, right?
Simply needed to subclass MPMoviePlayerViewController, load the subclass from the table/navigation on selection, then add this to that subclass:
-(void)viewWillDisappear:(BOOL)animated {
[self.moviePlayer stop];
}
You need to register for some notifications. See the 'Notifications' section of the MPMoviePlayerController class reference:
http://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/MPMoviePlayerController/MPMoviePlayerController.html
In particular, register to receive MPMoviePlayerDidExitFullscreenNotification and MPMoviePlayerPlaybackDidFinishNotification and in your method that is called when these notifications are sent, stop the movie from playing by sending the 'stop' message.
But what about when you want to stop downloading the video before it is finished playing. For example, when you go to a different screen.
For me, i try to stop the video when the videoWillDisappear method gets called. Yet, the video still downloads even when the current video is gone!

Resources