How does overlayViewTouched notification work in the MoviePlayer sample code - iphone

I have a question regarding the MoviePlayer sample code provided by apple.
I don't understand how the overlayViewTouch notification works. The NSlog message I added to it does not get sent when I touch the view (not button).
// post the "overlayViewTouch" notification and will send
// the overlayViewTouches: message
- (void)overlayViewTouches:(NSNotification *)notification
{
NSLog(#"overlay view touched");
// Handle touches to the overlay view (MyOverlayView) here...
}
I can, however, get the NSlog notification if I place it in -(void)touchesBegan in "MyOverlayView.m". Which makes me think it is recognizing touches but not sending a notification.
// Handle any touches to the overlay view
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
if (touch.phase == UITouchPhaseBegan)
{
NSLog(#"overlay touched(from touchesBegan")
// IMPORTANT:
// Touches to the overlay view are being handled using
// two different techniques as described here:
//
// 1. Touches to the overlay view (not in the button)
//
// On touches to the view we will post a notification
// "overlayViewTouch". MyMovieViewController is registered
// as an observer for this notification, and the
// overlayViewTouches: method in MyMovieViewController
// will be called.
//
// 2. Touches to the button
//
// Touches to the button in this same view will
// trigger the MyMovieViewController overlayViewButtonPress:
// action method instead.
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:OverlayViewTouchNotification object:nil];
}
}
Can anyone shed light on what I am missing or doing wrong?
Thank you.

As it seems to me the sample code is missing the addObserver selector call to the Notification. An example of the registration can be found in the AppDelegate:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
As in NSNotificationCenter Documentation
When an object (known as the notification sender) posts a notification, it sends an NSNotification object to the notification center. The notification center then notifies any observers for which the notification meets the criteria specified on registration by sending them the specified notification message, passing the notification as the sole argument.
If there are no observers no one will be informed by NSNotificationCenter.
Just add the appropriate register in init for example.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(overlayViewTouches:)
name:OverlayViewTouchNotification
object:nil];

It's because the overlay view is small. You can see the area covered by the overlay view by changing the background color of the overlay view. The notification will be delivered when you touch the area.

Related

Get Notification when a video starts or stops in UIWebView

