How to browse a video from video library in iphone - iphone

i have video application where i want to Browse video from video library.Can anybody please help me in solving this problem on how to browse for particular video from video library in iphone.Thanks

Use UIImagePickerController:
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.mediaTypes = [NSArray arrayWithObjects: (NSString *) kUTTypeMovie, nil];
picker.delegate = self; // don't forget implement UINavigationControllerDelegate, UIImagePickerControllerDelegate methods
[picker presentModalViewController: cameraUI animated: YES];
Read Camera Programming Topics for more details

Here is our .h file:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface AudioAndVideoViewController : UIViewController{
#public
}
MPMoviePlayerController *moviePlayer;
#property (nonatomic, retain) MPMoviePlayerController *moviePlayer;
- (IBAction) startPlayingVideo:(id)paramSender;
- (IBAction) stopPlayingVideo:(id)paramSender;
#end
- (IBAction) startPlayingVideo:(id)paramSender{
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *urlAsString = [mainBundle pathForResource:#"Sample" ofType:#"m4v"];
if (self.moviePlayer != nil)
{
[self stopPlayingVideo:nil];
}
MPMoviePlayerController *newMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.moviePlayer = newMoviePlayer; [newMoviePlayer release]; if (self.moviePlayer != nil){
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(videoHasFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
NSLog(#"Successfully instantiated the movie player.");
self.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
[self.moviePlayer play];
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
}
else {
NSLog(#"Failed to instantiate the movie player.");
}
}
- (IBAction) stopPlayingVideo:(id)paramSender
{
if (self.moviePlayer != nil){
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
[self.moviePlayer stop];
if (self.moviePlayer.view != nil && self.moviePlayer.view.superview != nil &&[self.moviePlayer.view.superview isEqual:self.view] == YES)
{
[self.moviePlayer.view removeFromSuperview];
}
}}

Related

MPMoviePlayerController is not playing video

I am using image matcher sdk with MPMoviePlayerController to play video after matched.
In NSLog video is process but not play.
Here .m file`
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize moviePlayer;
- (void)viewDidLoad
{
[super viewDidLoad];
_cvView = [[ImageMatcher alloc] initWithAppKey:#"apikey" useDefaultCamera:TRUE];
_cvView.matcherDelegate = self;
[_cvView start];
[_cvView setEnableMedianFilter:YES];
[_cvView setImagePoolMinimumRating:0];
[_cvView setMatchMode:matcher_mode_All];
bool value = [_cvView addImage:[UIImage imageNamed:#"pic2.jpeg"] withUniqeID:[NSNumber numberWithInt:20]];
}
-(void) viewDidAppear:(BOOL)animated
{
[self presentViewController:_cvView animated:YES completion:nil];
}
-(void) imageRecognitionResult:(int)uId
{
if (uId == 20) {
[self startPlayingVideo2:nil];
NSLog(#"ID = %d",uId);
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void) startPlayingVideo1:(id)paramSender{
NSLog(#"function startPlayingVideo make");
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *urlAsString = [mainBundle pathForResource:#"test1" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:urlAsString];
if(self.moviePlayer != nil){
NSLog(#"if 1 make");
[self stopPlayingVideo:nil];
}
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
if(self.moviePlayer != nil){
NSLog(#"if 2 make");
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(videoHasFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
moviePlayer.view.frame = CGRectMake(0, 0, 1024, 760);
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
[self.moviePlayer play];
}else{
NSLog(#"fail");
}
}
-(void) startPlayingVideo2:(id)paramSender{
NSLog(#"function startPlayingVideo make");
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *urlAsString = [mainBundle pathForResource:#"sample_mpeg4" ofType:#"mp4"];
NSURL *url = [NSURL fileURLWithPath:urlAsString];
if(self.moviePlayer != nil){
NSLog(#"if 1 make");
[self stopPlayingVideo:nil];
}
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
if(self.moviePlayer != nil){
NSLog(#"if 2 make");
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(videoHasFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
moviePlayer.view.frame = CGRectMake(0, 0, 1024, 760);
[self.view addSubview:self.moviePlayer.view];
[self.moviePlayer setFullscreen:YES animated:YES];
[self.moviePlayer play];
}else{
NSLog(#"fail");
}
}
-(void) stopPlayingVideo:(id)paramSender{
if (self.moviePlayer != nil) {
NSLog(#"stopPlayingVideo on");
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
[self.moviePlayer stop];
if([self.moviePlayer.view.superview isEqual:self.view]){
[self.moviePlayer.view removeFromSuperview];
}
}
}
-(void) videoHasFinishedPlaying:(NSNotification *)paramNotification{
NSNumber *reason = [paramNotification.userInfo valueForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
if (reason != nil) {
NSInteger reasonAsInteger = [reason integerValue];
switch (reasonAsInteger){
case MPMovieFinishReasonPlaybackEnded:{
NSLog(#"MPMovieFinishReasonPlaybackEnded");
break;
}
case MPMovieFinishReasonPlaybackError:{
NSLog(#"MPMovieFinishReasonPlaybackError");
break;
}
case MPMovieFinishReasonUserExited:{
NSLog(#"MPMovieFinishReasonUserExited");
break;
}
}
NSLog(#"finish reason = %ld", (long)reasonAsInteger);
[self stopPlayingVideo:nil];
}
}
#end
`
Add prepareToPlay in your code (Prepares a movie player for playback. (required))'
[self.moviePlayer prepareToPlay];
[self.moviePlayer play];
To prepare a new movie player for playback, call the prepareToPlay method, described in MPMediaPlayback Protocol Reference.

How to record video of app screen

I have created a streaming video app. and now i want to record that video which is playing in MPVideoPlayer,also its sound. I have tried this code. but this code is only record black screen and no sound. I am adding MPVideoPlayer instance dynamically. so , plz help me to do this any source code or library will be great help.
Try this :
.h file
import MediaPlayer/MediaPlayer.h
NSString *strSelectedVideoPath;
MPMoviePlayerController *player
#property (nonatomic,retain) NSString *strSelectedVideoPath;
#property (nonatomic,retain) MPMoviePlayerController *player;
.m file
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:strSelectedVideoPath]];
player.scalingMode = MPMovieScalingModeFill;
player.movieSourceType = MPMovieSourceTypeFile;
player.view.frame = CGRectMake(0, 45, 320, 400);
player.shouldAutoplay = YES;
[player prepareToPlay];
[self.view addSubview:player.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[player play];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player1 = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player1];
[player1.view removeFromSuperview];
player1 = nil;
}
You have to add MediaPlayer.framework
For Record video :
-(void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:NO];
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey: UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (moviePath,self,#selector(video:didFinishSavingWithError:contextInfo:), nil);
}
}
}
- (void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo {
if (error) {
}
else{
NSURL *videoURl = [NSURL fileURLWithPath:videoPath];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURl options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
generate.appliesPreferredTrackTransform = YES;
NSError *err = NULL;
//For Thumb Image
CMTime time = CMTimeMake(1, 60);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
self.strUploadVideoPath = videoPath;
CGImageRelease(imgRef);
[generate release];
[asset release];
}
}

How to dismiss MPMoviePlayerController after stopping video

I have successfully stopped a video within 30 seconds. But I am not able to dismiss the MP MovieViewController and I want to stop activity for buffering. I have used this code.....
Video Play Code :
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Pungi" ofType:#"mp4"]];
self.movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
self.movie .controlStyle = MPMovieControlStyleEmbedded;
[ self.movie play];
[self.view addSubview:self.movie.view];
[self.movie setFullscreen:YES animated:YES];
self.movie.initialPlaybackTime = 0.5;
[NSTimer scheduledTimerWithTimeInterval:15.0
target:self
selector:#selector(stopVideo)
userInfo:nil
repeats:NO];
stopVideo :
[self.movie stop];
[self.movie.view removeFromSuperview];
[self.movie release];
Put your MPMoviePlayerController in a separate class and load it:
MoviePlayerViewController.h
#import <MediaPlayer/MediaPlayer.h>
#interface MoviePlayerViewController : UIViewController
#end
MoviePlayerViewController.m
#import "MoviePlayerViewController.h"
MPMoviePlayerViewController *movieController;
#interface MoviePlayerViewController ()
#end
#implementation MoviePlayerViewController
- (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(#"willEnterFullscreen");
}
- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(#"enteredFullscreen");
}
- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(#"willExitFullscreen");
}
- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(#"exitedFullscreen");
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
case MPMovieFinishReasonPlaybackEnded:
[self dismissModalViewControllerAnimated:YES];
break;
case MPMovieFinishReasonPlaybackError:
break;
case MPMovieFinishReasonUserExited:
[self dismissModalViewControllerAnimated:YES];
break;
default:
break;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
NSString *videoName = #"Videoname";
NSString *filepath = [[NSBundle mainBundle] pathForResource:videoName #"movietype"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:fileURL];
[movieController.view setFrame:CGRectMake(0, -20, 320, 480)];
[self.view addSubview:movieController.view];
[movieController.moviePlayer play];
}
ViewController.m
MoviePlayerViewController *player = [[MoviePlayerViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:player animated:YES];
What it does:
MoviePlayerViewController is a custom class which loads an MPMoviePlayerController with a Video. in the viewDidLoad method (or wherever you want it) you LOAD the MoviePlayerViewController.
([self presentModalViewController:animated])....
This has the advantage, that your main class isn't overloaded with moviecrap/definitions and you can EASILY dismiss the MoviePlayerViewController when it is finished using Notifications to check wether it has stopped or not.
if it has stopped:
[self dismissModalViewControllerAnimated:YES];
Hope this Helps!
As code below shows, you should pause and set initialPlaybackTime to -1 before actual stop. This is one of tricky things that MPMoviePlayerController provides.
[_moviePlayerController pause];
if ([_moviePlayerController isKindOfClass:[MPMoviePlayerController class]]) {
((MPMoviePlayerController*)_moviePlayerController).initialPlaybackTime = -1;
}
[_moviePlayerController stop];
if ([_moviePlayerController isKindOfClass:[MPMoviePlayerController class]]) {
((MPMoviePlayerController*)_moviePlayerController).initialPlaybackTime = -1;
}
[_moviePlayerController.view removeFromSuperview];
You need to add notification observer in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayerPlaybackDidFinishNotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
Then add the method:
- (void)moviePlayerPlaybackDidFinishNotification:(NSNotification*)notification
{
[self dismissMoviePlayerViewControllerAnimated];
}
Import the some headers in your header:
#import <MediaPlayer/MediaPlayer.h>
#import <MediaPlayer/MPMoviePlayerViewController.h>
You may also need to balance your "presentMoviePlayer" call with the dismiss somewhere:
[self dismissMoviePlayerViewControllerAnimated];
if you are finished with the resource early, you may be able to release it sooner by using NotificationManager to watch for MPMoviePlayerPlaybackDidFinishNotification.
and also
- (void)dealloc {
[movie release],
movie = nil;
[super dealloc];
}
try this.......
self.movie.initialPlaybackTime = -1;
[self.movie stop];
[self.movie release];

How can i play video by getting Video from PhotoLibrary

I am doing an app on ipad,Which will capture video and it will save in photo Library. But what i want is i want to play that video choosing from the photo library and need to play over there.I saw many examples using MPMoviePlayerController but all i saw is they adding video over there and they playing that video.Is there any way to write path for my below mentioned code.
My code goes here
This where i calling Photo library
-(IBAction) goToPhotos:(id)sender {
ipc = [[UIImagePickerController alloc] init];
ipc.delegate = self;
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.allowsEditing = YES;
UIPopoverController *videoController = [[UIPopoverController alloc]initWithContentViewController:ipc];
// pop.popoverContentSize = CGSizeMake(300, 900);
[videoController presentPopoverFromRect:[sender frame] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
[ipc release];
[self presentModalViewController:ipc animated:YES];
}
Here i am calling MPMoviePlayerController .Code goes here
- (void)viewDidLoad
{
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Stock_Footage_Demobroadband"
ofType:#"mp4"];
MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:[playerViewController moviePlayer]];
[self.view addSubview:playerViewController.view];
//---play movie---
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
[super viewDidLoad];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification
{
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self.view removeFromSuperview];
[player autorelease];
}
Can any one tell me how can i get path for photolibrary and i need to play video over there.
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Stock_Footage_Demobroadband"
ofType:#"mp4"];
Can we modify this line and is there any way that i can get path to photo library so that i can play video over there. Help me Thanks!!
You need to implement the delegate methods for UIImagePickerController.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *movieURL = [info objectForKey:UIImagePickerControllerMediaURL];
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
//finish implementing the movie player controller
}

how to add video to app

I'm trying to make an app that plays a video, and I'm having problems. I hear the sound but see no video. I have sincerely tried looking around for a solution, but all the tips don't seem to work. Here's the code:
VideoTestViewController.m
#import "VideoTestViewController.h"
#implementation VideoTestViewController
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
NSString *moviePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"spitfiregrill_iPhone.m4v"];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
theMovie.controlStyle = MPMovieControlStyleDefault;
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
[theMovie play];
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
VideoTestViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface VideoTestViewController : UIViewController {
}
#end
- (void)viewDidLoad {
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Stock_Footage_Demobroadband"
ofType:#"mp4"];
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//---play partial screen---
player.view.frame = CGRectMake(184, 200, 400, 300);
[self.view addSubview:player.view];
//---play movie---
[player play];
[super viewDidLoad];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self.view removeFromSuperView];
[player autorelease];
}
Video in full screen mode -
- (void)viewDidLoad {
NSString *url = [[NSBundle mainBundle]
pathForResource:#"Stock_Footage_Demobroadband"
ofType:#"mp4"];
MPMoviePlayerViewController *playerViewController =
[[MPMoviePlayerViewController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:[playerViewController moviePlayer]];
[self.view addSubview:playerViewController.view];
//---play movie---
MPMoviePlayerController *player = [playerViewController moviePlayer];
[player play];
[super viewDidLoad];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player stop];
[self.view removeFromSuperView];
[player autorelease];
}
A bit of guesswork - but this is what I'd do:
VideoTestViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface VideoTestViewController : UIViewController
{
MPMoviePlayerController* m_player;
}
- (void) play;
#end
VideoTestViewController.m
#implementation VideoTestViewController
- (void) viewDidAppear:(BOOL)animated
{
[self play];
}
- (void) play
{
NSURL* url = [[NSBundle mainBundle] URLForResource:#"spitfiregrill_iPhone" withExtension:#"m4v"];
m_player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[m_player.backgroundView setBackgroundColor:[UIColor blackColor]];
[m_player.view setBackgroundColor:[UIColor blackColor]];
[m_player setControlStyle:MPMovieControlStyleNone];
[[m_player view] setFrame:[self.view bounds]];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[m_player play];
[self.view addSubview:[m_player view]];
}
- (void) moviePlayBackDidFinish:(NSNotification*)_notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[m_player.view removeFromSuperview];
[m_player stop];
[m_player release];
m_player = nil;
}
#end