Iphone : Video playing at startup doesn't quit - iphone

i made a video playing when my app loads however it doen't quit after playing even if you prees "Done". What am i doing wrong?
- (void)viewDidLoad {
NSBundle *bundle=[NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:#"Video Logo Final" ofType:#"mp4"];
NSURL *movieURL=[[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
theMovie.view.frame = CGRectMake(0.0, 0.0, 320.0, 480.0);
[self.view addSubview:theMovie.view];
[theMovie play];
[super viewDidLoad]; }
Also, i made a try to put the same code in "application didFinishLaunchingWithOptions" but i get a warning at "[self.view addSubview:theMovie.view];"
Ay ideas about that?
p.s. As you probably guessed i am very new to programming, any help would be really appreciated...

Basically you need to register for a notification.
The way I do it is:
// Register for the playback finished notification
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(myMovieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: theMovie];
Change the parameters to fit however it should in your app.
Here's the documentation for the MPMoviePlayerPlaybackDidFinishNotification.
Also, if it's not "quitting" after playing (even when hitting the "Done" button), it sounds like you need to remove the theMovie MPMoviePlayerController and associated view from the view you originally added it to.

Related

Exit fullscreen when movie is done playing

I'm trying to implement a notification in to my movie player, so that once the movie is done playing, it will exit fullscreen. The code is have so far i posted below. The IBAction is hooked up to a button. Also on a side note, i have been trying to figure out how to get the "play symbol" on top of my movie view, so that the user just have to press that and the video will start. Anybody know how to implement that?
- (void)viewDidLoad
{
//Video player
NSString *url = [[NSBundle mainBundle] pathForResource:self.navigationItem.title ofType:#"mov"];
_player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath: url]];
_player.view.frame = CGRectMake(350, 200, 400, 400);
[self.view addSubview:_player.view];
}
- (IBAction)playMovie
{
[_player play];
}
You can detect when your movie finishes playing by registering for an MPMoviePlayerPlaybackDidFinishNotification notification in your viewDidLoad method:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(playerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:_player];
And then perform whatever action you want to do in the callback method you specified when registering:
- (void) playerPlaybackDidFinish:(NSNotification*)notification
{
// movie finished playing
_player.fullscreen = NO;
}

How to implement a volume key shutter for iPhone?

I want to implement the same behavior with the native camera of iOS5:
press the volume + button to take a photo
What's the ideal way to archive it?
Are there any ways to capture the volume key pressed event?
After googling & searching around for hours, I found 1 solution: using NSNotificationCenter:
...
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(volumeChanged:)
name:#"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
...
- (void)volumeChanged:(NSNotification *)notification{
[self takePhoto];
}
However, it has 2 issues:
There is an semi-transparent overlay of "current system volume" show up every time when pressing the volume key, this is not what I wanted.
For the native camera, when you press the volume key as shutter, the system volume won't change, however, by using the above method, the system volume will change.
I've found another way to hide the "system volume overlay" and "bypass the system volume change when the volume key pressed" by myself.
The bad part: this is an super UGLY hack.
However, the good part is: this ugly hack uses NO private APIs.
Another note is: it only works for ios5+ (anyway, for my issue, since the AVSystemController_SystemVolumeDidChangeNotification only works for ios5, so this UGLY hack just fits my issue.)
The way it work: "act as a music/movie player app and let the volume key to adjust the application-volume".
Code:
// these 4 lines of code tell the system that "this app needs to play sound/music"
AVAudioPlayer* p = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"photo-shutter.wav"]] error:NULL];
[p prepareToPlay];
[p stop];
[p release];
// these 5 lines of code tell the system that "this window has an volume view inside it, so there is no need to show a system overlay"
[[self.view viewWithTag:54870149] removeFromSuperview];
MPVolumeView* vv = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, -100, 100, 100)];
[self.view addSubview:vv];
vv.tag = 54870149;
[vv release];
(5 hours spending on discovering this super ugly method... shit... 草尼马啊!)
Another thing:
if you take the above hack, you need to run the code EVERY-TIME when your app become active.
So, you might need to put some code into your app delegate.
- (void)applicationDidBecomeActive:(UIApplication *)application
Building on huxia's code, this works on ios5+, no need to run the code every time it becomes active, just run it once in the beginning.
// these 4 lines of code tell the system that "this app needs to play sound/music"
AVAudioPlayer* p = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"photoshutter.wav"]] error:NULL];
[p prepareToPlay];
[p stop];
//make MPVolumeView Offscreen
CGRect frame = CGRectMake(-1000, -1000, 100, 100);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[volumeView sizeToFit];
[self.view addSubview:volumeView];
…
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(volumeChanged:)
name:#"AVSystemController_SystemVolumeDidChangeNotification"
object:nil];
…
- (void)volumeChanged:(NSNotification *)notification{
CGRect frame = CGRectMake(-1000, -1000, 100, 100);
MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:frame];
[volumeView sizeToFit];
[self.view addSubview:volumeView];
[volumeView release];
[self takePhoto];
}
There's currently no official way to capture the volume key pressed event. Apple's stated line is that the volume button works with the UIImagePickerController if you've allowed it to show camera controls.
Other approaches, such as listening for the notification, seem to be unsupported hacks that Apple's team are — anecdotally — sometimes turning a blind eye to. To prevent the volume HUD from appearing you can use the undocumented UIApplication methods:
- (void)setSystemVolumeHUDEnabled:(BOOL)enabled;
- (void)setSystemVolumeHUDEnabled:(BOOL)enabled forAudioCategory:(NSString *)category;
The only statement of their use I've seen is:
UIApplication *app = [UIApplication sharedApplication];
[app setSystemVolumeHUDEnabled:NO forAudioCategory:#"Ringtone"];
[app setSystemVolumeHUDEnabled:NO];
I'm unsure if or why you seemingly need to disable the HUD for a specific category and then in general, but without proper documentation that's difficult to figure out.
So: use UIImagePickerController and its camera buttons if you want to be within the rules. If you've found an app that seems to work outside of the rules then it's probably using the methods above.
I call this method from viewDidAppear
-(void) startTrackingVolume
{
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
if (!self.volumeView) {
// put it somewhere outside the bounds of parent view
self.volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, -100, 10, 0)];
[self.volumeView sizeToFit];
}
if (!self.volumeView.superview) {
[self.view addSubview:self.volumeView];
}
}
In viewWillDisappear in call
[[AVAudioSession sharedInstance] setActive:NO error:nil];

