如何从暂停播放歌曲到ios中播放

时间:2014-06-04 09:13:09

标签: ios objective-c avaudioplayer

我正在使用AVAudio播放器框架在iPhone中使用音频播放器。我的问题是当我选择暂停按钮并再次播放按钮时它应该从启动开始。我想暂停播放该歌曲并继续使用iphone时的持续时间点编程。当我试图暂停这首歌时,我又想开始播放我暂停的歌曲。 我正在写这样的代码..

-(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];       
    }
}

2 个答案:

答案 0 :(得分:1)

在播放按钮操作中,请保留以下代码。

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;
}

答案 1 :(得分:1)

看起来您的问题是,每次调用AVAudioPlayer方法时,您都会重新创建- playOrPauseButtonPressed:

对于以下代码行:

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语句中,这样只有在AVAudioPlayer未初始化时才会调用它们,如下所示:

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];
}