如何添加UISlider到AVPlayer [英] How to add UISlider to AVPlayer

查看:245
本文介绍了如何添加UISlider到AVPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加uislider到我的播放器,以便我可以实现擦洗,而播放音频在AVPlayer。一旦第一首歌曲被播放,uislider将回到原始位置,下一首歌曲将开始播放。如果有人帮助我,我会感激它。这是我的代码。

I want to add uislider to my player so that i can implement scrubbing while playing audio in AVPlayer . as soon as the first song gets played out the uislider will go back to original position and the next song will start playing . If anyone help me i would appreciate it .This is my code.

-(IBAction)goToiPodLibrary:(UIButton *)sender
{

    // Create picker view
    MPMediaPickerController* picker = [[MPMediaPickerController alloc] init];
    picker.delegate = self;

    if (userMediaItemCollection) {

        MusicTableViewController *controller = [[MusicTableViewController alloc] initWithNibName: @"MusicTableView" bundle: nil];
        controller.delegate = self;

        controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;

        [self presentModalViewController: controller animated: YES];

        // else, if no music is chosen yet, display the media item picker
    } else {

        MPMediaPickerController *picker =
        [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeMusic];

        picker.delegate                     = self;
        picker.allowsPickingMultipleItems   = YES;
        picker.prompt                       = NSLocalizedString (@"Add songs to play", "Prompt in media item picker");

        // The media item picker uses the default UI style, so it needs a default-style
        //      status bar to match it visually
        [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleDefault animated: YES];

        [self presentModalViewController: picker animated: YES];
    }

}



-(IBAction)playButtonPressed:(UIButton *)sender
{
    [myPlayer play];
}


-(IBAction)pauseButtonPressed:(UIButton *)sender
{
    [myPlayer pause];
}


-(void) mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {

    // Dismiss selection view
    [self dismissViewControllerAnimated:YES completion:nil];

}

-(void) mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {

    // Dismiss selection view
    [self dismissViewControllerAnimated:YES completion:nil];

    // Get AVAsset
    NSURL* assetUrl = [mediaItemCollection.representativeItem valueForProperty:MPMediaItemPropertyAssetURL];
    AVURLAsset* asset = [AVURLAsset URLAssetWithURL:assetUrl options:nil];

    // Create player item
    AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:asset];



    // Play it
    myPlayer = [AVPlayer playerWithPlayerItem:playerItem];

    [playlistSongsArray addObjectsFromArray:mediaItemCollection.items];

    NSLog(@" Playlist songs  %@",playlistSongsArray);

    [self.myPlaylistTable reloadData];

    [myPlayer play];

}


推荐答案

- (void)sliderValueChanged:(id)sender {
    CMTime newTime = CMTimeMakeWithSeconds(timeSlider.value * durationSeconds, player.currentTime.timescale);
    [self.player seekToTime:newTime];
}

使用 UISlider (这里叫做timeSlider)和上面的方法

Use a UISlider (here called timeSlider) and the method above

这是如何在播放时更新滑块:

This is how to update slider when playing:

self.playbackObserver = [player addPeriodicTimeObserverForInterval:interval queue:NULL usingBlock:^(CMTime time) {
    CMTime endTime = CMTimeConvertScale (player.currentItem.asset.duration, player.currentTime.timescale, kCMTimeRoundingMethod_RoundHalfAwayFromZero);
    if (CMTimeCompare(endTime, kCMTimeZero) != 0) {
        double normalizedTime = (double) player.currentTime.value / (double) endTime.value;
        self.timeSlider.value = normalizedTime;
    }
    Float64 currentSeconds = CMTimeGetSeconds(player.currentTime);
    int mins = currentSeconds/60.0;
    int secs = fmodf(currentSeconds, 60.0);
    NSString *minsString = mins < 10 ? [NSString stringWithFormat:@"0%d", mins] :      [NSString stringWithFormat:@"%d", mins];
    NSString *secsString = secs < 10 ? [NSString stringWithFormat:@"0%d", secs] : [NSString stringWithFormat:@"%d", secs];
    currentTimeLabel.text = [NSString stringWithFormat:@"%@:%@", minsString, secsString];
}];

这篇关于如何添加UISlider到AVPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