簡體   English   中英

帶Avplayer的Uislider

[英]Uislider with Avplayer

嗨,我創建了Avplayer來播放服務器中的音頻,並且其工作正常,但是即使更新了UISlider方法后,UISlider也無法與音頻同步,但是滑塊可以正常移動,但無法與音頻同步。我需要將該音頻與UISlider進行匹配

seekbar =[[UISlider alloc]init];
    seekbar.frame=CGRectMake(10,CGRectGetMinY(PlayAudio.frame)-50, CGRectGetWidth(self.view.frame)-20, 20);
   [seekbar addTarget:self action:@selector(seektime:) forControlEvents:UIControlEventValueChanged];
    seekbar.continuous=YES;
    [self.view addSubview:seekbar];
-(void)Audio{
    NSString *urlString= @"https:/embed-ssl.wistia.com/deliveries/85dcf8de9db3830f47f136a4dba89114be58403f/dhwpxq4l9l.wav";

    audioPlayer = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:urlString]];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playerItemDidReachEnd:)
                                                 name:AVPlayerItemDidPlayToEndTimeNotification
                                               object:[audioPlayer currentItem]];
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateProgress) userInfo:nil repeats:YES];
    [audioPlayer play];
    NSLog(@"Audio playing");
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

    if (object == audioPlayer && [keyPath isEqualToString:@"status"]) {
        if (audioPlayer.status == AVPlayerStatusFailed) {
            NSLog(@"AVPlayer Failed");

        } else if (audioPlayer.status == AVPlayerStatusReadyToPlay) {
            NSLog(@"AVPlayerStatusReadyToPlay");
            [audioPlayer play];


        } else if (audioPlayer.status == AVPlayerItemStatusUnknown) {
            NSLog(@"AVPlayer Unknown");

        }
    }
}


- (void)seektime:(UISlider*)sender {
   CGFloat currentSongTime = CMTimeGetSeconds([audioPlayer currentTime]);
    seekbar.value = currentSongTime;

    seekbar.minimumValue=0;
    seekbar.maximumValue=currentSongTime;
}

- (void)updateProgress {

    [seekbar setValue:CMTimeGetSeconds(audioPlayer.currentTime)];
}

為什么每次更改都會更改最大值? 當您根據audioPlayer.currentItem.duration獲得AVPlayerStatusReadyToPlay時,可以設置minimumValuemaximumValue

因此,除了seektime您可以使用:

    else if (audioPlayer.status == AVPlayerStatusReadyToPlay) {
                NSLog(@"AVPlayerStatusReadyToPlay");
                [audioPlayer play];
                seekbar.minimumValue = 0;
                seekbar.maximumValue = CMTimeGetSeconds(audioPlayer.currentItem.duration);

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM