简体   繁体   English

带Avplayer的Uislider

[英]Uislider with Avplayer

Hi i created Avplayer for playing audio from server and its working perfectly but UISlider is not syncing with Audio even after updating UISlider method but the slider is moving properly but its not syncing with audio.i need to match that audio with UISlider 嗨,我创建了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)];
}

Why are you changing the maximum value at every change? 为什么每次更改都会更改最大值? You can set the minimumValue and maximumValue when you get AVPlayerStatusReadyToPlay according to audioPlayer.currentItem.duration 当您根据audioPlayer.currentItem.duration获得AVPlayerStatusReadyToPlay时,可以设置minimumValuemaximumValue

So instead of the seektime you can just use: 因此,除了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