How to init MPMoviePlayerController with video paused - ios

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.

Related

How to play the Video in iphone

NSURL *url="file:///Users/adi/Library/Developer/CoreSimulator/Devices/F7C9D729-6E67-45C3-9234-629460FDD0A4/data/Containers/Data/Application/319F0627-CF3E-43D4-A44F-3E5E797CCCEA/Library/Caches/Attachments/7157783VID_20150602_185133.mp4"
Here's my code:
MPMoviePlayerController * moviePlayer =[[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(10, 100, 300, 200);
[moviePlayer play];
It shows blank screen.
Hope this may help you
#import<MediaPlayer/MediaPlayer.h>
-(IBAction)VideoPlayer:(id)sender
{
NSString *path = [[NSBundle mainBundle]pathForResource: #"video-en-2b" ofType:#"mp4"];
moviePlayer = [[MPMoviePlayerViewControlle alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
Codin[self presentModalViewController:moviePlayer animated:NO];
}
Well if you have to play video using local file use this code.It is helpful for you.
NSString*thePath=[[NSBundle mainBundle] pathForResource:#"yourVideo" ofType:#"mp4"];//7157783VID_20150602_185133 name
NSURL*theurl=[NSURL fileURLWithPath:thePath];
moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:theurl];
[moviePlayer.view setFrame:CGRectMake(10, 100, 200, 300)];
[moviePlayer prepareToPlay];
[moviePlayer setShouldAutoplay:NO]; // And other options you can look through the documentation.
[self.view addSubview:moviePlayer.view];
If you want to control playback process
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playBackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
Try this :
-(void)playVideo : (NSString*)apath
{
NSURL *videoURL =[NSURL fileURLWithPath:apath];
if ([[NSFileManager defaultManager] fileExistsAtPath:apath]) //Does file exist?
{
MPMoviePlayerViewController *videoPlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
videoPlayerView.moviePlayer.movieSourceType=MPMovieSourceTypeFile;
[self presentMoviePlayerViewControllerAnimated:videoPlayerView];
[videoPlayerView.moviePlayer play];
}
}
Note: this method only accept local file path

How to play Video in UIView using URL getting from server in iOS

I want to play video in my UIView from the URL that i got from the JSON Response from server.
This is code that i wrote to play video but nothing happens.
NSURL *movieURL = [NSURL URLWithString:objAlbum.strVideoURL];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[moviePlayerController.view setFrame:self.movieView.bounds];
[self.movieView addSubview:moviePlayerController.view];
// Configure the movie player controller
moviePlayerController.controlStyle = MPMovieControlStyleNone;
[moviePlayerController prepareToPlay];
[moviePlayerController play];
In strVideoURL i'm getting video url.
Check that URL has any spaces. Better to Add below line of code
NSURL *movieURL = [NSURL URLWithString:[objAlbum.strVideoURL stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding]];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[moviePlayerController.view setFrame:self.movieView.bounds];
// Configure the movie player controller
moviePlayerController.controlStyle = MPMovieControlStyleNone;
//[moviePlayerController prepareToPlay];
[moviePlayerController play];
[self.movieView addSubview:moviePlayerController.view];
Note: Print the movie URL and play using browser to check it is valid URL or not..!
Hope it helps you...!
i have solve this using following code
NSURL *vidURL = [[NSURL alloc]initWithString:objAlbum.strVideoURL];
self.movie = [[MPMoviePlayerController alloc]initWithContentURL:vidURL];
self.movie.controlStyle = MPMovieControlStyleDefault;
self.movie.shouldAutoplay = YES;
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
but i want to open it now in UIView defined in TableViewCell like video open in FB(Facebook)

MPMoviePlayerController is functioning wrong

When i tried a video saved on the Xcode project , it worked.
then when i changed it with a url , it always returns this error :
_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}
Here's my code :
NSURL *url = [NSURL fileURLWithPath:#"https://m.youtube.com/watch?v=9MNAeGWd_04"];
movieController = [[MoviePlayerVC alloc]initWithContentURL:url];
movieController.view.frame = self.view.bounds;
[movieController.moviePlayer setShouldAutoplay:YES];
[movieController.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
[[movieController view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
[self presentViewController:movieController animated:NO completion:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackStateDidChange:)
name:#"MPAVControllerPlaybackStateChangedNotification"
object:nil];
fileURLWithPath: expects a local path url - /User/blah/blah.mp3
Try to use [NSURL URLWithString:#"https://m.youtube.com/watch?v=9MNAeGWd_04"]
IF YOU PLAY FROM URL THEN TRY THIS
-(void)playFromLiveUrl
{
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];
}
IF YOU ARE USE LOCAL FILE THEN USE THIS
-(void)playFromLocal
{
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:#"** LOCAL PATH ** "]];
_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];
}
The MPMoviewPlayerController Can't get your format. U r try to load the MPMoviePlayerController with youTube link, it won't work (as u can see :)
Here u can find the iPhone video format support.
Try to load your MoviePlayer something like this (for example .m3u8 format):
[movieController setContentURL:[NSURL URLWithString:#"http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8"]];
MPMoviePlayerController cannot play a Flash Youtube URL. Use a web view instead. UIWebView on iOS is smart enough play youtube content.
You can see the video formats MPMoviePlayer and AVFoundation support here

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

iOS How to play multiple videos on one view

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.

Resources