Play 2 videos from url one after another - iphone

In my application i want to play 2 url videos one after another.
This is my code:
`- (void)viewDidLoad {
NSLog(#"viewDidLoad");
player = [[MPMoviePlayerController alloc] initWithContentURL:[self movieURL]];
[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player play];
[super vieDidLoad];
}
- (NSURL *)movieURL {
return [NSURL URLWithString: #"https://s3.amazonaws.com/adplayer/colgate.mp4"];//First video url after this video complete.I want to play the next url video.
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
NSLog(#"movieFinishedCallback");
player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player autorelease];
}
`
After one url video completed i want to play the next url video.
please anyone help me.
I have store the url's in array like this,
array=[[NSArray alloc] initWithObjects:#"https://s3.amazonaws.com/adplayer/colgate.mp4",#"https://s3.amazonaws.com/ventuno-platform-flv-sep2010/happy_family.mp4",nil];
Then how can i retrive the url in - (void) movieFinishedCallback:(NSNotification*) aNotification {
}method.please give me guidance in this.

I a have not tested it, but is should work, You might need it modify it little bit. At least, you now have an idea, how this can be done
-(void)viewDidLoad
{
[self initializPlayer];
}
static int i;
-(void)initializPlayer
{
if(i<=[arrMovieURL count])
i +=1;
else {
i = 0;
}
if(player)
{
[player release];
player = nil;
}
player = [[MPMoviePlayerController alloc] initWithContentURL:[arrMovieURL objectAtIndex:i]];
[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player play];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
NSLog(#"movieFinishedCallback");
player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//calling again to play the next video
[self initializPlayer];
}

Related

Video from web cam is not streaming in iPhone/Objective c

I am trying this code but it shows a still image, and not proper video streaming. However it shows the video, but only for some time.
[reykjavikurtjorn loadRequest:[NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://213.167.154.114/mila/_definst_/tjornin.stream/playlist.m3u8"]]];
[super viewDidLoad];
NSURL *url = [NSURL fileURLWithPath:#"http://www.domain.com/myVideo.mp4"];
MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self presentMoviePlayerViewControllerAnimated:mpvc];
and the delegate
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
if ([player respondsToSelector:#selector(setFullscreen:animated:)]) {
[player.view removeFromSuperview];
}

Issue Playing sound from url

I am making an Iphone app which have requirement to play sound in a loop. The sound will come from HTTP links like follows:
Link 1
Link 2
Link 3
Link 4
Link 5
Link 6
Link 7
Link 8
Actually there are three button loop,play all and stop. I am able to do play once click all and loop but When i click other button app will crash.How can i solve this problem.My code is below resultArrayFinal is a globle array where all above sound link store.
-(void)replayVedio
{
DataClass *obj=[DataClass getInstance];
NSURL *url = [NSURL URLWithString:obj.soundInfo];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer setControlStyle:MPMovieControlStyleDefault];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer prepareToPlay];
[moviePlayer play];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
if(_globle==1)
{
[self replayAudio];
}
else if(_globle==2)
{
DataClass *obj=[DataClass getInstance];
obj.soundInfo=[resultArrayFinal objectAtIndex:count++];
if (count==[resultArrayFinal count])
count=0;
[self replayAudio];
}
}
Thanks.

MPMoviePlayerViewController quits while playing the video

I am recording the video in my application. I am saving that video inside Documents directory. After taking the video if I try to play the video using the following code. The player opens and quits in one seconds. However if I Quit and then open my app fresh the video I took earlier is playing.
+ (void) playMovieAtURL: (NSURL*) theURL :(id)sender{
NSLog(#"playMovieAtURL");
//senderID = (id *)sender;
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) {
NSLog(#"> 3.2");
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:theURL];
if (mp)
{
// save the movie player object
//self.moviePlayerViewController = mp;
//[mp release];
[sender presentMoviePlayerViewControllerAnimated:mp];
//mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[mp.moviePlayer play];
}
[mp release];
}
else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2) {
NSLog(#"< 3.2");
MPMoviePlayerController* theMovie = [[MPMoviePlayerController alloc] initWithContentURL: theURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: #selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: theMovie];
// Movie playback is asynchronous, so this method returns immediately.
[theMovie play];
[theMovie release];
}
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
MPMoviePlayerController *aMoviePlayer = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:aMoviePlayer];
// If the moviePlayer.view was added to the view, it needs to be removed
if ([aMoviePlayer respondsToSelector:#selector(setFullscreen:animated:)]) {
[aMoviePlayer.view removeFromSuperview];
}
}
This is the code I use for storing the video inside documents.
NSURL *videoURL = [imageInfo objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
self.itsVideoName = fileName;
[webData writeToFile:[NSString stringWithFormat:#"%#/%#",dataPath,fileName] atomically:TRUE];
I finally figured out the answer after few hours of searching. This is the code I am using now..
-(IBAction)playMovie:(NSString *) theURL
{
NSURL *fileURL = [NSURL fileURLWithPath:theURL];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
//After putting the following line the problem solved.
moviePlayerController.useApplicationAudioSession = NO;
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}
the code moviePlayerController.useApplicationAudioSession = NO; is used to solve this problem. I am not sure how this problem solved I guess I am using session for audio I guess that was causing problem to this.
Also the code which I originally posted was taking lots of memory like if I opened 2 video it cost 104 mb allocation for me. This new code was perfect and it is taking only less memory. Hope this will help for some one..
*NSURL fileURL = [NSURL fileURLWithPath:theURL];
should use NSURL *fileURL = [NSURL URLWithString:theURL];

MPMoviePlayer-Notification doesn´t get called

HI,
i want to play a movie using MPMoviePlayerViewController and listen to a MPMoviePlayerPlaybackDidFinishNotification, but it doesn´t get called. Any ideas on that?
Would be helpful to get any hints on that.. Thanks you
Here´s my code:
NSString *url = [[NSBundle mainBundle] pathForResource:[sender contentName] ofType:#"m4v"];
MPMoviePlayerViewController* moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url] ];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
moviePlayerController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
[moviePlayerController.moviePlayer play];
}
- (void) moviePlayBackComplete:(NSNotification*) notification {
NSLog(#"moviePlayBackComplete complete");
MPMoviePlayerController* moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self dismissMoviePlayerViewControllerAnimated];
[moviePlayerController release];
}
You're trying to get notifications from a MPMovePlayerViewController but I'm pretty sure only MPMoviePlayerController provides them. Try and change your notification setup
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(moviePlayBackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController.moviePlayer];

MPMoviePlayerController release problem

here is code
- (void)applicationDidFinishLaunching:(UIApplication *)application {
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"sample_mpeg4" ofType:#"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.movieControlMode = MPMovieControlModeHidden;
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *theMovie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie stop];
[theMovie release];
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
}
so my problem is my apps memory usage goes additional 3MB , it stays there even after release,does that mean memory does not get released?
Look at your code in
- (void) moviePlaybackDidFinish:(NSNotification*)notification
MPMoviePlayerController *theMovie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
Are you sure "theMovie" is your created "moviePlayer"? I believe they are different memory address because you didn't assign a object when you register the notification. Make sure
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
Then try it again.