i have search on the site but i haven't find the same problem as mine
when i do a pinch out on my video, the notification "MPMoviePlayerPlaybackDidFinishNotification" is called.
after, the "done" button put the video in pause and the player works badly...
I don't understand why this notification is called...
this is my code
- (id) init
{
self = [super init];
movie=[[MPMoviePlayerViewController alloc] init];
//we init the frame here and after the view rotate the video
[movie.view setFrame: CGRectMake(0, 0, 1024,768)];
return self;
}
+ (MoviePlayerManager*) getInstance
{
static MoviePlayerManager *movieSingleton;
if (movieSingleton==nil)
{
movieSingleton = [[MoviePlayerManager alloc]init];
}
return movieSingleton;
}
- (void) load:(NSURL*) a_videoFile withType:(VideoType)a_type
{
type = a_type;
[movie.moviePlayer setContentURL:a_videoFile];
switch (type) {
case VT_INTRO:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallbackIntro:) name:MPMoviePlayerPlaybackDidFinishNotification object:movie.moviePlayer];
break;
case VT_RESPONSE:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieFinishedCallbackResponse:) name:MPMoviePlayerPlaybackDidFinishNotification object:movie.moviePlayer];
break;
default:
NSLog(#"video Type not initialised");
break;
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myMovieIsReadyToPlay:) name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification object:movie.moviePlayer];
[movie.moviePlayer prepareToPlay];
}
-(void)myMovieIsReadyToPlay:(NSNotification*)aNotification
{
[gsDelegate.view addSubview:movie.view];
[movie.moviePlayer play];
movie.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
}
- (void) myMovieFinishedCallbackIntro:(NSNotification*)aNotification
{
NSNumber* reason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
NSLog(#"%d",reason);
if(aNotification != nil)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:movie.moviePlayer];
[gsDelegate movieIntroDidStop];
}
}
the NSNumber* reason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
is the same for a pinch out or when i press "done"
thx for your help (and sorry for my bad english ;op)
NSNumber* reason = [[aNotification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
NSLog(#"%d",reason);
NSNumber is an Objective-C object, not a primitive C type. You are displaying the pointer to the object, not the value.
Correct with:
NSLog(#"%#", reason);
OR change reason to an Integer:
int reason = [[userInfo objectForKey:#"MPMoviePlayerPlaybackDidFinishReasonUserInfoKey"] intValue];
NSLog(#"%d", reason);
Related
I have a UISwitchcontroller in my setting page to change the image in main page. So I put a notification from setting page to main page. But only one notification I active every time, my setting page switch code look like this:
-(IBAction)_clickswitchlowlight:(id)sender
{
if(switchControll.on){
[switchControll setOn:YES animated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_lowlighton object:nil];
}
else{
[switchControll setOn:NO animated:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_lowlightoff object:nil];
}
}
In my main page.h..I write this code:
extern NSString * const NOTIF_lowlighton;
extern NSString * const NOTIF_lowlightoff;
In .m above the implementation of main page I write this:
NSString * const NOTIF_lowlighton = #"lowlighton";
NSString * const NOTIF_lowlightoff = #"lowlightoff";
In viewwillappear I write this code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(clicklowlighton:) name:#"lowlighton" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(clicklowlightoff:) name:#"lowlightoff" object:nil];
}
Then this code for changing the image:
- (void)clicklowlighton:(NSNotification *)notif
{
[[self multiPageView] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"bgipad"]]];
}
- (void)clicklowlightoff:(NSNotification *)notif
{
[[self multiPageView] setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"bglowlight#2x"]]];
}
I only get the clicklowlightoff notification, I didn't get the first notification...any missing in my code?
bind your function to event: UIControlEventValueChanged to this function
-(IBAction)_clickswitchlowlight:(id)sender
{
if(switchControll.on){
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_lowlighton object:nil];
}
else{
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIF_lowlightoff object:nil];
}
}
How do I find out if the keyboard is up?
I have a UISearchbar instance which becomes the first responder.
When the keyboard appears a notification is sent out as part of the API, however I don't want to respond to this right away. I could record this in a boolean state, but that seems clunky. I'd like to know if there is a "getter" some where I can call to find out.
This is how I do it:
KeyboardStateListener.h
#interface KeyboardStateListener : NSObject {
BOOL _isVisible;
}
+ (KeyboardStateListener *) sharedInstance;
#property (nonatomic, readonly, getter=isVisible) BOOL visible;
#end
KeyboardStateListener.m
#import "KeyboardStateListener.h"
static KeyboardStateListener *sharedObj;
#implementation KeyboardStateListener
+ (KeyboardStateListener *)sharedInstance
{
return sharedObj;
}
+ (void)load
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
sharedObj = [[self alloc] init];
[pool release];
}
- (BOOL)isVisible
{
return _isVisible;
}
- (void)didShow
{
_isVisible = YES;
}
- (void)didHide
{
_isVisible = NO;
}
- (id)init
{
if ((self = [super init])) {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:#selector(didShow) name:UIKeyboardDidShowNotification object:nil];
[center addObserver:self selector:#selector(didHide) name:UIKeyboardWillHideNotification object:nil];
}
return self;
}
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
#end
Then use this to figure out the rest:
KeyboardStateListener *obj = [KeyboardStateListener sharedInstance];
if ([obj isVisible]) {
//Keyboard is up
}
The only sure way that I can think to do it as you said. using notifications like this:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
and then
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
Other than that, you may be able to iterate through your views subviews and look for the keyboard like:
UIView *keyboard = nil;
for (UIView *potentialKeyboard in [myWindow subviews]) {
// iOS 4
if ([[potentialKeyboard description] hasPrefix:#"<UIPeripheralHostView"]) {
potentialKeyboard = [[potentialKeyboard subviews] objectAtIndex:0];
}
if ([[potentialKeyboard description] hasPrefix:#"<UIKeyboard"]) {
keyboard = potentialKeyboard;
break;
}
}
But I am not sure if this will break when the SDK changes ...
Maybe use this method and add a category to the window so that you can just always ask the window for the keyboard ... just a thought.
I have notification on movie player:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
And it's handler:
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
[[UIApplication sharedApplication] setStatusBarHidden:YES];
// Remove observer
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
[self dismissModalViewControllerAnimated:YES];
}
Here in this handler method I want to check if the done button is sender. Because I have two senders to this method. How ti check this?
Per docs: MPMoviePlayerPlaybackDidFinishNotification userInfo dictionary must contain NSNUmber for MPMoviePlayerPlaybackDidFinishReasonUserInfoKey key indicating the reason playback has finished. Its possible values:
enum {
MPMovieFinishReasonPlaybackEnded,
MPMovieFinishReasonPlaybackError,
MPMovieFinishReasonUserExited
};
You will first need to assign tag to your buttons before the action and then check the value of the sender tag.
Just add these lines of code:
- (void) moviePlayBackDidFinish:(NSNotification*)notification {
NSInteger anyInteger = [sender tag];
//Now check the value of the anyInteger and write the code accordingly.
//switch case or if condition whatever you want.
}
That's it.
This is an old thread but I stumbled upon it while looking for a solution, and the accepted solution doesn't show the final code.
Here is what you have to do:
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
NSLog(#"moviePlayBackDidFinish");
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
NSInteger movieFinishReason= [[[notification userInfo]objectForKey:
MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] intValue];
if(movieFinishReason == 2 || movieFinishReason == 1 || movieFinishReason == 0){
[self dismissViewControllerAnimated:YES completion:nil];
}
/*
MPMovieFinishReasonPlaybackEnded = 0,//played movie sucessfuly.
MPMovieFinishReasonPlaybackError = 1, //error in playing movie
MPMovieFinishReasonUserExited = 2; //user quitting the application / user pressed done button
*/
}
Add tag with the button and put condition according to the tag.
Or check by
if([sender isEqual:btn1])
{
}
else
{
}
Here's the code:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *musicURL = [NSURL URLWithString:#"http://live-three2.dmd2.ch/buureradio/buureradio.m3u"];
if([musicURL scheme])
{
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:musicURL];
if (mp)
{
// save the music player object
self.musicPlayer = mp;
[mp release];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(popBack:) name:#"MPMoviePlayerDidExitFullscreenNotification" object:nil];
// Play the music!
[self.musicPlayer play];
}
}
}
-(void)popBack:(NSNotification *)note
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
The selector method never gets called. I just want to pop back to the root menu when the "Done" button is pressed on the movie player. I put an NSLog in the selector to check if it was even being called, nothing. The music plays fine. Any thoughts?
This should work
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(popBack:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
I have a mainView. To this view, I am adding a view of the same size. When the mainView(the background) rotates, its being detected but the subview doesnt have any idea about being rotated. And its functions are not even being called. Even when the program launches too, if I am in landscape mode, its the same way.
How can I make the subView know that the device is being rotated?
Perhaps you can shoot an event from the mainView to the subView, like so (in mainView):
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
[subView didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
I quickly grew frustrated by the lack of rotation notification support for non-primary UIViewController instances.
So I baked my own as a UIViewController extension. Note that this is purely for rotation detection within the subview, it won't rotate the subview - I'm working on that part now.
Source code then example usage below.
// Released under license GPLv3.
// Copyright (c) 2012 N David Brown. All Rights Reserved.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
// Note: 'shouldAutorotateToInterfaceOrientation:' is automatically called by
// 'willRotate..', 'didRotate..' method calling notification handler
// blocks, so typically will not be desired for notification.
#define NOTIFY_SHOULD_AUTOROTATE 0
#interface UIViewController (NDBExtensions)
// For dispatchers.
#if NOTIFY_SHOULD_AUTOROTATE
-(void)notifyShouldAutorotate:(UIInterfaceOrientation)toInterfaceOrientation;
#endif
-(void)notifyWillRotate:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
-(void)notifyDidRotate:(UIInterfaceOrientation)fromInterfaceOrientation;
// For listeners.
#if NOTIFY_SHOULD_AUTOROTATE
-(void)listenForShouldAutorotate;
#endif
-(void)listenForWillRotate;
-(void)listenForDidRotate;
-(void)listenForAnyRotate;
-(void)stopListeningForAnyRotate;
#end
#implementation UIViewController (NDBExtensions)
#if NOTIFY_SHOULD_AUTOROTATE
-(void)notifyShouldAutorotate:(UIInterfaceOrientation)toInterfaceOrientation {
NSString *name = #"shouldAutorotate";
NSString *key = #"toInterfaceOrientation";
NSNumber *val = [NSNumber numberWithInt:toInterfaceOrientation];
NSDictionary *info = [NSDictionary dictionaryWithObject:val forKey:key];
[[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
}
#endif
-(void)notifyWillRotate:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
NSString *name = #"willRotate";
NSString *key = #"toInterfaceOrientation";
NSNumber *val = [NSNumber numberWithInt:toInterfaceOrientation];
NSString *key2 = #"duration";
NSNumber *val2 = [NSNumber numberWithDouble:duration];
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:val,key,val2,key2,nil];
[[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
}
-(void)notifyDidRotate:(UIInterfaceOrientation)fromInterfaceOrientation {
NSString *name = #"didRotate";
NSString *key = #"fromInterfaceOrientation";
NSNumber *val = [NSNumber numberWithInt:fromInterfaceOrientation];
NSDictionary *info = [NSDictionary dictionaryWithObject:val forKey:key];
[[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
}
#if NOTIFY_SHOULD_AUTOROTATE
-(void)listenForShouldAutorotate {
[[NSNotificationCenter defaultCenter]
addObserverForName:#"shouldAutorotate"
object:nil queue:nil
usingBlock:^(NSNotification* notification){
NSNumber *val = [[notification userInfo] objectForKey:#"toInterfaceOrientation"];
UIInterfaceOrientation toInterfaceOrientation = (UIInterfaceOrientation)[val intValue];
[self shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
}];
}
#endif
-(void)listenForWillRotate {
[[NSNotificationCenter defaultCenter]
addObserverForName:#"willRotate"
object:nil queue:nil
usingBlock:^(NSNotification* notification){
NSNumber *val = [[notification userInfo] objectForKey:#"toInterfaceOrientation"];
UIInterfaceOrientation toInterfaceOrientation = (UIInterfaceOrientation)[val intValue];
NSNumber *val2 = [[notification userInfo] objectForKey:#"duration"];
NSTimeInterval duration = [val2 doubleValue];
if ([self shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]) {
[self willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
}
}];
}
-(void)listenForDidRotate {
[[NSNotificationCenter defaultCenter]
addObserverForName:#"didRotate"
object:nil queue:nil
usingBlock:^(NSNotification* notification){
NSNumber *val = [[notification userInfo] objectForKey:#"fromInterfaceOrientation"];
UIInterfaceOrientation fromInterfaceOrientation
= (UIInterfaceOrientation)[val intValue];
UIInterfaceOrientation toInterfaceOrientation
= (UIInterfaceOrientation)[[UIDevice currentDevice] orientation];
if ([self shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]) {
[self didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
}];
}
-(void)listenForAnyRotate {
#if NOTIFY_SHOULD_AUTOROTATE
[self listenForShouldAutorotate];
#endif
[self listenForWillRotate];
[self listenForDidRotate];
}
-(void)stopListeningForAnyRotate {
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"shouldAutorotate" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"willRotate" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:#"didRotate" object:nil];
}
#end
Example usage:
// In PrimaryViewController.h (instance of this contains 'view'
// which is first subview in window).
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
// Normal rules go here.
return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
// Normal rules go here.
// ..and notification dispatch:
[self notifyWillRotate:toInterfaceOrientation duration:duration];
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
// Normal rules go here.
// ..and notification dispatch:
[self notifyDidRotate:fromInterfaceOrientation];
}
// In OtherViewController.h (this could be any non-primary view controller).
-(void)viewDidLoad {
[self listenForAnyRotate];
}
-(void)viewDidUnload {
[self stopListeningForAnyRotate];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
// Normal rules go here.
return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
// Normal rules go here.
NSLog(#"#willRotate received!");
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
// Normal rules go here.
NSLog(#"#didRotate received!");
}
You could fire an NSNotification when the main view is rotated, which the subview is registered to listen for. There's a quick overview of NSNotification over here.
One advantage of this approach is that objects other than subclasses of UIView can listen for this notification.