Play video in app instead of default player - ios

I use the following code to play videos:
MPMoviePlayerViewController *theMovie=
[[MPMoviePlayerViewController alloc] initWithContentURL: movieURL];
theMovie.moviePlayer.repeatMode=MPMovieRepeatModeOne;
[self presentMoviePlayerViewControllerAnimated:theMovie];
This causes a the default movie player to cover the app. Instead I want the video to play inside the app itself as follows:
Can this be achieved?

You can set its frame like this:
theMovie.view.frame = CGRectMake(10, 10, 300, 300);
Then without presenting it add it as subview:
[self.view addSubview: theMovie.view];
Hope this helps.. :)

Yes, this can be achieved by using the AVPlayerLayer. Please see examples here:
"AV Foundation Programming Guide", "Putting It All Together: Playing a Video File Using AVPlayerLayer" section
A tutorial from the iosguy.com
Appuccino's "Playing Movies With AVPlayerLayer In iOS 6" video tutporial

Related

MPMoviePlayerViewController Not playing video by streaming?

I am using the below code to play the video, but its working fine while using the local url that mean resourse file path. Not working for server url.
player = [[MPMoviePlayerViewController alloc] initWithContentURL:urlvalue];
player.view.frame = CGRectMake(10, 10, 800, 800);
player.moviePlayer.shouldAutoplay=YES;
player.moviePlayer.movieSourceType= MPMovieSourceTypeFile;
[player.moviePlayer setControlStyle:MPMovieControlStyleDefault];
[self.view addSubview:player.view];
[player.moviePlayer prepareToPlay];
[player.moviePlayer play];
Kindly correct me if I missed out something. Thanks in advance.
Pay attention on iOS 9 App transport security. If you haven't set it up, streaming will fail with black empty screen.
Change movieSourceType from File to Streaming :
player.moviePlayer.movieSourceType= MPMovieSourceTypeStreaming;
Note : url should be a streaming url not physical file path

iOS Force user to watch a YouTube video

Im using this code to play a commercial video in my iOS application:
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:[NSString stringWithFormat:#"<iframe width=\"%f\" height=\"%f\" src=%# frameborder=\"1\" allowfullscreen></iframe>",frame.size.width-10,frame.size.height-10,urlString] baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
}
I want to force the user to see the video and close the UIWebView once the video is completed without allowing the user to shut the video off.
I think if i can make the Video plays automatically and set the:
videoView.userInteractionEnabled = NO;
that will force the user to watch it.
My Questions:
1- How to force the UIWebView using <iframe> ?
2- How to detect if the video finished playing?
3- Is there any other way (Better One) to play YouTube videos in iOS?
Glad to hear any other ideas or answers, Thanks in advance.
try this:
videoView.userInteractionEnabled = NO;
and you can create a UIButton to close the video.
you can use LBYoutube for youtube videos. see following link
LBYouTube

Control buttons in AVPlayer and frame

I have to play a series of videos in my app. I am using AVQueuePlayer to play the videos using the code:
AVQueuePlayer *queuePlayer = [[AVQueuePlayer alloc] initWithItems:items];
AVPlayerLayer *myPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:queuePlayer];
myPlayerLayer.frame = CGRectMake(0, 0, 320, 350);
[self.view.layer addSublayer:myPlayerLayer];
[self.queuePlayer play];
I am facing two issues.
When the video plays, the frame of player is not according to the given values(i.e. 0, 0, 320, 350).
No control buttons(volume,play,stop, forward etc) are being showed as they are shown when we play video with MPMediaPlayer. Am i missing something for both cases?
(I am using AVFoundation for the first time so these questions may seem stupid).
Thanks in advance.
When the video plays, the frame of player is not according to the
given values(i.e. 0, 0, 320, 350).
Apple's sample code doesn't use -playerLayerWithPlayer:. It creates a UIView subclass whose layer is an AVPlayerLayer. Perhaps there's a reason for that. I do that in my code and it works great. Take a look at the AVPlayerDemo code and try doing it that way.
No control buttons(volume,play,stop, forward etc) are being showed as
they are shown when we play video with MPMediaPlayer. Am i missing
something for both cases? (I am using AVFoundation for the first time
so these questions may seem stupid).
AVPlayer does not provide these for you. That's what MPMoviePlayerController is for. If you want controls and you want to use AVPlayer, you'll need to create and manage the controls yourself.
As an alternative to Dave's answer to your first question...
If using auto layout, you can store, as a local property, a reference to the player's layer returned by playerLayerWithPlayer: and update its frame whenever the view controller's view lays out its subviews -
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.playerLayer.frame = self.view.layer.bounds;
}
Or if you're working in a UIView subclass -
- (void)layoutSubviews
{
[super layoutSubviews];
self.playerLayer.frame = self.layer.bounds;
}
This assumes that you would like the player to fill the view; you can update the code if you have other intentions.

Play Video as the Background on iPhone on button click?

I want to create an app which has a default image as the background of a view that has many buttons on it. When a user clicks on any of the buttons, a unique video plays. The buttons are gonna be almost transparent so, I want the video to play in the background of the same view itself (replacing/playing above the image) and not open in a new Player(as it does using the media player). Has anyone tried it, or does anyone have any idea?
Thank You!
#import <MediaPlayer/MPMoviePlayerController.h>
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath: moviePath]];
moviePlayer.controlStyle = MPMovieControlStyleNone;
[RootViewController.view insertSubview: moviePlayer.view aboveSubview: myView];
[moviePlayer play];

MPMoviePlayerController stops and resets the movie when user tries to play fullscreen [iOS]

I embedded a MPMoviePlayerController on my mail view. I can play/pause the movie and seek forward/backward. But when I touch the "fullscreen button" the movie stops and the playback state is set to MPMoviePlaybackStateStopped... Should the movie be played in full screen?
Here is my code:
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoUrl];
player.shouldAutoplay = NO;
player.movieSourceType = MPMovieSourceTypeFile;
player.controlStyle = MPMovieControlStyleEmbedded;
player.allowsAirPlay = YES;
player.view.frame = CGRectMake(xPos, yPos, width, height);
[self.view addSubview:player.view];
I found my bug: when pressing the full screen toggle button in MPMoviePlayerController's view, the method "viewWillLayoutSubviews" is invoked. I could never imagine this behavior...
I hope my experience can be useful to other developers.
Remember that any containing ViewController will have its viewWillDisappear, viewDidDisappear methods invoked when the MPMoviePlayerController goes full screen. Also, viewWillAppear and viewWillDisappear get called when it comes back from full screen.
If you have any logic in there that affects video playback behavior, it'll get called unless you use some conditional logic to see if the video is still playing.

Resources