iOS How to play multiple videos on one view - ios

I was wondering how to play multiple videos in one page, like where you have two buttons, and when you press one, it plays a video, and when you press the other it plays a different one. I have this code so far:
-(void)WelcomeVideo1
{
NSURL *url31 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"DirectorsWelcome" ofType:#"mp4"]];
welcomePlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:url31];
welcomePlayer1.controlStyle = MPMovieControlStyleDefault;
welcomePlayer1.shouldAutoplay = YES;
[self.view addSubview:welcomePlayer1.view];
[welcomePlayer1 setFullscreen:YES animated:YES];
}
-(void) moviePlayBackDidFinish:(NSNotification *)aNotification{
[welcomePlayer1.view removeFromSuperview];
welcomePlayer1 = nil;
}
- (void)moviePlayerWillExitFullscreen:(NSNotification*) aNotification {
[welcomePlayer1 stop];
[welcomePlayer1.view removeFromSuperview];
welcomePlayer1 = nil;
}
-(void)storyVideo1
{
NSURL *url4 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"OurStory" ofType:#"mp4"]];
storyPlayer1 = [[MPMoviePlayerController alloc]
initWithContentURL:url4];
storyPlayer1.controlStyle = MPMovieControlStyleDefault;
storyPlayer1.shouldAutoplay = YES;
[self.view addSubview:storyPlayer1.view];
[storyPlayer1 setFullscreen:YES animated:YES];
}
-(void) moviePlayBackDidFinish2:(NSNotification *)aNotification{
[storyPlayer1.view removeFromSuperview];
storyPlayer1 = nil;
}
- (void)moviePlayerWillExitFullscreen2:(NSNotification*) aNotification {
[storyPlayer1 stop];
[storyPlayer1.view removeFromSuperview];
storyPlayer1 = nil;
}
but whenever I try to play both videos, the 2nd one I play crashes the app. Any ideas?

Use AVPlayer and AVPlayerLayer instead of MPMoviePlayerController.
Take a look into following apple example.

Related

Not able to play video file on iPad using AVPlayerViewController

I have seen exactly the same issue here but its unanswered so asking the same question again.
AVPlayer wont play video from url in iOS9
I tried all various ways to try to play the video but unfortunately they are not working for me.
Here is the code :
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
NSString *url = [[NSBundle mainBundle] pathForResource:#"Intro" ofType:#"mp4"];
_asset = [AVURLAsset assetWithURL: [NSURL URLWithString:url]];
_playerItem = [AVPlayerItem playerItemWithAsset: _asset];
_player = [[AVPlayer alloc] initWithPlayerItem: _playerItem];
[playerViewController.view setFrame:_videoView.frame];
playerViewController.showsPlaybackControls = YES;
[_videoView addSubview:playerViewController.view];
playerViewController.player = _player;
[_player play];
and here is the screen shot of what it looks like currently by using above code. And I used another Apple code to check the video and it worked fine but the Apple code is like too much for me as its too complicated for simple things. Here it is :https://developer.apple.com/library/prerelease/ios/samplecode/AVFoundationSimplePlayer-iOS/
Thanks
AJ
I was finally able to figure this out by again looking into Apple's Sample code.
Solution:
Added the AAPLPlayerView files to my project.
Then changed my UIView in Storyboard to above class
Then updated below lines of code into my view controller class:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.playerView.playerLayer.player = self.player;
NSURL *movieURL = [[NSBundle mainBundle] URLForResource:#"Intro" withExtension:#"mp4"];
self.asset = [AVURLAsset assetWithURL:movieURL];
self.playerItem = [AVPlayerItem playerItemWithAsset: self.asset];
[self.player play];
}
- (AVPlayer *)player {
if (!_player)
_player = [[AVPlayer alloc] init];
return _player;
}
- (AVPlayerLayer *)playerLayer {
return self.playerView.playerLayer;
}
- (AVPlayerItem *)playerItem {
return _playerItem;
}
- (void)setPlayerItem:(AVPlayerItem *)newPlayerItem {
if (_playerItem != newPlayerItem) {
_playerItem = newPlayerItem;
// If needed, configure player item here before associating it with a player
// (example: adding outputs, setting text style rules, selecting media options)
[self.player replaceCurrentItemWithPlayerItem:_playerItem];
}
}
And it worked.
The issue was the PlayerLayer was not set properly and the video was not visible due to that.

How to init MPMoviePlayerController with video paused

I'm developing an app for iOS where it shows a movie, my method is a class method this is the code
Functions.m
static MPMoviePlayerController *moviePlayer = nil;
+(void)playMP4Movie:(NSString *)moviename
insideOfView:(UIView *)view
autoplay:(BOOL)autoplay{
NSString *path = [[NSBundle mainBundle] pathForResource:moviename ofType:#"mp4"];
NSURL *videoURL = [NSURL fileURLWithPath:path];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
moviePlayer.repeatMode = MPMovieRepeatModeOne; // for looping
moviePlayer.view.frame = view.bounds;
[moviePlayer prepareToPlay];
if (autoplay) { [moviePlayer play]; }
else { [moviePlayer stop]; }
[view addSubview:moviePlayer.view];
}
and where I want to call it with video paused:
[Functions playMP4Movie:videoName insideOfView:_videoPlayerView autoplay:NO];
it shows my video but no matter if I set autoplay to YES or NO, the result is always the same... any help I'll appreciate
Try using:
if (autoplay) { [moviePlayer setShouldAutoplay:YES]; }
else { [moviePlayer setShouldAutoplay:NO]; }
And see if you have better luck.

Playing video from server is not working in MediaPlayer Framework

Here i want to play videos from server using MediaPlayer framework. 1)I am adding mediaplayer framework. 2)I am importing #import header file 3)I implemented code by using google.
My Code is :
- (void)act_GoToVideoPlayer:(UIButton*)sender
{
NSString* resourcePath = [NSString stringWithFormat:#"http://%#",[postMediaFileArr objectAtIndex:sender.tag]]; //Video file URL is getting from server and stored it in a MutableArray
NSURL *url = [NSURL URLWithString: resourcePath];
mediaplayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:str]];
[mediaplayer.view setFrame:CGRectMake(20, 100, 250, 150)];
[self.view addSubview:mediaplayer.view];
mediaplayer.fullscreen = YES;
mediaplayer.allowsAirPlay = YES;
mediaplayer.shouldAutoplay = YES;
mediaplayer.controlStyle = MPMovieControlStyleEmbedded;
}
The view goes to blank screen and infinite loading when this function is called. I have tried many other versions of this implementation and the results are all fail. The log in the is particular case is:
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
If i passed any other videos from websites. it will be playing audios not videos.
Got ideas on the cause? This is my first venture into playing video and it has turned out to be a nightmare at this point.
-(void)notifyCompletelyVisible
{
NSURL *aUrl = [NSURL URLWithString:#"*YOUR URL*"];
_moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:aUrl];
_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.view.frame = asyvideoImage.frame;
_moviePlayer.scalingMode=MPMovieScalingModeFill;
[self.view addsubview _moviePlayer.view];
[_moviePlayer prepareToPlay];
[_moviePlayer setFullscreen:NO animated:NO];
}
this is working just import
import "MediaPlayer/MediaPlayer.h"
you try in .h:
#import <MediaPlayer/MediaPlayer.h>
and you add delegate
interface YourViewcontroler : UIViewController <MPMediaPlayback>
{
MPMoviePlayerController *player;
}
and in your .m file:
-(void) launchVideo{
YourAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSString* myURL =#"http://yourUrl/file.mp4";
NSURL *urlString = [NSURL URLWithString:myURL];
player = [[MPMoviePlayerController alloc] initWithContentURL:urlString];
[player view].alpha=1;
[appDelegate.window.rootViewController.view addSubview:[player view]];
[player play];
}
and the delegate methode for finishing:
-(void)videoPlayBackDidFinish:(NSNotification*)notification {
[player.view removeFromSuperview];
[player stop];
player=nil;
[player release];
}
i hope this help you

