How to add UISlider to AVPlayer

17,035

Solution 1

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

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];
}];

Solution 2

I have made my own player for playing video. Here is code for UISlider i have made. Initialize the UISlider

self.progressBar = [[[UISlider alloc] init] autorelease];
self.progressBar.frame = CGRectMake(44*frameWidth/240, 6*frameHeight/160, 187*frameWidth/240,33*frameHeight/160);
[self.progressBar addTarget:self action:@selector(progressBarChanged:) forControlEvents:UIControlEventValueChanged];
[self.progressBar addTarget:self action:@selector(proressBarChangeEnded:) forControlEvents:UIControlEventTouchUpInside];
[self.progressBar setThumbImage:[UIImage imageNamed:@"Slider_button"] forState:UIControlStateNormal];
[self.playerManageViewBottom addSubview:self.progressBar];

Now add an observer and the functions.

CMTime interval = CMTimeMake(33, 1000);
playbackObserver = [self.moviePlayer addPeriodicTimeObserverForInterval:interval queue:dispatch_get_current_queue() usingBlock: ^(CMTime time) {
    CMTime endTime = CMTimeConvertScale (self.moviePlayer.currentItem.asset.duration, self.moviePlayer.currentTime.timescale, kCMTimeRoundingMethod_RoundHalfAwayFromZero);
    if (CMTimeCompare(endTime, kCMTimeZero) != 0) {
        double normalizedTime = (double) self.moviePlayer.currentTime.value / (double) endTime.value;
        self.progressBar.value = normalizedTime;
    }
    self.playBackTime.text = [self getStringFromCMTime:self.moviePlayer.currentTime];
}];

Functions :


-(void)progressBarChanged:(UISlider*)sender
{
    if (self.isPlaying) {
        [self.moviePlayer pause];
    }
    CMTime seekTime = CMTimeMakeWithSeconds(sender.value * (double)self.moviePlayer.currentItem.asset.duration.value/(double)self.moviePlayer.currentItem.asset.duration.timescale, self.moviePlayer.currentTime.timescale);
    [self.moviePlayer seekToTime:seekTime];
}

-(void)proressBarChangeEnded:(UISlider*)sender { if (self.isPlaying) { [self.moviePlayer play]; } }

Hope it can help you.

Solution 3

I tried the top solution, and for longer videos this was okay, but when I had a ~4 second video, the UISlider was very choppy. If you want precision, try this solution:

- (IBAction)sliderSlid:(id)sender {
    if ([sender isKindOfClass:[UISlider class]]) {
        UISlider *slider = sender;

        CMTime playerDuration = player.currentItem.duration;
        if (CMTIME_IS_INVALID(playerDuration)) {
            return;
        }

        double duration = CMTimeGetSeconds(playerDuration);
        if (isfinite(duration)) {
            float minValue = [slider minimumValue];
            float maxValue = [slider maximumValue];
            float value = [slider value];

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

            [player seekToTime:CMTimeMakeWithSeconds(time, NSEC_PER_SEC) toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
        }
        [player pause];
    }
}

Solution 4

This My code is Online Play the Audio Song with Volume slider. You can easily increase and decrease the volume.Using AvAudioPlayer...

*

AudioPlayerController.h 
@interface AudioPlayerController : UIViewController{

    AVAudioPlayer *audioPlayer;
    NSMutableData *data;
    NSURLConnection *connection;
    UIView *myView;
    IBOutlet UISlider *volumeSlider1;
    IBOutlet UIView *volumeSlider;



}
@property (retain, nonatomic) IBOutlet UIActivityIndicatorView *activity;
@property (retain ,nonatomic)  AVAudioPlayer *audioPlayer;
- (IBAction)pause:(id)sender;
- (IBAction)play:(id)sender;
- (IBAction)stop:(id)sender;
@end

AudioPlayerController.m
- (void)viewDidLoad
{
//JD...
    [super viewDidLoad];

    volumeSlider1.minimumValue = 0;
    volumeSlider1.maximumValue = 10;
    volumeSlider1.value = [[[NSUserDefaults standardUserDefaults] valueForKey:@"volume"] intValue]; 

    self.navigationController.navigationBarHidden = NO;
    [self getAudioFromNet];
  }
- (void)getAudioFromNet {

    NSURL *url = [NSURL URLWithString:@"http://sound17.mp3pk.com/indian/cocktail/[Songs.PK]%20Cocktail%20-%2001%20-%20Tum%20Hi%20Ho%20Bandhu.mp3"]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

}
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
    if (data==nil) {
        data = [[NSMutableData alloc] initWithCapacity:2048];
    }
    [data appendData:incrementalData];
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

    audioPlayer = [[AVAudioPlayer alloc] initWithData:data error: nil];
    [audioPlayer setVolume:3];
    [audioPlayer play];

}
- (IBAction)play:(id)sender{


    [audioPlayer play];

}
- (IBAction)pause:(id)sender{


    [audioPlayer pause];

}
- (IBAction)stop:(id)sender{



    [audioPlayer stop];

}
//This Method is Used for Volume...
- (IBAction)sliderMoved:(UISlider *)aSlider
{
    audioPlayer.volume = volumeSlider1.value;

    int valumeInt = volumeSlider1.value;
    [[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%d",valumeInt] forKey:@"volume"];
    audioPlayer.volume = [[[NSUserDefaults standardUserDefaults] valueForKey:@"volume"] intValue]; 

}
@end

*

Share:
17,035
coded
Author by

coded

Updated on June 05, 2022

Comments

  • coded
    coded almost 2 years

    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];
    
    }