gpt4 book ai didi

ios - 根据 mp3 的播放量更新 UIProgressView

转载 作者:行者123 更新时间:2023-11-28 22:09:57 25 4
gpt4 key购买 nike

我一直在寻找一种简单的方法来在 UIProgressView 中显示播放 MP3 的进度。我有一种感觉,它将涉及一个 NSTimer 对象,但我不确定如何实现它。

这是我目前所拥有的。

#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UISlider *VolumeLevel;
@property (weak, nonatomic) IBOutlet UIProgressView *progressBar;

@end

@implementation ViewController

AVAudioPlayer *player;


- (IBAction)play:(id)sender {

[player play];

}

- (IBAction)pause:(id)sender {

[player stop];

}
- (IBAction)volume:(id)sender {

player.volume = _VolumeLevel.value;

}

- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *songURL =[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Gregory Is Here" ofType:@"mp3"]];
player =[[AVAudioPlayer alloc]initWithContentsOfURL:songURL error:nil];
player.volume = 0.5;


// Do any additional setup after loading the view, typically from a nib.
}

最佳答案

使用 NSTImer,您可以:

- (IBAction)play:(id)sender {
[player play];
myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
}

- (IBAction)pause:(id)sender {
[player stop];
[myTimer invalidate];
}

- (void)updateProgressBar {
float progress = player.currentTime / player.duration;
[myProgressBar setProgress:progress animated:YES];
}

您可以更改计时器的 timeInterval 参数以增加或减少更新进度条的频率(1.0 为 1 秒)。

关于ios - 根据 mp3 的播放量更新 UIProgressView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23151775/

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