ipad movie paused rather play when page is loaded

I have a movie that loads fine and plays fine but it plays as soon as the page loads. I would like it to load in a paused state requiring the user to hen press play to watch.
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"iobserve1" ofType:#"mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayerController.view.frame = CGRectMake(60, 44, 900, 656); // player's frame must match parent's
[self.movieView addSubview:moviePlayerController.view];
moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
[moviePlayerController prepareToPlay];
[moviePlayerController pause];
}
The pause on the last line does not seem to do anything. Any help? Thanks
Put your code blurb you've written above in viewwillappear.
Then add play at the end like so:
-(void) viewWillAppear: (BOOL) animated {
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"iobserve1" ofType:#"mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayerController.view.frame = CGRectMake(60, 44, 900, 656); // player's frame must match parent's
[self.movieView addSubview:moviePlayerController.view];
moviePlayerController.controlStyle = MPMovieControlStyleEmbedded;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
}
at the end of it.
In a viewdidappear do a
-(void) viewDidAppear: (BOOL) animated {
[moviePlayerController pause];
}
Remember to remove code from viewDidLoad
You might want to try setting the MPMoviePlayerController property shouldAutoplay to NO.
Add the following line before your prepareToPlay call;
moviewPlayerController.shouldAutoplay = NO;
From the reference:
shouldAutoplay
A Boolean that indicates whether a movie should begin playback automatically.
#property (nonatomic) BOOL shouldAutoplay
Discussion
The default value of this property is YES. This property determines
whether the playback of network-based content begins automatically
when there is enough buffered data to ensure uninterrupted playback.
Availability
Available in iOS 3.2 and later.
Declared In

ShouldAutoplay on MoviePlayerController

I am a new developer, therefore I'm still learning and know I'm doing some things wrong. I have a segmented control object in which when the user presses one of the segments, it goes to play a video. I have it setup to where they press the segment, then have to press a play button to get the video to play. I want to cut out the play button and have it play automatically. This is where I'm having trouble. I found the shouldAutoplay option but when I use it and cut out the button, it won't take me to the video at all. I'm sure I'm not using the shouldAutoplay option correctly. Was hoping for some help or at least a point in the right direction.
- (IBAction)playMovie:(id)sender;
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"mytestimony" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.shouldAutoplay = YES;
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie autorelease];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}
Have you tried [theMovie play];?
UPDATE: I just noticed you set shouldAutoplay in theMovie, but what you presented is another instance of MPMoviePlayerViewController, which is moviePlayer. That's why your movie did not autoplay. You should instead have:
[self presentMoviePlayerViewControllerAnimated:theMovie];
Don't play the video, just prepare it for playing and put the shouldAutoPlay as NO, because you don't want to video player to play it automatically.
-(void) SetVideoFile:(NSString *)fileName {
videoFileName=fileName;
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:fileName]];
moviePlayer.initialPlaybackTime = 0;
moviePlayer.view.frame = self.view.frame ;
[orientationHandler VideoStart];
[self.view addSubview:moviePlayer.view] ;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
moviePlayer.shouldAutoplay = NO;
[moviePlayer prepareToPlay];
}
-(void)moviePlaybackDidFinish:(NSNotification *)notification {
// your custom code which you want to play after the player did finish playing
}

Resources