Pause the Views from Updating - iphone

Want to pause the multiple views from updating when Pause button is pressed
In h file
#property BOOL appIsPaused;
In m file
#synthesize appIsPaused;
-(void)playpauseAction:(id)sender
{
if
([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateSelected];
[audioPlayer pause];
appIsPaused = YES;
} else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
appIsPaused = NO;
[self performSelector:#selector(displayviewsAction:) withObject:nil afterDelay:11.0];
}
}
- (void)displayviewsAction:(id)sender
{
FirstViewController *viewController = [[FirstViewController alloc] init];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
[self performSelector:#selector(secondViewController) withObject:nil afterDelay:23];
[viewController release];
}
-(void)secondViewController {
SecondViewController *secondController = [[SecondViewController alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[self performSelector:#selector(ThirdviewController) withObject:nil afterDelay:27];
[secondController release];
}
and it goes on like this for multiple views.
Any ideas how to pause views from updating whenever pause button is pressed.

Instead of using performSelector after delay, you should consider using a NSTimer.
Like this:
Declare a NSTimer *timer ivar.
Declare a NSUInteger viewControl;
Set viewControl to 0;
On the play part of the method add this line:
timer = [NSTimer scheduledTimerWithTimeInterval:11 target:self selector:#selector(tick) userInfo:nil repeats:YES];
-(void)tick
{
switch(viewControl)
{
case 0:
[self performSelector:#selector(firstViewController) withObject:nil];
break;
case 1:
[self performSelector:#selector(secondViewController) withObject:nil];
break;
case 2:
[self performSelector:#selector(thirdViewController) withObject:nil];
break;
.
.
.
default:
break;
}
viewControl++;
if(viewControl > MAX_VIEWS)
{
viewControl = 0;
}
}
And add this line on pause action:
[timer invalidate]
It is also cleaner and let you have more control over your code.
Hope it helps.

Related

UIControlEventValueChanged not working when i tapped an already selected segment section

My aim is to trigger UIControlEventValueChanged delegate again when i tapped an already selected segment .
So i have referred the below links
uisegmentedcontrol-register-taps-on-selected-segment
maintaining-selection-in-uisegmentedcontrol
As per the links i have created a uisegmentedcontrol subclass , implemented the - (void) setSelectedSegmentIndex:(NSInteger)toValue { method and wrote the codes (from the above links).
Its worked fine with Ipad1. However the setSelectedSegmentIndex in the inherited/sub class is not triggering in Ipad2 when tapped an already selected segment!!!! however it is working fine when i tapped an unselected segment index.
The code i done is below
Main Class
ReselectableSegmentControl *firstNextSegmentedControl = [[ReselectableSegmentControl alloc] init];
firstNextSegmentedControl.frame = CGRectMake(xCordinate, 255, 188, 50);
firstNextSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
firstNextSegmentedControl.tag = index + 1;
[firstNextSegmentedControl addTarget:self action:#selector(firstNextThenSegmentedControlValueChanged:) forControlEvents:UIControlEventValueChanged];
[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:#"NoTimerCounter.png"] atIndex:0 animated:YES];
[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:#"Timer.png"] atIndex:1 animated:YES];
[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:#"Counter.png"] atIndex:2 animated:YES];
firstNextSegmentedControl.selectedSegmentIndex = 0;
[self.view addSubview:firstNextSegmentedControl];
[firstNextSegmentedControl release];
firstNextSegmentedControl = nil;
UISegmentSubClass .h file
#import <Foundation/Foundation.h>
#interface ReselectableSegmentControl : UISegmentedControl {
}
#end
UISegmentSubClass .m file
-(void)setSelectedSegmentIndex:(NSInteger )selectedValue{
if (selectedValue==self.selectedSegmentIndex) {
[self sendActionsForControlEvents:UIControlEventValueChanged];
}
[super setSelectedSegmentIndex:selectedValue];
}
or
-(void)setSelectedSegmentIndex:(NSInteger )selectedValue{
if (self.selectedSegmentIndex == selectedValue) {
//doing my operation if tapped again the selected value.
else {
super.selectedSegmentIndex = selectedValue;
}
}
or
- (void) setSelectedSegmentIndex:(NSInteger)toValue {
if (self.selectedSegmentIndex == toValue) {
[super setSelectedSegmentIndex:UISegmentedControlNoSegment];
} else {
[super setSelectedSegmentIndex:toValue];
}
}
The all above methods are not working with Ipad2.
Any help on this issue is appreciated.
Thanks.
Phew... finally i got the solution..
I have used UITapGestureRecognizer instead of using addTarget:self action:#selector()
Now my code is looking like,
UISegmentedControl *firstNextSegmentedControl = [[UISegmentedControl alloc] init];
firstNextSegmentedControl.frame = CGRectMake(xCordinate, 255, 188, 50);
firstNextSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
firstNextSegmentedControl.selectedSegmentIndex = 0;
[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:#"NoTimerCounter.png"] atIndex:0 animated:YES];
[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:#"Timer.png"] atIndex:1 animated:YES];
[firstNextSegmentedControl insertSegmentWithImage:[UIImage imageNamed:#"Counter.png"] atIndex:2 animated:YES];
//Added UITapGestureRecognizer instead of using addTarget: method
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] init];
tapGesture addTarget:self action:#selector(firstNextThenSegmentedControlValueChanged:) ];
[firstNextSegmentedControl addGestureRecognizer:tapGesture];
[tapGesture release];
tapGesture = nil;
[self.view addSubview:firstNextSegmentedControl];
[firstNextSegmentedControl release];
firstNextSegmentedControl = nil;
-(void)firstNextThenSegmentedControlValueChanged:(UITapGestureRecognizer *)tapGesture{
UISegmentedControl *segmentControl = (UISegmentedControl *)tapGesture.view;
switch (segmentControl.selectedSegmentIndex) {
case 0:
[self doIt:segmentControl];
break;
default:
break;
}
}

[__NSDate timeIntervalSinceNow]: message sent to deallocated instance

App crashed with the message when hitting rewind twice just to test app on iPhone device [__NSDate timeIntervalSinceNow]: message sent to deallocated instance
-(void)pauseTimer{
pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
previousFireDate = [[timer fireDate] retain];
[timer setFireDate:[NSDate distantFuture]];
}
-(void)dealloc
{
[audioPlayer release];
[pauseStart release];
[previousFireDate release];
[super dealloc];
}
-(void)playpauseAction:(id)sender {
if([audioPlayer isPlaying])
{
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
[self pauseLayer:self.view.layer];
}else{
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
[self resumeLayer:self.view.layer];
if(isFirstTime == YES)
{
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
isFirstTime = NO;
}
}
}
-(void)rewind:(id)sender{
[timer invalidate];
audioPlayer.currentTime = 0;
MainViewController *viewController = [[[MainViewController alloc] init]autorelease];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
[self playpauseAction:_playButton];
}
when hitting rewind button again and purposely to test app on iphone device app crashes and gives message [__NSDate timeIntervalSinceNow]: message sent to deallocated instance
Any ideas what is wrong.
Appreciate help.
Thanks.
Removed
[pauseStart release];
[previousFireDate release];
from the dealloc method.
That solved my problem.

Warning message Attempting to create USE_BLOCK_IN_FRAME variable with block that isn't in the frame

When setting exception breakpoint and debugging getting warning message that Catchpoint 2 (exception thrown).Current language: auto; currently objective-c
warning: Attempting to create USE_BLOCK_IN_FRAME variable with block that isn't in the frame.
(gdb)
What does it means.
In my case in the mainviewcontroller on UIToolbar have plapause toggle button which when hits plays audio file and loads 20 view controllers one after the other by using perform selector
-(void)playpauseAction:(id)sender
{
if
([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateSelected];
[audioPlayer pause];
} else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self performSelector:#selector(displayviewsAction:) withObject:nil afterDelay:11.0];
}
}
- (void)displayviewsAction:(id)sender
{
FirstViewController *viewController = [[FirstViewController alloc] init];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
[self performSelector:#selector(secondViewController) withObject:nil afterDelay:23];
[viewController release];
}
-(void)secondViewController {
SecondViewController *secondController = [[SecondViewController alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[self performSelector:#selector(ThirdviewController) withObject:nil afterDelay:27];
[secondController release];
}
When simply try to run the application in simulator it works fine but when debug with exception breakpoint and hits play button to start debugging gets this warning message.
Anyone have ideas what is going on with exception breakpoint.
It's a warning from the debugger,when it could not create any info struct for a particular object.
Here is an example :
NSString* string = [[NSString alloc] initWithUTF8String:line];
in this line of code the method initWithUTF8String: expects an UTF-8 encoded C string and not a NSString object.
so this time complier will give an warrning of USE_BLOCK_IN_FRAME.
We should to insure that all warrnings are resolve ,because it can be big error or app may crashed in near future .
Update:
-(void)playpauseAction:(id)sender
{
UIButton *btn = (UIButton *)sender;
if([audioPlayer isPlaying]){
[audioPlayer pause];
[btn setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
} else {
[audioPlayer play];
[btn setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateNormal];
[self performSelector:#selector(displayviewsAction) withObject:nil afterDelay:11.0];
}
}
- (void)displayviewsAction
{
FirstViewController *viewController = [[FirstViewController alloc] initWithNibName:"FirstViewController"];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
[self performSelector:#selector(secondViewController) withObject:nil afterDelay:23];
[viewController release];
}
-(void)secondViewController {
SecondViewController *secondController = [[SecondViewController alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[self performSelector:#selector(ThirdviewController) withObject:nil afterDelay:27];
[secondController release];
}

How to resume updating views when resumed AVAudioplayer

I m struggling with this issue for so long but not able to find any solution for this
The issue i have is when i pause the audioplayer i want views to be paused from updating so i used timer invalidate to pause the views from loading but when i resume play only Audioplayer is resumed not the views cause i invalidated the timer.
Now i how i can resume updating views from that point where view was paused. By starting the timer again i can start updating views again but how program will know that at what view it was paused and it should resume updating views from that point. There are multiple views involved displayed code for only two.
-(void)playpauseAction:(id)sender
{
if
([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"play.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[timer invalidate];
} else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11 target:self selector:#selector(displayviewsAction:) userInfo:nil repeats:NO];
}
}
- (void)displayviewsAction:(id)sender
FirstViewController *viewController = [[FirstViewController alloc] init];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
[viewController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:23 target:self selector:#selector(secondViewController) userInfo:nil repeats:NO];
-(void)secondViewController {
SecondViewController *secondController = [[SecondViewController alloc] init];
secondController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:secondController.view];
[self.view addSubview:toolbar];
[secondController release];
self.timer = [NSTimer scheduledTimerWithTimeInterval:27 target:self selector:#selector(ThirdviewController) userInfo:nil repeats:NO];
}
Thanks and appreciate for your answers and helping me out with this issue.
Use a background thread to basically bind the view to the player, something like this:
UIImageView* imageView; //the view that gets updated
int current_selected_image_index = 0;
float time_for_next_image = 0.0;
AVAudioPlayer* audioPlayer;
NSArray* image_times; //an array of float values representing each
//time when the view should update
NSArray* image_names; //an array of the image file names
-(void)update_view
{
UIImage* next_image_to_play = [image_names objectAtIndex:current_selected_image_index];
imageView.image = next_image_to_play;
}
-(void)bind_view_to_audioplayer
{
while(audioPlayer.isPlaying)
{
float currentPlayingTime = (float)audioPlayer.currentTime;
if(currentPlayingTime >= time_for_next_image)
{
current_selected_image_index++;
[self performSelectorOnMainThread:#selector(update_view) withObject:nil waitUntilDone:NO];
time_for_next_image = [image_times objectAtIndex:[current_selected_image_index+1)];
}
[NSThread sleep:0.2];
}
}
-(void)init_audio
{
current_selected_image_index = 0;
time_for_next_image = [image_times objectAtIndex:1];
}
-(void)play_audio
{
[audioPlayer play];
[self performSelectorInBackground:#selector(bind_view_to_audioplayer) withObject:nil];
}
-(void)pause_audio
{
[audioPlayer pause];
//that's all, the background thread exits because audioPlayer.isPlaying == NO
//the value of current_selected_image_index stays where it is, so [self play_audio] picks up
//where it left off.
}
also, add self as observer for the audioPlayerDidFinishPlaying:successfully: notification to reset when the audioplayer finishes playing.

Why does the AVAudioPlayer stop method not work when switching views

I have an iphone app that displays images and plays audio selected from a main menu.
The user clicks on a button to select the image/audio combo they want. The code to switch the views with animation works fine.
All of the code to display the image, play, pause, scrub, and stop the audio while in the new view works fine too.
However, when the users clicks the Main Menu button I want the playing audio to stop. I am using viewWillDisappear:(BOOL)animated to call the stop method:
-(void)viewWillDisappear:(BOOL)animated {
audioPlayer.stop;
[super viewWillDisappear: animated];}
This code doesn't stop the sound when the user switches back to the main menu view. Is there a better way to do this? Am I doing something wrong?
Here is the code from the entire class where the snippet above resides:
#import "twelthPoem.h"
UIImageView *largeImageView;
#implementation twelthPoem
-(void)resetControls
{
audioPlayer.currentTime = 0;
scrubber.value = 0;
[playButton setImage:[UIImage imageNamed:#"play_HL.png"]
forState:UIControlStateNormal];
}
-(void)play:(id)sender {
if (! audioPlayer.playing) {
audioPlayer.play;
[playButton setImage:[UIImage imageNamed:#"pauseHL.png"] forState:UIControlStateNormal];
}
else {
audioPlayer.pause;
[playButton setImage:[UIImage imageNamed:#"play_HL.png"] forState:UIControlStateNormal];
}
[self becomeFirstResponder];
}
-(void)stop:(id)sender {
audioPlayer.stop;
[self resetControls];
}
-(void)changeVolume:(id)sender {
audioPlayer.volume = volume.value;
[self becomeFirstResponder];
}
-(void)scrub:(id)sender {
if (audioPlayer.playing) {
audioPlayer.pause;
audioPlayer.currentTime = scrubber.value;
audioPlayer.play;
}
else
audioPlayer.currentTime = scrubber.value;
[self becomeFirstResponder];
}
-(void)createControls {
//play/pause button
playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton setFrame:CGRectMake(60,405,80,20)];
[playButton setImage:[UIImage imageNamed:#"play_HL.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:#selector(play:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:playButton];
//stop button
stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
[stopButton setFrame:CGRectMake(180,405,80,20)];
[stopButton setImage:[UIImage imageNamed:#"stopHL.png"] forState:UIControlStateNormal];
[stopButton addTarget:self action:#selector(stop:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:stopButton];
//volume control
volume = [[UISlider alloc] initWithFrame:CGRectMake(10,445,145,20)];
[volume addTarget:self action:#selector(changeVolume:)
forControlEvents:UIControlEventValueChanged];
volume.minimumValue = 0.0;
volume.maximumValue = 1.0;
volume.value = audioPlayer.volume;
volume.continuous = YES;
[self.view addSubview:volume];
//scrubber control
scrubber = [[UISlider alloc] initWithFrame:CGRectMake(165,445,145,20)];
[scrubber addTarget:self action:#selector(scrub:)
forControlEvents:UIControlEventValueChanged];
scrubber.minimumValue = 0.0;
scrubber.maximumValue = audioPlayer.duration;
scrubber.value = audioPlayer.currentTime;
scrubber.continuous = NO;
[self.view addSubview:scrubber];
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
switch (event.subtype)
{
case UIEventSubtypeRemoteControlTogglePlayPause:
[self play:nil];
break;
case UIEventSubtypeRemoteControlNextTrack:
//do nothing
break;
case UIEventSubtypeRemoteControlPreviousTrack:
//do nothing
break;
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
-(void)viewDidLoad {
//for scrolling the image
[super viewDidLoad];
CGRect scrollFrame = CGRectMake(0,40,320,350);
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollFrame];
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 1.5;
scrollView.delegate = self;
UIImage *bigImage = [UIImage imageNamed:#"birches.png"];
largeImageView = [[UIImageView alloc] initWithImage:bigImage];
[scrollView addSubview:largeImageView];
scrollView.contentSize = largeImageView.frame.size; //important!
[self.view addSubview:scrollView];
[scrollView release];
//for playing the recording
NSString *filePath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"birches_final_mp3.mp3"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSError *error = nil;
OSStatus status = AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
status = AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
sizeof (sessionCategory),
&sessionCategory);
AudioSessionSetActive(YES);
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL
error:&error];
if (error )
NSLog(#"An error occurred: %#",error);
else
{
audioPlayer.volume = 0.3;
[audioPlayer prepareToPlay];
[self createControls];
}
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return largeImageView;
}
- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
interrupted = audioPlayer.playing;
}
- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
if (interrupted)
audioPlayer.play;
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
successfully:(BOOL)flag
{
[self resetControls];
}
-(void)viewWillDisappear:(BOOL)animated {
audioPlayer.stop;
[super viewWillDisappear: animated];
}
- (void)dealloc {
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[scrubber release];
[volume release];
[audioPlayer release];
[super dealloc];
}
#end
You call methods in objective-c using the following syntax.
[audioPlayer stop];
audioPlayer.stop will not work.
Same goes for other places as well.
audioPlayer.stop will not work mostly because it needs an expression after it, e.g. audioPlayer.stop = //expression, stop is a bool, so you can say audioPlayer.stop = YES; or [audioPlayer stop];