user3655174
user3655174

Reputation: 83

how to continue the song from pause to play in ios

I am working audio player in iPhone using AVAudio Player framework.My problem is when I choose Pause button and again play button then It should start from starting.I want to pause the song and continue from that point of duration in iphone using programmatically. When I tried to pause the song and again I want to start to play the song where I paused. I am writing my code like this..

-(void)playOrPauseButtonPressed:(id)sender
{
    if(playing==NO)
    {
        [playButton setBackgroundImage:[UIImage imageNamed:@"Pause.png"] forState:UIControlStateNormal];
        // Here Pause.png is a image showing Pause Button.
        NSError *err=nil;
        AVAudioSession *audioSession=[AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];            
        NSLog(@"%@ %d",urlsArray,selectedIndex);            
        NSString *sourcePath=[urlsArray objectAtIndex:selectedIndex];
        NSData *objectData=[NSData dataWithContentsOfURL:[NSURL URLWithString:sourcePath]];            
        NSLog(@"%@",objectData);
        audioPlayer = [[AVAudioPlayer alloc] initWithData:objectData error:&err];
        if(err)
        {
            NSLog(@"Error %ld,%@",(long)err.code,err.localizedDescription);
        }
        NSTimeInterval bufferDuration=0.005;
        [audioSession setPreferredIOBufferDuration:bufferDuration error:&err];
        if(err)
        {
            NSLog(@"Error %ld, %@", (long)err.code, err.localizedDescription);
        }
        double sampleRate = 44100.0;
        [audioSession setPreferredSampleRate:sampleRate error:&err];
        if(err)
        {
            NSLog(@"Error %ld, %@",(long)err.code,err.localizedDescription);
        }
        [audioSession setActive:YES error:&err];
        if(err)
        {
            NSLog(@"Error %ld,%@", (long)err.code, err.localizedDescription);
        }
        sampRate=audioSession.sampleRate;
        bufferDuration=audioSession.IOBufferDuration;
        NSLog(@"SampeRate:%0.0fHZI/OBufferDuration:%f",sampleRate,bufferDuration);
        audioPlayer.numberOfLoops = 0;
        [audioPlayer prepareToPlay];
        [audioPlayer play];
        audioPlayer.delegate=self;
        if(!audioPlayer.playing)                
        {                
            [audioPlayer play];                
        }            
        playing=YES;            
    }        
    else if (playing==YES)            
    {            
        [playButton setBackgroundImage:[UIImage imageNamed:@"play12.png"] forState:UIControlStateNormal];            
        [audioPlayer pause];            
        playing=NO;            
        timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateViewForPlayerState) userInfo:nil repeats:YES];            
    }        
    if (self.audioPlayer)            
    {            
        [self updateViewForPlayerInfo];            
        [self updateViewForPlayerState];            
        [self.audioPlayer setDelegate:self];
    }
}

-(void)updateViewForPlayerInfo
{
    self.songDuration.text = [NSString stringWithFormat:@"%d:%02d", (int)self.audioPlayer.duration / 60, (int)self.audioPlayer.duration % 60, nil];
    NSLog(@"%f", self.audioPlayer.duration);
    self.progressBar.maximumValue = self.audioPlayer.duration;
    self.volumeSlider.value = self.audioPlayer.volume;        
}

-(void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
{
    if(playing)
    {
        playing=NO;
        interruptedOnPlayback=YES;
        [self updateViewForPlayerState];        
    }
}

-(void)audioPlayerEndInterruption:(AVAudioPlayer *)player
{
    if(interruptedOnPlayback)
    {
        [audioPlayer prepareToPlay];
        [audioPlayer play];
        playing=YES;
        interruptedOnPlayback=NO;
    }
}

-(void)updateViewForPlayerState
{
    [self updateCurrentTime];
    if (self.updatedTimer)
    {
        [self.updatedTimer invalidate];
    }
    if (self.audioPlayer.playing)       
    {
        self.updatedTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updateCurrentTime) userInfo:self.audioPlayer repeats:YES];       
    }
}

Upvotes: 1

Views: 942

Answers (2)

Matt Cooper
Matt Cooper

Reputation: 2062

It looks like your issue is that you're recreating the AVAudioPlayer every time you call the - playOrPauseButtonPressed: method.

For the following lines of code:

NSError *err=nil;
AVAudioSession *audioSession=[AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
NSLog(@"%@ %d",urlsArray,selectedIndex);
NSString *sourcePath=[urlsArray objectAtIndex:selectedIndex];
NSData *objectData=[NSData dataWithContentsOfURL:[NSURL URLWithString:sourcePath]];
NSLog(@"%@",objectData);
audioPlayer = [[AVAudioPlayer alloc] initWithData:objectData error:&err];

Try wrapping them in an if-statement so they are only called when the AVAudioPlayer isn't initialized, like this:

if (!audioPlayer){
    NSError *err=nil;
    AVAudioSession *audioSession=[AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
    NSLog(@"%@ %d",urlsArray,selectedIndex);
    NSString *sourcePath=[urlsArray objectAtIndex:selectedIndex];
    NSData *objectData=[NSData dataWithContentsOfURL:[NSURL URLWithString:sourcePath]];
    NSLog(@"%@",objectData);
    audioPlayer = [[AVAudioPlayer alloc] initWithData:objectData error:&err];
}

Upvotes: 1

abhi
abhi

Reputation: 620

In your play button action keep the following code.

if(!isPlaying)
{
    [playButton setBackgroundImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
    [player stop];
    isPlaying=YES;
}
else
{
    [playButton setBackgroundImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
    [player play];
    isPlaying=NO;
}

Upvotes: 1

Related Questions