I am trying to solve my other question. So i thought maybe with if else if else statement can solve my problem.
But i think i m missing expected expression in below if else else if statement.
-(void)playpauseAction:(id)sender
{
if ([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
} else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
}
else if
{
[audioPlayer play];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
}
}
Any idea what i m doing wrong over here.
Thanks.
You need to have the else if before the else block. And else if is a secondary conditional, it requires some kind of boolean statement following it like a normal if, such as ![audioPlayer isPlaying]. I'm not sure what you're trying to achieve with your code, so I can't provide any more advice beyond that.
use it in this way
-(void)playpauseAction:(id)sender
{
if([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
}
else if(put your condition here)
{
[audioPlayer play];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
}
else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
}
}
hope this will help you..
How can you have if ... else ... elseif?
Is your code not just in the wrong order?
Pretty sure you can't have an elseif of an else.
if elseif else?
Related
When repeat button is hit for the first time then it should run infinite times and when it is hit for second time then it should not repeat audio rather it should stop looping. . Below is the code it works very well for the first time but when it is pressed again it is not stopping looping.
BOOL isFirstTime;
#interface English : UIViewController <UITextViewDelegate, ADBannerViewDelegate, UIScrollViewDelegate,
-(void) RepeatAction:(id)sender{
if(isFirstTime == YES){
player.numberOfLoops = -1;
} else {
player.numberOfLoops = 0;
}
}
- (void)playAction:(id)sender
{
if([player isPlaying])
{
[sender setImage:[UIImage imageNamed:#"1play.png"] forState:UIControlStateSelected];
[player pause];
}else{
[sender setImage:[UIImage imageNamed:#"audiopause.png"] forState:UIControlStateNormal];
[player play];
slidertimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(updateProgressBar:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:slidertimer forMode:NSRunLoopCommonModes];
timer = slidertimer;
}}
Thanks for help.
-(void) RepeatAction:(id)sender{
if(isFirstTime){ // even here you need not to compare with == YES
player.numberOfLoops = -1;
isFirstTime = NO;
} else {
player.numberOfLoops = 0;
isFirstTime = YES;
}
}
Have a UISlider ProgressBar on UIToolbar when audio plays wants UISlider ProgressBar to display the duration of the audio file and currenttime of the audio file.
- (void)playAction:(id)sender
{
if([player isPlaying])
{
[sender setImage:[UIImage imageNamed:#"1play.png"] forState:UIControlStateSelected];
[player pause];
//[self pauseTimer];
}else{
[sender setImage:[UIImage imageNamed:#"audiopause.png"] forState:UIControlStateNormal];
[player play];
//[self resumeTimer];
}
[self updateProgressBar:timer];
}
- (void)updateProgressBar:(NSTimer *)timer
{
NSTimeInterval playTime = [self.player currentTime];
NSTimeInterval duration = [self.player duration];
float progress = playTime/duration;
[_progressBar setProgress:progress];
}
But it is not working.
Thanks for help.
You're probably going to want to call the updateProgressBar method using a timer, rather than what you're doing right now (calling it in the playAction method). Instead, you can use the playAction method to create the timer that calls updateProgressBar, or pause/stop existing timers.
Looks like you already have an instance variable to keep track of the timer, which is good. Here is how you would create a new timer:
[timer invalidate]; // stop the old timer
// this timer runs once per second, perhaps you want to make it something shorter which would look less choppy
NSTimer *progressTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(updateProgressBar:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:progressTimer forMode:NSRunLoopCommonModes];
timer = progressTimer;
If you have a method to pause the audio, you can invalidate the timer there too.
[timer invalidate];
timer = nil;
Altogether, in your code, this would look something like:
- (void)playAction:(id)sender
{
if([player isPlaying])
{
[sender setImage:[UIImage imageNamed:#"1play.png"] forState:UIControlStateSelected];
[player pause];
[timer invalidate];
timer = nil;
} else {
[sender setImage:[UIImage imageNamed:#"audiopause.png"] forState:UIControlStateNormal];
[player play];
[timer invalidate];
NSTimer *progressTimer = [NSTimer timerWithTimeInterval:1.0 target:self selector:#selector(updateProgressBar:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:progressTimer forMode:NSRunLoopCommonModes];
timer = progressTimer;
}
}
- (void)updateProgressBar:(NSTimer *)timer
{
NSTimeInterval playTime = [self.player currentTime];
NSTimeInterval duration = [self.player duration];
float progress = playTime/duration;
[_progressBar setProgress:progress];
}
Try this:
This is for the update slider..
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateSlider) userInfo:nil repeats:YES];
slider.maximumValue = avAudioPlayer.duration;
[slider addTarget:self action:#selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
- (void)updateSlider {
slider.value = avAudioPlayer.currentTime;
}
- (IBAction)sliderChanged:(UISlider *)sender {
[avAudioPlayer stop];
[avAudioPlayer setCurrentTime:slider.value];
[avAudioPlayer prepareToPlay];
[avAudioPlayer play];
}
This rewind method is for rewind button as well as for play button.
-(void)rewind:(id)sender
{
[timer invalidate];
audioPlayer.currentTime = 0;
MainViewController *viewController = [[MainViewController alloc] init];
viewController.view.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:viewController.view];
[self.view addSubview:toolbar];
[viewController release];
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;
}
}
}
Now in this method sender is rewind button and toggling of play/pause is happening on rewind button whereas i want this toggling on play button rather then sender which is rewind button. So i tried giving the name of the actual button on which i want toggling to happen and i defined that button as play button in my code and whose action method i have added in this rewind method.
That's how defined the play button
//Add Play Button
UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playButton addTarget:self action:#selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
playButton.frame = CGRectMake(0, 0, 30, 30);
[playButton setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateNormal];
[playButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playButton];
and this is the method of play button which i added in the rewind button
-(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;
}
}
}
now problem i m facing at this time is that when i m clicking the rewind button toggling should happen on play button than rewind button.
But to fix this problem i simply replaced sender with playButton in the rewind method like this
[playButton setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
but then i get warning message in red that use of undeclared identifier playButton.
How can i tell rewind method to do toggling on playButton rather then on rewind button.
Please help.
Thanks.
Your "undeclared identifier playButton" is telling you that you don't have an instance variable playButton.
It is probably only visible in the method where you created it, but you need to save it into an iVar to be able to access it from the other method.
Trying to solve my Other question. This time trying this way
Added multiple methods to one button but when clicking on button it does nothing looks like those methods are not working.
//Add Play Button
UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
[playpauseButton addTarget:self action:#selector(playAction:) forControlEvents:UIControlEventTouchUpInside];
[playpauseButton addTarget:self action:#selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
playpauseButton.frame = CGRectMake(0, 0, 30, 30);
[playpauseButton setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateNormal];
[playpauseButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playpauseButton];
- (void)playAction:(id)sender{
[audioPlayer play];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
}
-(void)playpauseAction:(id)sender {
if ([audioPlayer isPlaying]){
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateSelected];
[audioPlayer pause];
[self pauseTimer];
} else {
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateNormal];
[audioPlayer play];
[self resumeTimer];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
}
}
-(void)pauseTimer{
pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
previousFireDate = [[timer fireDate] retain];
[timer setFireDate:[NSDate distantFuture]];
}
-(void)resumeTimer{
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[timer setFireDate:[previousFireDate initWithTimeInterval:pauseTime sinceDate:previousFireDate]];
[pauseStart release];
[previousFireDate release];
}
It shows play button when clicking on it nothing happens. It should play the audio file, start the NSTimer, start loading views and toggle between play and pause button. Any idea what is wrong.
Thanks.
When the user taps the button, the playAction: calls [audioPlayer play].
Immediately afterwards, the playpauseAction: evaluates [audioPlayer isPlaying] to YES and does [audioPlayer pause].
I can't get this code to work.
Declared playpauseButton just above the given below statements
UIButton *playpauseButton = [UIButton buttonWithType:UIButtonTypeCustom];
Getting local declaration of playpauseButton hides instance variable warning message for following statements
playpauseButton addTarget:self action:#selector(playpauseAction:) forControlEvents:UIControlEventTouchUpInside];
playpauseButton.frame = CGRectMake(0, 0, 30, 30);
[playpauseButton setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateNormal];
[playpauseButton setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
UIBarButtonItem *play = [[UIBarButtonItem alloc] initWithCustomView:playpauseButton];
After i defined playpauseAction method
-(void)playpauseAction:(id)sender {
if( playpauseButton.state == UIControlStateNormal ){
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
[audioPlayer play];
self.timer = [NSTimer scheduledTimerWithTimeInterval:11.0
target:self
selector:#selector(displayviewsAction:)
userInfo:nil
repeats:NO];
} else if (playpauseButton.state == UIControlStateSelected)
{
[sender setImage:[UIImage imageNamed:#"Play Icon.png"] forState:UIControlStateNormal];
[audioPlayer pause];
[self pauseTimer];
} else if (playpauseButton.state == UIControlStateNormal)
{
[sender setImage:[UIImage imageNamed:#"pause.png"] forState:UIControlStateSelected];
[audioPlayer play];
[self resumeTimer];}}
Some help please.
Thanks.
It's telling you that you have two declarations of playpauseButton. One is in the interface section of your class (the instance variable) and the other is a local variable.
It's not an actual error but the warning tells you that you may get results different from what you want.