MPMovieController all black (no Rugby inside though :D :D)

)
My client asks me to read a short video as a splahscreen (as it is done especially for games for example). I googled for a bit and I think the guys use a MPMoviePlayerController with StyleControl to None (correct me if I'm wrong...)
I tried with a video from my iPhone (normally in the right format so ....), but remains black. My background is red, I see it appear at the beginning, then it is hidden by my video, which remains black and never starts.
However, I set out the frame of my video (self.view is initialized at the time), I add the player to view my self.view ... In short, I do not see what might messing around.
This is the code (with FullScreen controls to try to see, but I never see them either.
An idea by any chance?
Thx guys ;)
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
MPMoviePlayerController * mMoviePlayer;
// Do any additional setup after loading the view, typically from a nib.
NSURL* mMovieURL;
NSBundle *bundle = [NSBundle mainBundle];
if (bundle)
{
NSString *moviePath = [bundle pathForResource:#"movie" ofType:#"mov"];
if (moviePath)
{
mMovieURL = [NSURL fileURLWithPath:moviePath];
}
}
NSLog(#"%#", mMovieURL);
[[UIApplication sharedApplication] setStatusBarHidden:YES];
mMoviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mMovieURL];
// mMoviePlayer.scalingMode = MPMovieScalingModeAspectFill;
mMoviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[mMoviePlayer.view setFrame: self.view.bounds]; // player's frame must match parent's
[self.view addSubview: mMoviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mMoviePlayer];
[mMoviePlayer play];
}
you may try:
[player setFullscreen:YES];

Writing an app to stream video to iPhone

I'm interested in creating an iPhone app that can stream video from a central server, YouTube style. I was wondering if anyone has ever tried to do this before, what is the path of least resistant, existing APIs, etc? I really know nothing about how this is generally done. Would I be working with sockets? Just looking for some direction here. Thanks!
If you have the streaming server up and ready, it is quite easy to implement a video controller that pops up youtube-style.
NSString *videoURLString = #"http://path-to-iphone-compliant-video-stream";
NSURL *videoURL = [NSURL URLWithString:videoURLString];
MPMoviePlayerController moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer prepareToPlay];
[moviePlayer play];
[self.view addSubview:moviePlayer.view];
You need to handle the controller that display the video player's view (which is self in this case).
In iOS 3.2+ MPMoviePlayerViewController make it even easier:
NSString *videoURLString = #"http://path-to-iphone-compliant-video-stream";
NSURL *videoURL = [NSURL URLWithString:videoURLString];
MPMoviePlayerViewController *moviePlayerView = [[[MPMoviePlayerViewController alloc] initWithContentURL:videoURL] autorelease];
[self presentMoviePlayerViewControllerAnimated:moviePlayerView];
presentMoviePlayerViewControllerAnimated is a MediaPlayer's additional method to FWViewController that you will find in iOS 3.2+ and it takes care of creating a view controller and pushing it on the stack, animating it with a slide-from-bottom animation, as in youtube.app.
Apple has a detailed article about setting up server side for media streaming:
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/StreamingMediaGuide/Introduction/Introduction.html
and Best Practices Note:
https://developer.apple.com/library/content/technotes/tn2224/_index.html
Not only it contains info about streaming service architecture and tools used to build it but also has some requirements to such kind of service that must be fulfilled and references to live test streams.
Use this code to use low memory. On streaming video....
-(IBAction)playMovie:(NSURL *) theURL
{
NSURL *fileURL = theURL;
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
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];
}
QuickTime videos already stream to the phone. The path of least resistance would be to use the media player controller and point it to a streaming media file on a streaming server.
While the existing answers are good, if you need to use non HTTP streams (mms or rtmp for example) or non Apple supported audio / video codecs, things get a bit more complicated.
I'm not an expert myself, but I've been using this VideoStreaming SDK to solve those problems, and it makes customizing the client much easier (background streaming, pausing streams, etc). Might be worth a look if you have those requirements as well.
2018 answer You can use AVPlayerViewController since MPMoviePlayerController is deprecated since iOS 9
NSURL *url = [NSURL URLWithString:videoUrl];
_playerViewController = [[AVPlayerViewController alloc] init];
_playerViewController.player = [AVPlayer playerWithURL:url];
_playerViewController.player.volume = 1;
_playerViewController.showsPlaybackControls = YES;
_playerViewController.view.frame = CGRectMake(....);
[self.view addSubview:_playerViewController.view];

