Playing video in iPad application from server - iphone

I want to play a video which is uploaded to the server from an iPad application, but when the screen loads it gives an error:
An AVPlayerItem cannot be associated with more than one instance of AVPlayer
I am using the following code:
-(void)playVideo{
NSURL *url = [NSURL URLWithString:#"http://celeritas.com.pk/emrapp/test.mp4"];
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mp];
mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
[self presentMoviePlayerViewControllerAnimated:mp];
[mp release];
NSLog(#"Successfully playing thanks");
}
-(void)playbackFinishedCallback:(NSNotification *)notification{
MPMoviePlayerController *movie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:movie];
[movie release];
}

http://celeritas.com.pk/emrapp/test.mp4 the URL seems to be wrong...
The requested URL /emrapp/test.mp4 was not found on this server.
Any way this will work
.h
MPMoviePlayerViewController * plyr;
NSURL * url;
#property (nonatomic,retain) MPMoviePlayerViewController *plyr;
#property (nonatomic,retain) NSURL *url;
.m
#synthesize plyr ;
#synthesize url;
url = [NSURL URLWithString:#"valid url"];
plyr = [[MPMoviePlayerViewController alloc] initWithContentURL:url];

Url might be wrong double check that video url link is accessible, secondly do see your network firewall this might also cause problem.
The one tweak you can change instead of:
mp.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
use:
mp.moviePlayer.movieSourceType = MPMovieSourceTypeUnknown;

try this
create object of MPMoviePlayerViewController in .h like MPMoviePlayerViewController *mp;
#import<MediaPlayer/MediaPlayer.h>
mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:url]];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setUseApplicationAudioSession:NO];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
[[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
[self presentMoviePlayerViewControllerAnimated:mp];

try this Code
-(IBAction)playMovie:(id)sender
{
NSURL *fileURL = [NSURL URLWithString:#"http://www.youtube.com/watch?v=3Qjh56woQMw"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
MPMoviePlayerController *moviePlayerController = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotificationobject:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
[moviePlayerController release];
}

Related

Vimeo on iOS5 using MPMoviePlayerController won't load video

The following code loads a Vimeo vide just fine in a webview, but the user has to click the play
button to start
VidViewController *webScrollView =
[[VidViewController alloc] initWithNibName:#"VidViewController" bundle:nil];
NSURL *url = [NSURL URLWithString:#"http://player.vimeo.com/video/36120576"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
webScrollView.request = request;
[self.navigationController pushViewController:webScrollView animated:YES];
[webScrollView release];
I would like to get MPMoviePlayerController to work, but can't get the video to load.
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *audioURL = [NSURL URLWithString:#"http://player.vimeo.com/video/36120576"];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc]
initWithContentURL:audioURL];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player play];
}
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player autorelease];
}
There isn't much support on developer.apple

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];

Video won't play on iPhone 5.0 simulator

Im new to xCode programming, ive got this code from an ebook tutorial playing mp4 xCode.
function triggered from a button
(IBAction)playMovie:(id)sender {
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"videoSample" ofType:#"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter]addObserver:self selector:#selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.fullscreen=YES;
[moviePlayerController play];
moviePlayerController.scalingMode = MPMovieScalingModeFill;
}
called from play movie function
(void)moviePlaybackComplete:(NSNotification *)notification{
MPMoviePlayerController *moviePlayerController = [notification
object];
[[NSNotificationCenter defaultCenter]removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view removeFromSuperview];
}
Ive got no error and warning after i build it. it just output a blank screen after i click the button that triggers the playMovie function. Im confuse ive google it and still got no idea on how to solve the proble.
Im using xCode 4.2 iOS SDK 5.0
try this....replace "example" with the name of your file and "m4v" with the type of file e.g. "mp4" (not .mp4 and dont put the .mp4 bit in the name of the file)
-(IBAction)playVideo:(id)sender;
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"example" ofType:#"m4v"];
MPMoviePlayerViewController* tmpMoviePlayViewController=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]];
if (tmpMoviePlayViewController) {
tmpMoviePlayViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:tmpMoviePlayViewController animated:YES];
tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(myMovieViewFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:tmpMoviePlayViewController];
[tmpMoviePlayViewController.moviePlayer play];
}
}
-(void)myMovieFinishedCallback:(NSNotification*)theNotification
{
MPMoviePlayerController *moviePlayer=[theNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer.view removeFromSuperview];
}
I don't know if you are still looking for the answer. Here's what you can try - declare the MPMoviePlayerController in your header(.h) file (i.e. make it a member variable).
MPMoviePlayerController *moviePlayerController;
and add this to your .m file
moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
If it still does not work then try to declare it as a #property.

Video Clip does not play correctly

I am trying to play a video in one of my screens. It seems to run but when the file opens, the fullscreen loads up and closes right after. The video is a few seconds long, and is m4v format. I added the framework and imported the class. How can I make it so that the video plays properly?
My header:
-(IBAction)playMedia:(id)sender {
NSString *movieFile;
MPMoviePlayerController *moviePlayer;
movieFile = [[NSBundle mainBundle]
pathForResource:#"movie" ofType:#"m4v"];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL: [NSURL fileURLWithPath: movieFile]];
[moviePlayer.view setFrame:CGRectMake(145.0, 20.0, 155.0 , 100.0)];
[self.view addSubview:moviePlayer.view ];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playMediaFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer play];
if ([toggleFullscreen isOn]) {
[moviePlayer setFullscreen:YES animated:YES];
}
}
My implementation file is:
-(void)playMediaFinished: (NSNotification*)theNotfication {
MPMoviePlayerController *moviePlayer=[theNotfication object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer.view removeFromSuperview];
[moviePlayer release];
}
thanks

MPMoviePlayerController not released

I am running an intro movie in a MPMoviePlayerController and i've got a problem with the allocated memory not being released.
When the movie finishes, Instruments shows the memory being released.
However when i skip the movie by tapping on it the memory is not released.
Here are the important parts of my code:
- (void)applicationDidFinishLaunching:(UIApplication *)theApplication {
self.application = theApplication;
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"mymovie.mov" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[window addSubview:moviePlayer.view];
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.view.frame = window.frame;
UIView *introFrontView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
introFrontView.userInteractionEnabled = YES;
introFrontView.opaque = NO;
introTabRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(introTabFrom:)];
[introFrontView addGestureRecognizer:introTabRecognizer];
[moviePlayer.view addSubview:introFrontView];
[introFrontView release];
[introTabRecognizer release];
self.moviePlayerController = moviePlayer;
[moviePlayer release];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(introFinished:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController play];
}
- (void)introTabFrom:(UITapGestureRecognizer*)recognizer{
[[NSNotificationCenter defaultCenter] postNotificationName:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
}
- (void) introFinished:(NSNotification*)notification {
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:self.moviePlayerController];
[self.moviePlayerController pause];
[self.moviePlayerController stop];
[self.moviePlayerController release];
...
}
Is there anything i forgot? To my understanding i'm posting the correct notification...
Any ideas?
You may want to register for MPMoviePlayerPlaybackStateDidChangeNotification, and look at the playbackState that is passed in.