There are two video file.When I want to see these two video
1) Only the last video file can play,see in fullscreen mode and also minimise when done button clicked..but first video file can't .
2) After a few time the first video file also see in black screen
- (void)viewDidLoad
{
[super viewDidLoad];
MPMoviePlayerController *moviePlayer;
NSArray *filename=#[#"nissan1",#"nissan5"];
//n![enter image description here][1]issan1,nissan5 are mp4 file
NSURL *fileURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:[filename objectAtIndex:0] ofType:#"mp4"]];
moviePlayer= [[MPMoviePlayerController alloc] initWithContentURL:fileURL1];
[moviePlayer.view setFrame:CGRectMake(5, 50, 100,100)];
moviePlayer.shouldAutoplay = NO;
moviePlayer.repeatMode = MPMovieRepeatModeOne;
moviePlayer.initialPlaybackTime = -1.0;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[moviePlayer prepareToPlay];
[self.view addSubview:moviePlayer.view];
/* second video file*/
NSURL *fileURL2 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:[filename objectAtIndex:1] ofType:#"mp4"]];
moviePlayer= [[MPMoviePlayerController alloc] initWithContentURL:fileURL2];
[moviePlayer.view setFrame:CGRectMake(200, 50, 100,100)];
moviePlayer.shouldAutoplay = NO;
moviePlayer.repeatMode = MPMovieRepeatModeNone;
moviePlayer.initialPlaybackTime = -1.0;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[moviePlayer prepareToPlay];
[self.view addSubview:moviePlayer.view];
}
You should try with two instances. As Cristik pointed out, ARC targets it for deallocation once the method ends and you are using same variable.
Since you asked for sample code, try this:
- (void)viewDidLoad
{
[super viewDidLoad];
MPMoviePlayerController *moviePlayer1;
MPMoviePlayerController *moviePlayer2;
//filename is a name array
NSURL *fileURL1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:[filename objectAtIndex:0] ofType:#"mp4"]];
moviePlayer1= [[MPMoviePlayerController alloc] initWithContentURL:fileURL1];
[moviePlayer1.view setFrame:CGRectMake(5, 50, 100,100)];
moviePlayer1.shouldAutoplay = NO;
moviePlayer1.repeatMode = MPMovieRepeatModeOne;
moviePlayer1.initialPlaybackTime = -1.0;
moviePlayer1.movieSourceType = MPMovieSourceTypeFile;
[moviePlayer1 prepareToPlay];
[self.view addSubview:moviePlayer1.view];
/* second video file*/
NSURL *fileURL2 = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:[filename objectAtIndex:1] ofType:#"mp4"]];
moviePlayer2= [[MPMoviePlayerController alloc] initWithContentURL:fileURL2];
[moviePlayer2.view setFrame:CGRectMake(200, 50, 100,100)];
moviePlayer2.shouldAutoplay = NO;
moviePlayer2.repeatMode = MPMovieRepeatModeNone;
moviePlayer2.initialPlaybackTime = -1.0;
moviePlayer2.movieSourceType = MPMovieSourceTypeFile;
[moviePlayer2 prepareToPlay];
[self.view addSubview:moviePlayer2.view];
}
Related
In my app I use, I loop a video over and over again. It will always show the QuickTime logo at the beginning of the video each time. I have checked, and there isn't anything like that in the video. Thoughts to how I can get rid of that?
NSURL *videoURL = [[NSBundle mainBundle] URLForResource:#"warpspeed" withExtension:#"mov"];
UIView *patternView = [[UIView alloc] initWithFrame:self.view.bounds];
patternView.backgroundColor = [UIColor blackColor];
[self.moviePlayer2.backgroundView addSubview:patternView];
self.moviePlayer2 = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[self.moviePlayer2 setControlStyle:MPMovieControlStyleDefault];
self.moviePlayer2.controlStyle = MPMovieControlStyleNone;
self.moviePlayer2.scalingMode = MPMovieScalingModeAspectFit;
self.moviePlayer2.movieSourceType = MPMovieSourceTypeFile;
[self.moviePlayer2 setAllowsAirPlay:YES];
self.moviePlayer2.view.frame = self.view.frame;
I have a controller where I displays a video using MPMoviePlayerController. And I need to put an image over the video.
I am trying with the following code, but it doesn't show up. What I am missing?
// method to play the video
- (void)playVideoInLoopMode:(BOOL)loop {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"myvideo" ofType:#"m4v"]];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:url];
mp.controlStyle = MPMovieControlStyleNone;
if (loop) {
mp.repeatMode = MPMovieRepeatModeOne;
}
mp.view.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height);
self.player = mp;
[self.view addSubview:self.player.view];
[self.player prepareToPlay];
[self.player play];
}
// method to add the image
- (void) addImageLayer {
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myimage"]];
image.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:image];
}
First I am running the video with the method: [self playVideoInLoopMode:YES] and after 5 seconds I am trying to put the image layer with the method [self addImageLayer];
In my AppDelegate.h I have this code in the didFinishLaunchingWithOptions:
MyViewController *myVC = [[MyViewController alloc] initWithNibName:#"MyView" bundle:nil];
[self.window addSubview:myVC.view];
[self.window makeKeyAndVisible];
Try adding the image view to the mp.view and then u add the mp.view to the general view
for example if the image will be always the same u can add it in your starting code and delete the method to add the view...
// method to play the video
- (void)playVideoInLoopMode:(BOOL)loop
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"myvideo" ofType:#"m4v"]];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:url];
mp.controlStyle = MPMovieControlStyleNone;
if (loop)
{
mp.repeatMode = MPMovieRepeatModeOne;
}
mp.view.frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height);
self.player = mp;
[self.view addSubview:self.player.view];
image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myimage.png"]];
image.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.player addSubview:image];
[self.player prepareToPlay];
[self.player play];
}
I am using MPMoviePlayer to display a video from an external URL onto my iPhone App, however when I run the App a black screen is all that shows.
Here is the URL I am using:
2015-04-27 00:11:29.655 Floadt[21069:2598414] https://scontent.cdninstagram.com/hphotos-xaf1/t50.2886-16/11179443_819874424728492_389701720_n.mp4
Here is my code to try to setup MPMoviePlayer:
if (entry[#"videos"] != nil) {
NSLog(#"There is a Video: %#", entry[#"videos"]);
NSString *urlString = entry[#"videos"][#"standard_resolution"][#"url"];
NSLog(urlString);
NSURL *url = [NSURL URLWithString:urlString];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: url];
[player prepareToPlay];
[player.view setFrame: CGRectMake(10, 65, 299, 299)];
[cell.contentView addSubview: player.view];
player.shouldAutoplay = YES;
[player play];
}
You need to retain your instance to MPMoviePlayerController i.e. as a property or an instance variable. The reference to the movie player is lost if you do not retain.
When we try to load the video from URL initially it will display blank screen only. MPMoviePlayerController will take some time to load the video from url.So we can display first frame of the video till the video loads. For this need to import two frameworks.
1.AVFoundation
2.AssetsLibrary
Using these two we can display first frame of video into UIImageView as follows:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
url=[NSURL URLWithString:#"https://scontent.cdninstagram.com/hphotos-xaf1/t50.2886-16/11179443_819874424728492_389701720_n.mp4"];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
if ([[avAsset tracksWithMediaType:AVMediaTypeVideo] count] > 0)
{
AVAssetImageGenerator *imageGenerator =[AVAssetImageGenerator assetImageGeneratorWithAsset:avAsset];
Float64 durationSeconds = CMTimeGetSeconds([avAsset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
NSError *error;
CMTime actualTime;
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:&actualTime error:&error];
if (halfWayImage != NULL)
{
NSString *actualTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
NSString *requestedTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, midpoint));
NSLog(#"Got halfWayImage: Asked for %#, got %#", requestedTimeString, actualTimeString);
UIImage *img=[UIImage imageWithCGImage:halfWayImage];
_imgVw.image=img;
}
}
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(tapped)];
[_imgVw addGestureRecognizer:tap];
}
-(void)tapped
{
MPMoviePlayerController *movPlayer=[[MPMoviePlayerController alloc] init];
[movPlayer setContentURL:url];
[movPlayer setMovieSourceType:MPMovieSourceTypeFile];
[movPlayer.view setFrame:CGRectMake(0, 0, _imgVw.frame.size.width, 250)];
[movPlayer prepareToPlay];
movPlayer.controlStyle = MPMovieControlStyleNone;
movPlayer.fullscreen = NO;
movPlayer.shouldAutoplay=YES;
[movPlayer setScalingMode:MPMovieScalingModeAspectFill];
[_imgVw addSubview:movPlayer.view];
[movPlayer play];
}
Here i am taking UIImageView view for playing the video. In viewDidLoad i am loading the 1st frame and giving tap gesture to the UIImageView. When i tapped the ImageView then i am playing the video.
System: iOS 7.0
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *audioPath = [[docPaths objectAtIndex:0] stringByAppendingPathComponent:#"test.mp4"];
if (YES == [fileManager fileExistsAtPath:audioPath])
{
MPMoviePlayerController *moviePlayer = nil;
//1
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath:audioPath]];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
[moviePlayer setControlStyle: MPMovieControlStyleEmbedded];
[moviePlayer.view setBackgroundColor:[UIColor clearColor]];
[moviePlayer.view setFrame: CGRectMake(20, 240, 80, 80)];
moviePlayer.shouldAutoplay = NO;
[moviePlayer prepareToPlay];
self.abcd = moviePlayer;
[self.view addSubview: moviePlayer.view];
//2
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath:audioPath]];
[moviePlayer setScalingMode:MPMovieScalingModeAspectFit];
[moviePlayer setControlStyle: MPMovieControlStyleEmbedded];
[moviePlayer.view setBackgroundColor:[UIColor clearColor]];
[moviePlayer.view setFrame: CGRectMake(20, 40, 80, 80)];
moviePlayer.shouldAutoplay = NO;
[moviePlayer prepareToPlay];
self.edf = moviePlayer;
[self.view addSubview: moviePlayer.view];
}
When I init two MPMoviePlayerControllers, why does it only display one? If I only init #1, it's ok, and if only init #2, it's also ok, but if I init both #1 and #2, it only displays #2. Why is this happening? Thanks.
Can you elaborate on what you mean by it will only "display" one? You can add both views, but according to Apple's documentation:
Note: Although you can create multiple MPMoviePlayerController objects and present their views in your interface, only one movie player at a time can play its movie.
source: https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html
EDIT: See Sean's answer, that's probably the issue.
You need to create two different instances of MPMoviePlayerController. What you are doing is creating an instance of MPMoviePlayerController called moviePlayer and then overwriting it when you create the second. You want to create two unique instances, like:
MPMoviePlayerController *moviePlayer1 = nil;
MPMoviePlayerController *moviePlayer2 = nil;
//1
moviePlayer1 = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath:audioPath]];
[moviePlayer1 setScalingMode:MPMovieScalingModeAspectFit];
[moviePlayer1 setControlStyle: MPMovieControlStyleEmbedded];
[moviePlayer1.view setBackgroundColor:[UIColor clearColor]];
[moviePlayer1.view setFrame: CGRectMake(20, 240, 80, 80)];
moviePlayer1.shouldAutoplay = NO;
[moviePlayer1 prepareToPlay];
self.abcd = moviePlayer1;
[self.view addSubview: moviePlayer1.view];
//2
moviePlayer2 = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL fileURLWithPath:audioPath]];
[moviePlayer2 setScalingMode:MPMovieScalingModeAspectFit];
[moviePlayer2 setControlStyle: MPMovieControlStyleEmbedded];
[moviePlayer2.view setBackgroundColor:[UIColor clearColor]];
[moviePlayer2.view setFrame: CGRectMake(20, 40, 80, 80)];
moviePlayer2.shouldAutoplay = NO;
[moviePlayer2 prepareToPlay];
self.edf = moviePlayer2;
[self.view addSubview: moviePlayer2.view];
Another example of what you're doing is this:
NSString *string = nil; // first string is nil
string = #"red"; // then you create another string "red" and set it to string
string = #"blue"; // then you create another string "blue" and set it to string
No matter what you do, after this string will ALWAYS be "blue" and "red" is basically gone.
I have generated that MOV file from the screen shots of the UI and record the sound. Combined both video and audio and generate a MOV formatted movie.
I have seen quite a lot of MPMoviePlayerViewController sample but it just shows me black screen. I tried AVPlayer but I can't get it work.
I'm new to playing movie file in iOS, please help.
Here is my code:
NSString *fileNamePath = mVideo;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *oldappSettingsPath = [documentsDirectory stringByAppendingPathComponent:fileNamePath];
NSURL *path = [NSURL fileURLWithPath:oldappSettingsPath];
self.mPlayer = [[MPMoviePlayerController alloc] initWithContentURL:path];
self.mPlayer.controlStyle = MPMovieControlStyleFullscreen;
self.mPlayer.fullscreen = YES;
self.mPlayer.scalingMode = MPMovieScalingModeFill;
[[self.mPlayer view] setFrame: CGRectMake(0, 0, 480, 320)];
[self.view addSubview:[self.mPlayer view]];
[self.mPlayer prepareToPlay];
[self.mPlayer play];
I have found the fixed to my MPMoviePlayerController :
NSURL *path = [NSURL fileURLWithPath:oldappSettingsPath];
if ([[NSFileManager defaultManager] fileExistsAtPath:oldappSettingsPath]) {
NSLog(#"Exist");
self.mPlayer = [[MPMoviePlayerController alloc] initWithContentURL:path];
self.mPlayer.movieSourceType = MPMovieSourceTypeFile;
self.mPlayer.controlStyle = MPMovieControlStyleFullscreen;
self.mPlayer.fullscreen = YES;
self.mPlayer.scalingMode = MPMovieScalingModeFill;
[[self.mPlayer view] setFrame: CGRectMake(0, 0, 480, 320)];
[self.view addSubview:[self.mPlayer view]];
[self.mPlayer prepareToPlay];
[self.mPlayer play];
} else {
NSLog(#"Don't Exist");
}
I added also:
self.mPlayer.movieSourceType = MPMovieSourceTypeFile;
in my case the player shows black screen since the file i played is not there, so i added a checking first if the file exist or not.