gpt4 book ai didi

iOS - 如何在 AVPlayer 中实现 seektotime(从 URL 播放歌曲)?

转载 作者:行者123 更新时间:2023-11-29 01:43:57 25 4
gpt4 key购买 nike

我想在 AVPlayer 中实现 Scrubber (UISlider)。我试过但没有完全成功(有时 slider 显示不正确的值或有时进度条)。任何建议都会很棒。

-(void)playButtonClicked
{

// checks internet connnection : working or not

if([InternetUtility testInternetConnection])

{

if (audioButton.selected)
{
audioButton.selected = NO;
printf("\n\ndo audioButton.selected = NO; \n\n");

}

else
{
printf("\n\ndo audioButton.selected = YES; \n\n");
audioButton.selected = YES;
}

if (!isAlredayPlaying) {
printf("\n\nStarted song from 0\n");
isAlredayPlaying=YES;




NSURL *url = [NSURL URLWithString:AUDIO_FILE_URL];


playerItem = [AVPlayerItem playerItemWithURL:url];



player = [AVPlayer playerWithPlayerItem:playerItem];
player.volume=5.0;
[self.audioSliderBar setMinimumValue:0.0];
nsTimer=[NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
[player play];


}

else
{
if (isCurrentlyPlaying)

{
[nsTimer invalidate];
isCurrentlyPlaying=NO;
[player pause];


}
else
{
nsTimer=[NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
isCurrentlyPlaying=YES;
[player play];

}

}
} // internet connection check funtion, ended


else {
[ViewUtilities showAlert:INTERNET_CONNETION_TITLE :INTERNET_CONNECTION_MESSAGE];

}
}

- (void)updateTime:(NSTimer *)timer {


currentTime = CMTimeGetSeconds(playerItem.currentTime);

duration = CMTimeGetSeconds(playerItem.duration);


if (currentTime==duration) {
[self itemDidFinishPlaying];

}

[ self.audioSliderBar setValue:(currentTime/duration)];

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

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



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

if (!(player.currentItem && player.rate != 0 )&& isCurrentlyPlaying)
{
if (audioPlayerWillStop) {

isAlredayPlaying=NO;
[nsTimer invalidate];

}

else {

[player play];
printf("\n\n\n Force to play song...");

}


}
self.audioCurrentTimeLabel.text = timeInfoString;

}

- (IBAction)audioPlayerValueChanged:(UISlider *)aSlider {

 CMTime newSeekTime = CMTimeMakeWithSeconds(aSlider.value*seconds,1) ;
[player seekToTime:newSeekTime];

//此代码无法完全正常工作( slider 值不断上升)请帮忙

}

最佳答案

您可以使用我实现的方法。此代码运行良好。

        #define NSEC_PER_SEC 1000000000ull
-(void)funSeekPlayer
{
CMTime playerDuration = [self playerItemDuration];
if (CMTIME_IS_INVALID(playerDuration)) {
return;
}

float minValue = [aSlider minimumValue];
float maxValue = [aSlider maximumValue];
float value = [aSlider value];

double time = duration * (value - minValue) / (maxValue - minValue);

[player seekToTime:CMTimeMakeWithSeconds(CMTimeMakeWithSeconds(time, NSEC_PER_SEC), NSEC_PER_SEC) completionHandler:^(BOOL finished) {
dispatch_async(dispatch_get_main_queue(), ^{
// Do Your next task
});
}];

}

- (CMTime)playerItemDuration
{
AVPlayerItem *playerItem = [player currentItem];
if (playerItem.status == AVPlayerItemStatusReadyToPlay)
{
return([playerItem duration]);
}

return(kCMTimeInvalid);
}

关于iOS - 如何在 AVPlayer 中实现 seektotime(从 URL 播放歌曲)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32118743/

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