Xcode : Auto PlayVideo at start up - iphone

i want to play a short video as soon as the application launches. It's a short video and a light application so i do not expect any serious delays. However my problem is that although my code works fine wheni test it as an IBAction, it doesn't work when i paste it anywhere else like ViewDidLoad, awakeFromNib, ApplicationDidFinishedLaunching. I am searching all day long for tutorials that work in Xcode 4.2 (ARC enabled) but i couldn't find anything that worked.
Here is my code:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"testmovie" ofType:#"mp4"]];
MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc]
initWithContentURL:url];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
//playercontroller.movieControlMode=MPMovieControlModeHidden;
[playercontroller.moviePlayer play];
playercontroller = nil;
If anybody can provide a tutorial or sample code, i would be extremly grateful!
Thanks in advance.

take out movie player and just use
[playercontroller play];
if it needs releasing do so as well

Related

Getting a black screen when trying to play a movie

When i'm trying to play a movie on my iPhone, it isn't work. I dont know why.. searched here and all over the internet, try 5 different codes, but still can't get it to work. I'm new to Obj-C, so please be patient.
I just want the movie to play and when the movie is done, to exit the player. also, at the beginning I dont want it to show a black screen, I want to see the first frame of the video instead. is it possible?
-(void)playTheMovie
{
NSURL *url = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:#"C061381" ofType:#"mp4" inDirectory:#""]];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[player view] setFrame:[self.view bounds]]; // Frame must match parent view
[self.view addSubview:[player view]];
[player play];
}
Thanks!
Most probably you are messing up with the URL.
Check out Movie player sample project from apple
https://developer.apple.com/library/ios/#samplecode/MoviePlayer_iPhone/Introduction/Intro.html
Run it first to confirm its functionality and then
Replace its movie with yours and change the extension and path name in the code
if it works copy the code to your project..

Best way for looping and using background sounds in iphone?

Lets say I am dragging my finger on screen and I have 1 second .caf sound file in my bundle.
So what is the best way to play my sound file in a loop till i am dragging my fingers. And it should stop whenever I remove touches.
I know touches implementation. Just post your views about using sound file.
See AVAudioPlayer class, it worked pretty well for me for similar behaviour described in your question.
This is how I did it. At the moment, I don't know of a more efficient method...but here's what I used to loop my background sound. Hope this helps.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/yourAudio.caf", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];

Adding video to an iPhone application

How do I add a video to an iPhone app. using iPhone SDK 3.2.5?
I dont see the actual video but I can hear it playing.
Here is the code:
-(void) viewDidLoad{
NSBundle *bundle=[NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Facebook" ofType:#"mp4"];
NSURL *movieURL=[[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie play];
[super viewDidLoad];
}
First off – find out what video format you are having. I assume it's not the one supported by iPhones.
So when you learn this you can go ahead and look for a third party player that is able to play back your video. I'm sure there are a lot of them there. Recommendation from me – try using VLC.
If you want to use default videos app on your iPhone (which, of course, is way better than any other alternative) you can try out Windows & Mac application WALTR 2: https://softorino.com/waltr/windows-guides/how-transfer-videos-to-iphone-without-itunes. It adapts any video & music for iPhone playback and pushes them to your default app on an iPhone.
You need to set the frame for you MPMoviePlayerController and add it to your view:
theMovie.view.frame = CGRectMake(0.0, 0.0, 320.0, 480.0);
[self.view addSubview:theMovie.view];

Problem in playing sound in application?

I developing simple game application in which i playing sound with UIView animation.
theAudioPlayer = [[AVAudioPlayer alloc] init];
[theAudioPlayer initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"pageTurn"] ofType:#"mp3"]] error:NULL];
[theAudioPlayer play];
where theAudioPlayer is the instance object of AVAudioPlayer class.
Now my problem is when i playing the game after 10-15 minutes during the animation of UIViews sound going to mute.It never play again, why?
Thanks for sending an viewing my question.
First, the loading code is needlessly complex. You should only initialize the player once and get the sample URL right from the bundle:
theAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:
[[NSBundle mainBundle] URLForResource:#"pageTurn" ofType:#"mp3"] error:NULL];
I would not be surprised if that solved the problem alone (given the multiple initialization). And if it does not, which is quite probable, how do you release the player? Do you release it at all?

iPhone:Streaming feed URL sample?

I have a media streaming code like below for playing streaming video. I want to test it with some working streaming url feed on iOS 4 devices. Could someone provide me the exact sample streaming video feed if anything is available for iPhone supported? So that, i can test this code and make sure it works well, though it could be correct. I want a real streaming video url link.
NSString *strUrl = #"http://????????????"
NSURL *url = [NSURL URLWithString:strUrl];
MPMoviePlayerController *mPlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
mPlayer.view.frame = self.view.frame;
[self.view addSubview:mPlayer.view];
[mPlayer setFullscreen:YES animated:YES];
[mPlayer play];
Thanks.
I don't have a URL for you, but your code looks correct, except that you should turn auto-play off before setting full screen with animation. In my experience, the app could crash on setFullscreen:animated: if you use the player more than once. You might also want to observe the MPMoviePlayerPlaybackDidFinishNotification notification in case of playback error so that you can inform the user that there was a playback error.
NSURL *url = [NSURL URLWithString:strUrl];
MPMoviePlayerController *mPlayer = [[MPMoviePlayerController alloc]initWithContentURL:url]; mPlayer.view.frame = self.view.frame;
mPlayer.shouldAutoplay = NO;
[self.view addSubview:mPlayer.view];
[mPlayer setFullscreen:YES animated:YES];
[mPlayer play];
You can use below urls for testing purpose,
http://a1408.g.akamai.net/5/1408/1388/2005110405/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_mpeg4.mp4
I have not tested it, but it should work well...