繁体   English   中英

如何从暂停继续播放歌曲以在iOS中播放

[英]how to continue the song from pause to play in ios

我正在使用AVAudio Player框架在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];       
    }
}

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

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

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

对于以下代码行:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM