Avispyer与UISlider有关

时间:2015-11-02 10:54:55

标签: ios objective-c avplayer uislider

您好我刚创建了 AVplayer ,用于播放来自服务器的音频。我的问题是UISlider基于 Audioplayer 没有移动。请帮我解决这个问题。

    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;
    seekbar.minimumValue=0;
    seekbar.maximumValue=20;
    [self.view addSubview:seekbar];

    -(void)Audio{
        NSString *urlString= @"https.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];


    }

    - (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:(id)sender {

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

1 个答案:

答案 0 :(得分:2)

<md-progress-linear>仅在用户移动滑块时调用。所以这不是更新它的好地方。

你应该有一个seekTime方法,从NSTimer调用,你需要更新滑块位置:

updateProgress

因此,每当NSTimer触发时,它都应该更新滑块的位置。

- (void)updateProgress { [seekbar setValue:CMTimeGetSeconds(audioPlayer.currentTime)]; } 方法中,您应该执行相反的操作:将音频播放设置为用户选择的点。像这样:

seekTime