views:

127

answers:

2

Hey so i am just making an example application and i am having a little trouble, So i have a table view and then i have a few rows and when a user clicks a row it takes them to a new view. On this view i have a button to play music. I am using a timer to make a slider bar increase based on music duration and remaining time.

Now my problem is, what do i have to put so that when i go back to the table view via the top left button, that the NStimer stops?

this is what i have so far, i cannot get a repeat: YES timer to stop.

#import "SecondViewController.h"


@implementation SecondViewController
@synthesize lbl1;
@synthesize timer;
-(IBAction) slide {

 myMusic.currentTime = slider.value;
}


-(IBAction)play
{

timer=  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
 slider.maximumValue = [myMusic duration];
 myMusic.volume = 0.2;
 [myMusic prepareToPlay];
 [myMusic play];





}


-(IBAction)pause
{
 [myMusic pause];

}

-(IBAction)stop
{

 slider.value = 0;
 myMusic.currentTime = 0;
 [myMusic stop];

}

- (void)updateTime{


  slider.value = myMusic.currentTime;



}






/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

  //This plays music, we must give it a path to find the file and then u can change it. 
 NSString * pathToMusicFile = [[NSBundle mainBundle] pathForResource:@"Katy" ofType:@"mp3"];
     myMusic = [[ AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:pathToMusicFile] error:NULL];
     myMusic.delegate = self;
     myMusic.numberOfLoops = -1;

 slider.value = 0;
 //[myMusic play];



    [super viewDidLoad];
}



/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/



- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {

 [timer invalidate];
 timer = nil;
 //myMusic.currentTime = 0;
 [myMusic stop];
 [super viewDidUnload];



    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(IBAction) TxtChange;{

 lbl1.text = @" test2! I CHNAGED TEXT FROM ANOTHER XIB";


}

- (void)dealloc {
 [timer invalidate];
 timer = nil; 
 [myMusic release];
    [super dealloc];




}



@end
A: 

like this:

[yourtimername invalidate];

But be careful, you cant stop the timer if it's already stopped because your app will crash.

DailyDoggy
A: 

Use the below code. It will work But keep in mind that it must only be called if our timer is in running mode else the application will get crashed.

- (void)viewWillDisappear:(BOOL)animated {
    //BEFORE DOING SO CHECK THAT TIMER MUST NOT BE ALREADY INVALIDATED
     [timer invalidate];
     timer = nil;
     }

Happy Coding...

Suriya
I am going to go try this in the morning, its 2 AM, been stuck on this all day. Hopefully your solution works. ill get back to this post in a few hrs.
cruisx