Embedded Video in a UIView with iPhone

I would like to write an app that downloads (or streams) a video (encoded as required) in a view. I dont want to use the MPVideoPlayer from the SDK as it opens the video in full screen. I would like to place another UIView (transparent) over the video so that my users can annotate over the video.
Anyone have any idea or can point me to some code that will play video in a UIView?
If you want to do this you will need to include your own (software) video decoder, which will not have access to the hardware acceleration on the system. Even if you can get it working with acceptable performance it will be a huge battery drain.
If you wish to play a video in portrait mode, I have solution for that.
If you think that -MPMovie Player can run under a view, according to me it's not possible.
MP Movie player will work as Apple has designed.
So, MP Movie player will always / almost run in full screen.
Solution for portrait mode.
#interface MPMoviePlayerController (extend)
-(void)setOrientation:(int)orientation animated:(BOOL)value;
#end
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieUR];
[moviePlayer setOrientation:UIDeviceOrientationPortrait animated:NO];
if (moviePlayer)
{
[self.moviePlayer play];
}
Hope that will help to you.
See, My question is very similar like yours.
playing video in custom size screen - view in iphone
Try this:
UIViewController *v = [[UIViewController alloc] init];
v.view.backgroundColor = [UIColor orangeColor];
NSString *path = [[NSBundle mainBundle] pathForResource:#"demo" ofType:#"mp4"];
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSLog(#"cannot find %# in bundle or doctuments", path);
}
NSURL *url = [NSURL fileURLWithPath:path];
MoviePlayerViewController *mpvc = [[MoviePlayerViewController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:mpvc.moviePlayer];
mpvc.moviePlayer.fullscreen = NO;
[mpvc.moviePlayer setControlStyle:MPMovieControlStyleNone];
mpvc.moviePlayer.view.frame = CGRectMake(10, 100, 300, 300);
[v.view addSubview:mpvc.moviePlayer.view];
[mpvc.moviePlayer play];
[self presentModalViewController:v animated:YES];
[v release];
Maybe you should check the MediaPlayer's private headers, and simply add the video's view as your view's subview.