Hello i am new to objective - c
I'm having a problem with the UIWebView and MPMoviePlayerController: My UIWebView has a movie inside the html (it's a local html file), I'm using html5 and a video tag for the video.
I want a notification when video starts or stops in UIWebView....
I have tried using MPMoviePlayerPlaybackDidFinishNotification, but it doesnt fire ...
I have also tried to make the my main UIViewController's view a view of my own, and intercept -didAddSubview: and -willRemoveSubview:. but with no sucess...
Does any body know how to get notification from uiwebview??
You can observe #"MPAVControllerPlaybackStateChangedNotification" (use nil for the object). This notification isn't documented so I don't know if the App Store will approve your app.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackStateDidChange:)
name:#"MPAVControllerPlaybackStateChangedNotification"
object:nil];
The notification has the key MPAVControllerNewStateParameter in its userInfo. The value seems to be 0 before playback starts, 1 when it is paused, 2 when it is playing, and 3 (momentarily) when you are dragging the playback slider.
- (void)playbackStateDidChange:(NSNotification *)note
{
NSLog(#"note.name=%# state=%d", note.name, [[note.userInfo objectForKey:#"MPAVControllerNewStateParameter"] intValue]);
}
I searched alot about this..Here is the solution that I have found for getting the playback end notification call. Tested code on iOS6.0 and above. All thanks to #Morten.
In viewDidLoad add observer
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackDidEnd:)
name:#"MPAVControllerItemPlaybackDidEndNotification"//#"MPAVControllerPlaybackStateChangedNotification"
object:nil];
Then simply add following javascript code webViewDidFinishLoad delegate as below
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//http://stackoverflow.com/a/12504918/860488
[videoView stringByEvaluatingJavaScriptFromString:#"\
var intervalId = setInterval(function() { \
var vph5 = document.getElementById(\"video-player\");\
if (vph5) {\
vph5.playVideo();\
clearInterval(intervalId);\
} \
}, 100);"];
}
- (void)playbackDidEnd:(NSNotification *)note
{
//do your stuff here
[videoView removeFromSuperview];
videoView.delegate = nil;
videoView = nil;
}
You will get playbackDid End call in the above selected and can do whatever is your requirement.
Happy Coding !!

iPhone - sending a message "in the air" for any listener object

Is there a way onto the iPhone for an object to send a message without a specific receiver object, and into another object, listen to such messages, that could come with objects (parameters), and do what is needed ?
I searched around NSNotification but I don't see what I should do.
Objects that want to be notified need to register to receive notifications with the notification center. Thereafter, when a notification is posted to the notification center, the notification center will check it against all the registered filters, and the corresponding action will be taken for each matching filter.
A "filter" in this case is the pair of (notification name, notification object). A nil object in the filter is equivalent to any object (the notification object is ignored in matching). The name is required.
Example:
/* Subscribe to be sent -noteThis:
* whenever a notification named #"NotificationName" is posted to the center
* with any (or no) object. */
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:#selector(noteThis:)
name:#"NotificationName"
object:nil];
/* Post a notification. */
[nc postNotificationName:#"NotificationName" object:self userInfo:someDict];
/* Handle a notification. */
- (void)noteThis:(NSNotification *)note
{
id object = [note object];
NSDictionary *userInfo = [note userInfo];
/* take some action */
}
There is a more modern API using queues and blocks, but I find the old API easier to illustrate and explain.
Basically, you post a notification (NSNotification) to the shared class, NSNotificationCenter. Here's an example:
#define kNotificationCenter [NSNotificationCenter defaultCenter]
#define kNotificationToSend #"a notification name as a string"
//... Post the notification
[kDefaultCenter postNotificationNamed:knotificationToSend withObject:nil];
Any class that wants to listen, adds itself as an observer to the notifcation center. You must remove the observer as well.
[kNotificationCenter addObserver:self selector:#selector(methodToHandleNotification) object:nil];
//... Usually in the dealloc or willDisappear method:
[kNotificationCenter removeObserver:self];
You can do more with the notification center. See the NSNotificationCenter documentation fr complete reference.
I think NSNotification is the message object itself, to send to listen to what is sent try NSNotificationCenter. It has a singleton object, so to send the message:
NSNotification *notificationObj;
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotification:notificationObj];
And the other class listen to with:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(method:) object:nil];
Make sure that class has method: method. You can have a single parameter, which is an NSNotification object that is sent earlier. The NSNotification object has [notificationObj object which you can get as a piece of data sent by the sender class. Alternatively, you might use [notificationObj userInfo] if you want it to be more structured.
you can initialise notificationObj and tailor it with the message that you'd want. More information on NSNotificationCenter, you can find it
http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html#//apple_ref/occ/cl/NSNotificationCenter
or for more information about NSNotification itself
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotification_Class/Reference/Reference.html

How to control Next Track Item on MPMusicPlayer

In MPMusicPlayer i want to display MPMediaPickerView if the Next Track item are finished....
How this is possible on phone....Please help me....
Watch for MPMusicPlayerControllerNowPlayingItemDidChangeNotification, and in your selector method, check the playbackState for MPMusicPlaybackStateStopped, do other checking (maybe nowPlayingItem?), and present your view.
NSNotificationCenter * notifications = [NSNotificationCenter defaultCenter];
[notifications addObserver:self selector:#selector (trackDidChangeWithNotification:)
name:MPMusicPlayerControllerNowPlayingItemDidChangeNotification
object:self.iPodController];
.....
-(void)handleNextTrackNotification {
if ([self.iPodController playbackState] == MPMusicPlaybackStateStopped) {
// do whatever logic checks
// present view
}
}

iphone keyboard touch events

I need to be able to detect touch events on the keyboard. I have an app which shows a screen which occurs after a certain period of inactivity (i.e. no touch events) To solve this issue, I have subclassed my UIWindow and implemented the sendEvent function, which allows me to get touch events on the whole application by implementing the method in one place. This works everywhere beside when the keyboard is presented and the user is typing on the keyboard. What I need to know is that is there a way to detect touch events on the keyboard, kind of like what sentEvent does for uiWindow. Thanks in advance.
found a solution to the problem. if you observe the following notifications, you are able to get an event when the key is pressed. I added these notifications in my custom uiwindow class so doing it at one place will allow me to get these touch events throughout the application.
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(keyPressed:) name: UITextFieldTextDidChangeNotification object: nil];
[[NSNotificationCenter defaultCenter] addObserver: self selector: #selector(keyPressed:) name: UITextViewTextDidChangeNotification object: nil];
- (void)keyPressed:(NSNotification*)notification
{ [self resetIdleTimer]; }
anyways, hope it helps someone else.
iPhoneDev: here is what I am doing.
I have a custom UIWindow object. In this object, there is a NSTimer that is reset whenever there is a touch. To get this touch you have to override the sendEvent method of UIWindow.
this is what the sendEvent method looks like in my custom window class:
- (void)sendEvent:(UIEvent *)event
{
if([super respondsToSelector: #selector(sendEvent:)])
{
[super sendEvent:event];
}
else
{
NSLog(#"%#", #"CUSTOM_Window super does NOT respond to selector sendEvent:!");
ASSERT(false);
}
// Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
NSSet *allTouches = [event allTouches];
if ([allTouches count] > 0)
{
// anyObject works here.
UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
{
[self resetIdleTimer];
}
}
}
here is the resetIdleTimer:
- (void)resetIdleTimer
{
if (self.idleTimer)
{
[self.idleTimer invalidate];
}
self.idleTimer = [NSTimer scheduledTimerWithTimeInterval:PASSWORD_TIMEOUT_INTERVAL target:self selector:#selector(idleTimerExceeded) userInfo:nil repeats:NO];
}
after this, in the idleTimerExceeded, I send a message to the window delegates, (in this case, the appDelegate).
- (void)idleTimerExceeded
{
[MY_CUSTOM_WINDOW_Delegate idleTimeLimitExceeded];
}
When I create this custom window object in the appDelegate, I set the appDelegate as the delegate for this window. And in the appDelegate definition of idleTimeLimitExceeded is where I do what I have to when the timer expires. They key thing is the create the custom window and override the sendEvent function. Combine this with the two keyboard notification shown above I added in the init method of the custom window class and you should be able to get 99% of all touch events on screen anywhere in the application.

Can I detect if higher subview has been touched?

I've got a big UIView that responds to touches, and it's covered with lots of little UIViews that respond differently to touches. Is it possible to touch anywhere on screen and slide around, and have each view know if it's being touched?
For example, I put my finger down on the upper left and slide toward the lower right. The touchesBegan/Moved is collected by the baseView. As I pass over itemView1, itemView2, and itemView3, control passes to them. If I lift my finger while over itemView2, it performs itemView2's touchesEnded method. If I lift my finger over none of the items, it performs baseView's touchesEnded.
At the moment, if I touch down on baseView, touchEnded is always baseView and the higher itemViews are ignored.
Any ideas?
If I understand correctly, the touchesEnded event is detected, but not by the subview that needs to know about it. I think this might work for you:
In a common file, define TOUCHES_ENDED_IN_SUPERVIEW as #"touches ended in superview".
In the touchesEnded method of the containing view that is firing, add
[[NSNotificationCenter defaultCenter] postNotificationName: TOUCHES_ENDED_IN_SUPERVIEW object: self];
In the touchesBegan of the subviews, add
[[NSNotificationCenter defaultCenter] addObserver: self
selector: #selector(touchesEnded:)
name: TOUCHES_ENDED_IN_SUPERVIEW
object: self.superview];
In the touchesEnded methods of the subviews, use your normal logic for the event, and also add
[[NSNotificationCenter defaultCenter] removeObserver: self name: TOUCHES_ENDED_IN_SUPERVIEW object: self.superview];
Remember to put [[NSNotificationCenter defaultCenter] removeObserver: self] in your dealloc as well, in case it is possible to leave the page without getting the touchesEnded event.
You might want the notification to send its message to a special touchesEndedInSuperview method, which would invoke touchesEnded itself, but that depends on whether you have any special processing to do in that case.
You can use something like this:
-(void)touchesEnded: (NSSet *)touches
withEvent: (UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: touch.view];
if (CGRectContainsPoint(control1.frame, location)) {
[self control1Action];
} else if (CGRectContainsPoint(control2.frame, location)) {
[self control2Action];
}
}