使用 NSTimer 暂停/播放音频播放器 iOS Objective C
Pause/ Play Audio Player with NSTimer iOS Objective C
我正在开发一个音乐播放器应用程序。并在播放音频时使用 Play/Pause 按钮和进度条。我正在调用一个带有 NSTimer 的方法来更新进度条。我需要的是暂停进度条并在停止的地方恢复它。
我可以使用链接中引用的代码暂停计时器和进度条:
1. How to pause a NSTimer?
2. How to Pause/Play NSTimer?
但我正在努力恢复 ii 中断的地方。我还需要根据音频更新进度条。
我的代码如下:
AVAudioPlayer *audioPlayer;
NSTimer *audioTimer;
BOOL play;
BOOL ispaused;
int seconds1;
int minutes1;
int hours1;
NSDate *pauseStart, *previousFireDate;
Play/Pause 按钮动作:
- (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;
}
}
更新进度条:
- (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 的初学者,我不知道我哪里做错了。
谁能帮帮我……?
我在我的一个项目中正在做什么,
- (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
object,sliderTimer
是NSTimer
object, mySlider
就是 UISlider
object 来表示进度。就是这样。您可以按照我提到的方式管理您的案件。
和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
,那么 sliderChanged
将被调用,您的播放器将到达该点。
我明白了!
我只是在暂停功能中添加以下内容
rate = CMTimeGetSeconds([_player currentTime])/60;
并参考 ketan Parmer 的回答
将其设置为音频播放器的当前时间
[audioPlayer setCurrentTime:rate];
停止时
rate = 0.0;
并且 rate 是一个全局声明的浮点数。感谢 Ketan..!
我正在开发一个音乐播放器应用程序。并在播放音频时使用 Play/Pause 按钮和进度条。我正在调用一个带有 NSTimer 的方法来更新进度条。我需要的是暂停进度条并在停止的地方恢复它。
我可以使用链接中引用的代码暂停计时器和进度条: 1. How to pause a NSTimer? 2. How to Pause/Play NSTimer?
但我正在努力恢复 ii 中断的地方。我还需要根据音频更新进度条。
我的代码如下:
AVAudioPlayer *audioPlayer;
NSTimer *audioTimer;
BOOL play;
BOOL ispaused;
int seconds1;
int minutes1;
int hours1;
NSDate *pauseStart, *previousFireDate;
Play/Pause 按钮动作:
- (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;
}
}
更新进度条:
- (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 的初学者,我不知道我哪里做错了。 谁能帮帮我……?
我在我的一个项目中正在做什么,
- (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
object,sliderTimer
是NSTimer
object, mySlider
就是 UISlider
object 来表示进度。就是这样。您可以按照我提到的方式管理您的案件。
和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
,那么 sliderChanged
将被调用,您的播放器将到达该点。
我明白了!
我只是在暂停功能中添加以下内容
rate = CMTimeGetSeconds([_player currentTime])/60;
并参考 ketan Parmer 的回答
将其设置为音频播放器的当前时间[audioPlayer setCurrentTime:rate];
停止时
rate = 0.0;
并且 rate 是一个全局声明的浮点数。感谢 Ketan..!