gpt4 book ai didi

xcode - 如何在播放时更新音频的播放时间,使用 AVAudioPlayer

转载 作者:行者123 更新时间:2023-12-04 19:39:27 25 4
gpt4 key购买 nike

我想在播放音频时在标签上显示播放时间/总持续时间。我见过 AVAudioPlayerDelegate 方法,但我找不到这样的方法,它在播放后每秒通知它。我是这样做的:

-(IBAction)playAudio{

NSBundle *bundle = [NSBundle mainBundle];
NSString *audioPath = [bundle pathForResource:@"PhirMohobat" ofType:@"mp3"];
NSURL *audioUrl = [NSURL fileURLWithPath:audioPath];


player = [[AVAudioPlayer alloc]initWithContentsOfURL:audioUrl error:NULL];
player.delegate = self;

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
[player play];

}

-(void)updateTime
{

float minutes = floor(player.currentTime/60);
float seconds = player.currentTime - (minutes * 60);

float duration_minutes = floor(player.duration/60);
float duration_seconds = player.duration - (duration_minutes * 60);

NSString *timeInfoString = [[NSString alloc]
initWithFormat:@"%0.0f.%0.0f / %0.0f.%0.0f",
minutes, seconds,
duration_minutes, duration_seconds];


timerLabel.text = timeInfoString;
[timeInfoString release];

在 AVAudioPlayer 或 xcode 支持的任何其他音频播放器中是否有任何隐式委托(delegate)方法来执行此操作?

最佳答案

AVAudioPlayer 有一个名为 currentTime 的属性,因此您可以在某个时间间隔内使用 NSTimer 询问此属性的实际值是多少,然后根据需要根据此值进行更新。

关于xcode - 如何在播放时更新音频的播放时间,使用 AVAudioPlayer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8296807/

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