【问题标题】:how to continue the song from pause to play in ios如何在ios中从暂停继续播放歌曲
【发布时间】:2014-06-05 00:08:24
【问题描述】:

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

【问题讨论】:

标签: ios objective-c avaudioplayer


【解决方案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;
}

【讨论】:

  • 它对我来说工作正常,您能否详细说明您的问题,以便我详细查看。
  • 我的问题在于歌曲播放时间如果我点击暂停按钮歌曲应该进入暂停模式。然后我再次点击播放按钮然后它应该从开始开始。但我需要继续播放歌曲播放按钮。
  • 你想从头开始播放歌曲吗?然后保持 player=nil;在您停止或暂停播放器的块中。
  • 兄弟。不正常。请来堆栈溢出聊天:房间名称Talk-4-ios。我在等你。我有很多疑问
【解决方案2】:

看起来您的问题是每次调用 - 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];
}

【讨论】:

    猜你喜欢
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多