gpt4 book ai didi

ios - 如何在 ios 中从暂停继续播放歌曲

转载 作者:行者123 更新时间:2023-11-29 10:42:08 24 4
gpt4 key购买 nike

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

关于ios - 如何在 ios 中从暂停继续播放歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24033409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com