如何添加UISlider到AVPlayer
我想添加uislider到我的播放器,这样我就可以在播放AVPlayer中的audio时进行清理。 只要第一首歌曲播放出来,uislider就会回到原来的位置,下一首歌曲就会开始播放。 如果有人帮助我,我将不胜感激。这是我的代码。
-(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)和上面的方法
这是如何更新滑块时播放:
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]; }];
我尝试了最好的解决scheme,对于更长的video来说,这是可以的,但是当我有一个4秒钟的video,UISlider非常波涛汹涌。 如果你想要精度,试试这个解决scheme:
- (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]; } }
这是我的代码在线使用音量滑块播放audio歌曲。 您可以轻松地增加和减less音量。使用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
*
我制作了自己的播放器来播放video。 这里是我所做的UISlider
代码。 初始化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];
现在添加一个观察者和function。
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]; }];
function :
-(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)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]; }
– (无效)proressBarChangeEnded:(UISlider *)发件人
{
if(self.isPlaying){
[self.moviePlayer play];
}
}
希望它可以帮助你。