For some reason the setcurrentPlayback Method does not work. Thanks for any thoughts...
-(IBAction)abspielen:(id)sender {
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:titleOfButton ofType:#"mov"];
NSURL *movieURL = [ NSURL fileURLWithPath:moviePath];
MPMoviePlayerController *themovie =
[[MPMoviePlayerController alloc]initWithContentURL: movieURL];
[themovie play];
[themovie setCurrentPlaybackRate:2.f];
[themovie release];
MPMoviePlayerViewController *moviePlayer =
[[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
[moviePlayer release];
}
add the line
themovie.rate=2.f;
insted of [themovie setCurrentPlaybackRate:2.f]; line
may be its work....
sometime in use of HTML blog,this method not work so just try given 1 line
May be it is useful to you....
:-)
Related
I got this piece of code:
theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:#"/Resources/disc.mp4"]];
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[theMoviPlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:theMoviPlayer.view];
[theMoviPlayer play];
But i really dont know how to add the video to my project. In which folder do i have to put the video file!? Or do i have to do something else to add it to my project?
EDIT:
It looks like this in xcode, is it correct?
Because i do get a playback error right now.
Previously i used an url to play this video and this worked quite well, but with this file locally not :(
Ok your bundle path looks jacked, below should work.
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"disc" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[theMoviPlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:theMoviPlayer.view];
[theMoviPlayer play];
Add MediaPlayer Framework
import it to your file
#import <MediaPlayer/MediaPlayer.h>
Create an object of MPMoviePlayerController
MPMoviePlayerController * moviePlayer;
write this code where you wants to play video
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"spacetest.mp4" ofType:nil];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[self.view addSubview:moviePlayer.view];
moviePlayer.fullscreen = YES;
[moviePlayer play];
Using HTML5 as I promised above:
NSString *videoTitle = #"disc.mp4";
NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *playPath = [NSString stringWithFormat:#"<center><video width=\"640\" height=\"480\" controls><source src=\"%#\" media=\"all and (max-width:1024px)\"></video></center>",videoTitle];
[webView loadHTMLString:playPath baseURL:baseURL];
This will play in 640x480, but if you are familiar with HTML5 video tags, you can customize pretty heavily.
Since you are using MPMoviePlayerController and not UIWebView you can place your mp4 or file in your Resources and XCode/iOS will find it. Be sure that the directory/group that the file is under is yellow not blue. You don't want it to be a relative path.
Just drag the resource into your project. Copy items into destination is selected, First option of Folders is selected, and most importantly, add to target!
Ok try the code below:
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"disc" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));
UIWindow *backgroundWindow = [[UIApplication sharedApplication] keyWindow];
[theMoviPlayer.view setFrame:backgroundWindow.frame];
[backgroundWindow addSubview:theMoviPlayer.view];
[theMoviPlayer play];
I'm new here. I'm already 2 days browsing for solutions, but unhappy.
Here is my code:
NSString *docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *docaPathFull = [docPath stringByAppendingPathComponent:#"/IMG_0003.m4v"];
self.videoURL = [NSURL fileURLWithPath:docaPathFull];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
UIImage *thumbnail = [player thumbnailImageAtTime:4.0 timeOption:MPMovieTimeOptionExact];
player = nil;
self.imageView.image = thumbnail;
Problem is, i can't make video file visible for MPMoviePlayerController. I can't get thumbnail and I can't play it, just black player and loading infinity.
I did try:
NSString*thePath=[[NSBundle mainBundle] pathForResource:#"IMG_0003" ofType:#"m4v"];
self.videoURLtheurl=[NSURL fileURLWithPath:thePath];
and this
self.videoURL = [[NSBundle mainBundle] URLForResource:#"IMG_0005" withExtension:#"mov"];
and this
NSString *filePath = #"/Users/[user]/Documents/video/IMG_0004.mov";
self.videoURL = [NSURL fileURLWithPath:filePath];
If you have any ideas how to save my life, please do!!!
Many thanks for you time.
Actually this is my working example from my project. Make sure you added your m4v file to your project. The notification was a need for me, maybe it is not necessary for you.
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:
[NSURL fileURLWithPath: [[NSBundle mainBundle]
pathForResource:#"yourmovie" ofType:#"m4v"]]];
theMovie.view.frame = CGRectMake(0, 0, 320, 480);
[theMovie setControlStyle:MPMovieControlStyleNone];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[self.view addSubview:theMovie.view];
[theMovie play];
You can use following method to get a thumbnail image for a given Video Url,
-(UIImage *) giveThumbofVideoUrl:(NSURL *) url{
NSString *urlString = [url absoluteString];
AVAsset *asset = [AVAsset assetWithURL:url];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc]initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
//imageGenerator.maximumSize = CGSizeMake(320,480);
CMTime time = CMTimeMake(1, 1);
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:time actualTime:NULL error:NULL];
UIImage *image = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return image;
}
How you can use it ?,
NSString*thePath=[[NSBundle mainBundle] pathForResource:#"IMG_0003" ofType:#"m4v"];
NSURL *movieUrl=[NSURL fileURLWithPath:thePath];
UIImage *thumbnail = [self giveThumbofVideoUrl:movieUrl];
MPMoviePlayerController is Deprecated Now. So I have used AVPlayerViewController. and written the following code:
NSURL *videoURL = [NSURL fileURLWithPath:filePath];
//filePath may be from the Bundle or from the Saved file Directory, it is just the path for the video
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
//[playerViewController.player play];//Used to Play On start
[self presentViewController:playerViewController animated:YES completion:nil];
Please do not forget to import following frameworks:
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
Hope this Helps..
I am using the following code to play the video using the MPMoviePlayerController , but the video is not played. Can anyone tell me why ?
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"/one.mp4"];
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
[[moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [moviePlayer view]];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[moviePlayer play];
It's pretty weird, but it seems to work okay if you make your MPMoviePlayerController a property instead of a local variable. Seems something is going on behind the scenes. I'm thinking it's related to ARC. Are you using ARC?
It's also an issue that you've over-appended your path:
// You've already got the full path to the documents directory here.
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"/one.mp4"];
// Now you're appending the full path to the documents directory to your bundle path
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
When I run your code in the simulator, the path looks like this:
/Users/mlong/Library/Application Support/iPhone
Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Users/mlong/Library/Application Support/iPhone
Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
It should just be this:
/Users/mlong/Library/Application Support/iPhone
Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/Documents/one.mp4
So just delete this line:
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath];
And then change your player instantiation to this:
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];
[[_moviePlayer view] setFrame:[[self view] bounds]];
[[self view] addSubview: [_moviePlayer view]];
_moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[_moviePlayer play];
So you should add the MPMoviePlayerController as a property of your containing view controller.
All right, there is a big difference between the app bundle and the documents directory. I suggest you take a look at that.
First of all, Where is the video stored?
If your video is in the documents directory, don't append the documents directory path to the bundle path.
Just try with the filePath variable:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"/one.mp4"];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL filePath]];
However, if the file is in the app bundle (you added it to your project in XCode), you should use what is in jinx response.
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer.view setFrame:CGRectMake(//set rect frame)];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay=YES;
moviePlayer.repeatMode = NO;
[moviePlayer setFullscreen:YES animated:NO];
[moviePlayer prepareToPlay];
[self.view addsubview:movieplayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(MPMoviePlayerLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification {
if ((moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) {
//add your code
}
}
Try asking your bundle directly and not setting up the file path manually
NSString *path = [[NSBundle mainBundle] pathForResource:#"name" ofType:#"mov"];
Player is initialized with the URL of the video which we want to be played (It can be either a path of local file of a device or a live URL.) after that player is added as a sub view of current view.
Supported video formats by MPMoviePlayerController class are following
.mov .mpv .3gp .mp4
I am not sure how much you this article would help you out. I am new to this. I worked on the step by step instructions provided in this article
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
hope Help it
It took me few mins to debug the problem but the answer is quite simple. Here is the deal:
If you want to MPMoviePlayerViewController to play from a web URL use this:
NSURL *url = [NSURL URLWithString:#"https://www.test.com/anyMovie.mp4"];
And if you want MPMoviePlayerViewController to play it from App Bundle use this:
NSString *moviePath = [[NSBundle mainBundle] pathForResource:#"anyMovie" ofType:#"m4v"];
NSURL *url = [NSURL fileURLWithPath:moviePath];
The Rest of it goes the same, except you have to set this property "movieSourceType" as follows:
MPMoviePlayerViewController *moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
moviePlayerView.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[self presentViewController:moviePlayerView animated:YES completion:^{}];
I need your help please: how can I delete this wihte line?
NSString *AppFolderPath = [[NSBundle mainBundle] resourcePath];
NSString *MoviePath=[NSString stringWithFormat: #"/%#/%#", AppFolderPath,pathVideo];
NSLog(MoviePath);
NSURL *movieURL=[[NSURL fileURLWithPath:MoviePath]retain];
movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[self addSubview:movieController.view];
[movieController setMovieControlMode:MPMovieScalingModeAspectFit];
[movieController setShouldAutoplay:YES];
[movieController setFullscreen:YES animated:YES];
Actually I have also tried with same code. (Except that, setMovieControlMode: is removed since it was deprecated). But I 'm not getting that kind of line. Pl. try with another video.
I am already using the MediaPlayer framework but the media player can only be initialized through a URL. I have my video saved in my project, so how can I play it?
URLs can be files paths too (file://):
NSURL *movieURL = [[NSBundle mainBundle] URLForResource:#"movie" withExtension:#"mp4"];
You can also play your video in UIWebview.
CGRect webFrame = CGRectMake(0.0, 0.0, 1, 1);
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor whiteColor]];
webView.scalesPageToFit =YES;
webView.autoresizingMask =YES;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test123" ofType:#"mp4"]isDirectory:NO]]];
[self.view insertSubview:webView atIndex:0];
I am trying to play video inside a UIView, so my first step was to add a class for that view and start playing a movie in it using this code:
- (IBAction)movie:(id)sender{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Movie" ofType:#"m4v"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie play];
}