我正在开发音乐播放器应用程序。播放音频时使用播放/暂停按钮和进度条。我正在用NSTimer调用一个方法来更新进度条。我需要的是暂停进度条并将其恢复到停止的位置。
我可以使用链接中引用的代码暂停计时器以及进度条: 1. How to pause a NSTimer? 2. How to Pause/Play NSTimer?
但我正在努力恢复它,因为我被迫离开了。我还需要根据音频更新进度条。
我的代码如下:
AVAudioPlayer *audioPlayer;
NSTimer *audioTimer;
BOOL play;
BOOL ispaused;
int seconds1;
int minutes1;
int hours1;
NSDate *pauseStart, *previousFireDate;
播放/暂停按钮操作:
- (IBAction)playSoundTapped:(id)sender
{
songUrl = [NSURL URLWithString:@"http://www.stephaniequinn.com/Music/Allegro%20from%20Duet%20in%20C%20Major.mp3"];
self.playerItem = [AVPlayerItem playerItemWithURL:songUrl];
self.player = [AVPlayer playerWithPlayerItem:self.playerItem];
self.player = [AVPlayer playerWithURL:songUrl];
if (play==YES)
{
if (ispaused == YES)
{
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[audioTimer setFireDate:[NSDate dateWithTimeInterval:pauseTime sinceDate:previousFireDate]];
audioTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];
[self.player play];
ispaused = NO;
}
else
{
audioTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];
[self.player play];
ispaused = NO;
}
_playImg.image=[UIImage imageNamed:@"Pause.png"];
play=NO;
}
else
{
[self.player pause];
ispaused = YES;
pauseStart = [NSDate dateWithTimeIntervalSinceNow:0];
previousFireDate = [audioTimer fireDate];
[audioTimer setFireDate:[NSDate distantFuture]];
_playImg.image=[UIImage imageNamed:@"Play.png"];
play=YES;
}
}
更新ProgressBar:
- (void)updateProgress
{
if (ispaused == NO)
{
seconds1 += 1;
if (seconds1 == 60)
{
seconds1 = 0;
minutes1++;
if (minutes1 == 60)
{
minutes1 = 0;
hours1++;
}
}
float duration = CMTimeGetSeconds([_playerItem duration]);
float actDuration = duration/100;
float currentTime = CMTimeGetSeconds([_player currentTime]);
float minutes = currentTime/60;
//float seconds = (currentTime - minutes) * 60;
NSLog(@"Duration: %.2f",actDuration);
NSLog(@"Current Time: %.2f",minutes);
NSString *durStr = [NSString stringWithFormat: @"%f",duration];
if ([durStr isEqual: @"nan"])
{
NSLog(@"Nan");
}
else
{
[self setProgress:minutes animated:YES];
}
}
else
{
NSLog(@“Paused”);
}
}
重启音频而不是重新开始。由于我是iOS的初学者,我不知道我在哪里做错了。 任何人都可以帮助我......?
答案 0 :(得分:2)
我在项目中做了什么,
- (void)playPauseAudio : (UIButton*)sender{
if (!playPause.selected) {
[audioPlayer play];
sliderTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];
[mySlider addTarget:self action:@selector(sliderChanged:) forControlEvents:UIControlEventValueChanged];
mySlider.maximumValue = audioPlayer.duration;
}
else{
[audioPlayer pause];
[sliderTimer invalidate];
}
playPause.selected = !playPause.selected;
}
我有播放和暂停的常用按钮,它只是将normal state
和selected state
的标题分别更改为play
和pause
。
因此,在我的方法中,playPause
是按钮,audioPlayer
是AVAudioPlayer
对象,sliderTimer
是NSTimer
对象,mySlider
是{{ 1}}对象显示进度。而已。您可以像我提到的那样管理您的案例。
UISlider
和updateSlider
方法如下所示,
sliderChanged
因此,如果用户手动更改 - (void)updateSlider{
mySlider.value = audioPlayer.currentTime;
timerLabel.text = [self timeFormatted:audioPlayer.duration - audioPlayer.currentTime];
NSLog(@"update slider call");
}
- (void)sliderChanged : (UISlider*) sender{
[audioPlayer setCurrentTime:mySlider.value];
timerLabel.text = [self timeFormatted:audioPlayer.duration - mySlider.value];
}
,则UISlider
将被调用,您的播放器将在此时到达。
答案 1 :(得分:1)
我明白了!
我只是在暂停功能中添加以下内容
rate = CMTimeGetSeconds([_player currentTime])/60;
并将其设置为音频播放器的当前时间,参考来自凯尔纳的答案
[audioPlayer setCurrentTime:rate];
和停止时
rate = 0.0;
和rate是全局声明的float。感谢Ketan ..!