Stop and Starting MPMoviePlayerController by touch - mpmovieplayercontroller

I do a
player.shouldAutoplay = YES;
[player setEndPlaybackTime:initPlay];
and it works fine and then in
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if ([touches count] == 1) {
[player play];
}
}
Even though the touch happens and the play command happens it does not work, but if I try and set the setEndPlaybackTime again it stil does not play? I just want it to keep play after the user touches the screen any ideas?

Could it be that the video must rewind to the start? Have you tried:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
if ([touches count] == 1) {
player.initialPlaybackTime = 0.0;
[player play];
}
}
See http://developer.apple.com/library/ios/#documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/Reference/Reference.html
A value of -1 may also work.

Related

avaudioplayer play music in background

I have a problem with AVAudioPlayer in background, everything works but when I have the controls in the lock screen and I click on the pause button the control receive the event but the lock screen with controls disappears. I want that lock screen remain with controls like Music app.
I put these lines in viewDidLoad:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
and then :
-(void)viewDidAppear:(BOOL)animated{
[[UIApplication sharedApplication]beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
-(BOOL)canBecomeFirstResponder{ return YES; }
- (void)viewWillDisappear:(BOOL)animated {
//End recieving events
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
[super viewWillDisappear:animated];
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
//if it is a remote control event handle it correctly
if (event.type == UIEventTypeRemoteControl) {
if (event.subtype == UIEventSubtypeRemoteControlPause) {
[self.playButton setBackgroundImage:[UIImage imageNamed:#"play_icon.png"]
forState:UIControlStateNormal];
[self.audioPlayer pauseAudio];
self.isPaused = FALSE;
}
else if (event.subtype == UIEventSubtypeRemoteControlPlay){
if (!self.isPaused) {
[self.playButton setBackgroundImage:[UIImage imageNamed:#"pause.png"]
forState:UIControlStateNormal];
//start a timer to update the time label display
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(updateTime:)
userInfo:nil
repeats:YES];
[self.audioPlayer playAudio];
self.isPaused = TRUE;
}
}
}
}
I find the solution !!
Using this property :
#property MPNowPlayingInfoCenter* nowPlayingInfo;
I have to add these lines on my viewDidLoad :
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo=[NSDictionary dictionaryWithObject:#1.0f forKey:MPNowPlayingInfoPropertyPlaybackRate];

draggable specific class which is a subclass of UIImageView

I have create a class Box which is a subclass of UIImageView.
In my program, I want to make the Box to be the only draggable object in the main view controller.
Here is my code:
BoxViewController.m
-(void) viewDidLoad{
[super viewDidLoad];
target = [[Box alloc] initWithFrame:CGRectMake(10,10,100,100)];
[self.view addSubView:target];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if([touch view] == target){
CGPoint location = [touch locationInView:self.view];
target.center = location;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[self touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event];
}
I think the condition is not working, but I have tried isKindOfClass. Both didn't work. What is the solution? Thank you very much.
try this method
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint location = [[touches anyObject] locationInView:self.view];
target.transform = CGAffineTransformMakeTranslation(location.x, location.y);
}

Add an undo/redo method to core graphic draw app

I am trying to implement an undo/redo method using NSUndoManager. I have asked other questions on this, but am still stuck.
Where I am at the moment is as follows:
.h
NSUndoManager *undoManager;
#property(nonatomic,retain) NSUndoManager *undoManager;
.m
#synthesize undoManager;
[undoManager setLevelsOfUndo:99];
viewdidload:
NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter];
[dnc addObserver:self selector:#selector(undoButtonTapped) name:#"undo" object:nil];
[dnc addObserver:self selector:#selector(redoButtonTapped) name:#"redo" object:nil];
- (void)resetTheImage:(UIImage*)image
{
NSLog(#"%s", __FUNCTION__);
// image = savedImage.image;
if (image != drawImage.image)
{
[[undoManager prepareWithInvocationTarget:self] resetTheImage];
image = drawImage.image ;
} else {
NSLog(#"That didn't work");
}
}
- (void)undoButtonTapped {
NSLog(#"%s", __FUNCTION__);
[undoManager undo];
}
I get "That didn't work"...
I would appreciate help. I will post the answer to my original question when I figure out what I'm doing wrong.
---EDIT---
I have changed resetTheImage as follows:
- (void)resetTheImage:(UIImage*)image
{
NSLog(#"%s", __FUNCTION__);
image = savedImage.image;
if (image != drawImage.image)
{
drawImage.image = image;
savedImage.image = image;
NSLog(#"undo image");
[[self.undoManager prepareWithInvocationTarget:drawImage.image] image];
} else {
NSLog(#"same image");
savedImage.image = image;
}
}
However, confusion rains - it may help if someone (Brad?, Justin?) can provide a bullet point list of the steps I need to take to get this working. For example:
. Add notifications in ...
. Trigger notifications....
. Have undo /redo buttons point to...
. What methods/functions I really need to build..
....
I wish SO would allow me to give you more points than 1..
(It doesn't help that my "o" key is getting wonky)
It does help that I had a baby granddaughter yesterday :))))
First, I want to thank everyone for any/all assistance. I solved this finally although I'm not sure if it's the best solution.
I made a UIView called from the UIViewController. The controls (colors and brushes) remain in the VC. The drawing methods move to the View Method.
The View Method calls a Drawing method to actually perform the draw, and the View method controls the undo/redo.
Here are some code snippets:
-(void)undoButtonClicked
{
//NSLog(#"%s", __FUNCTION__);
if ([self.currentArray count] == 0) {
//nothing to undo
return;
}
DrawingPath *undonePath = [self.currentArray lastObject];
[self.currentArray removeLastObject];
[self.redoStack addObject:undonePath];
[self setNeedsDisplay];
}
-(void)redoButtonClicked
{
//NSLog(#"%s", __FUNCTION__);
if ([self.redoStack count] == 0) {
// nothing to redo
return;
}
DrawingPath *redonePath = [self.redoStack lastObject];
[self.redoStack removeLastObject];
[self.currentArray addObject:redonePath];
[self setNeedsDisplay];
}
Let me know if anyone wants clarification. Thanks all again..
UPDATE as requested:
These are some headers:
DrawingViewController *mvc;
NSMutableArray *pathArray;
NSMutableArray *colorArray;
NSMutableArray *bufferArray;
NSMutableArray *currentArray;
UIBezierPath *myPath;
NSString *brushSize;
CGPoint lastPoint;
int colorIndex;
NSString *colorKey;
SoundEffect *erasingSound;
SoundEffect *selectSound;
BOOL swiped;
int moved;
UIColor *currentColor;
NSString *result;
}
#property(nonatomic,assign) NSInteger undoSteps;
#property (strong, nonatomic) NSString *result;
#property (strong,nonatomic) UIColor *currentColor;
#property (strong,nonatomic) NSMutableArray *currentArray;
#property (strong,nonatomic) NSMutableArray *bufferArray;
#property (strong,nonatomic) DrawingPath *currentColoredPath;
#property (strong,nonatomic) NSMutableArray *redoStack;
#property (strong, nonatomic) NSString *colorKey;
and here are some more of the methods.. The currentArray then keeps track of points, brush and color in a sort of stack. Undo removes from the stack, and adds into a temp stack that can be used to Redo.
-(void)undoButtonClicked
{
//NSLog(#"%s", __FUNCTION__);
if ([self.currentArray count] == 0) {
//nothing to undo
return;
}
DrawingPath *undonePath = [self.currentArray lastObject];
[self.currentArray removeLastObject];
[self.redoStack addObject:undonePath];
[self setNeedsDisplay];
}
-(void)redoButtonClicked
{
//NSLog(#"%s", __FUNCTION__);
if ([self.redoStack count] == 0) {
// nothing to redo
return;
}
DrawingPath *redonePath = [self.redoStack lastObject];
[self.redoStack removeLastObject];
[self.currentArray addObject:redonePath];
[self setNeedsDisplay];
}
#pragma mark - Touch Methods
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(#"%s", __FUNCTION__);
self.currentColoredPath = [[DrawingPath alloc] init];
[self.currentColoredPath setColor:self.currentColor];
UITouch *touch= [touches anyObject];
[self.currentColoredPath.path moveToPoint:[touch locationInView:self]];
[self.currentArray addObject:self.currentColoredPath];
// Remove all paths from redo stack
[self.redoStack removeAllObjects];
lastPoint = [touch locationInView:self];
lastPoint.y -= 20;
if ([touch tapCount] == 2) {
[self alertOKCancelAction];
return;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(#"%s", __FUNCTION__);
UITouch *touch = [touches anyObject];
[self.currentColoredPath.path addLineToPoint:[touch locationInView:self]];
[self setNeedsDisplay];
CGPoint currentPoint = [touch locationInView:self];
currentPoint.y -= 20;
lastPoint = currentPoint;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//NSLog(#"%s", __FUNCTION__);
self.currentColoredPath = nil;
}

Cocoa Touch - UIButtons - Subclassing UIButton

Hey all, can anyone please explain how I can subclass UIButton and override some method so that when the user drags off a button it comes up right away? The problem is that when I drag out of the button frame it remains active and down. I want it to stop as soon as the finger leaves the button frame. Any ideas?
(Cocoa Touch)
If anyone ever has this problem, the following code allows for extremely accurate edge sensing while dragging. If you drag out of the button, it wont extend past the button's edge like normal.
(I subclassed UIButton and made the following:)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
touchBlocker = TRUE;
self.highlighted = TRUE;
NSLog(#"Touch Began");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self];
if (touchBlocker) {
if (!CGRectContainsPoint([self bounds], location)) {
touchBlocker =FALSE;
self.highlighted = FALSE;
NSLog(#"Touch Exit");
}
} else if (CGRectContainsPoint([self bounds], location)) {
touchBlocker = TRUE;
self.highlighted = TRUE;
NSLog(#"Touch Enter");
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
touchBlocker = FALSE;
self.highlighted = FALSE;
NSLog(#"Touch Ended");
}

How to get the x& y value from the touchesbegan to another method

HI
How to get the x& y value from the touchesbegan to our method,i have made the sample like this to get the location of my TextField,and this location was taken in another method to calculate the location of the text and add text to the UIImage,im facing problem in this final stage to pass this parameter to the CGContextShowTextAtPoint,
this is my code.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
NSUInteger tapCount = [touch tapCount];
CGPoint location = [touch locationInView:touch.view];
textBox.center = location;
NSLog(#"location-%f,%f", location.x,location.y);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"touches moved");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"touches ended");
}
i need the value of the location.x,location.y,get passed to another method.so that i can fix the location of the text,
can any one help me in this...
Thanks in advance

Resources