3

我想将 uislider 添加到我的播放器中,以便我可以在 AVPlayer 中播放音频时实现擦洗。一旦第一首歌曲播放完,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];

}
4

4 回答 4

18
- (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];
}];
于 2012-07-31T08:38:03.863 回答
5

我制作了自己的播放器来播放视频。这是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];

现在添加一个观察者和函数。

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

功能 :


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

希望它可以帮助你。

于 2013-12-16T06:51:49.720 回答
3

我尝试了顶级解决方案,对于更长的视频,这还可以,但是当我有大约 4 秒的视频时,UISlider 非常不稳定。如果您想要精确,请尝试以下解决方案:

- (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];
    }
}
于 2014-01-05T15:06:56.343 回答
1

这个我的代码是在线播放带有音量滑块的音频歌曲。您可以轻松增加和减少音量。使用 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

*

于 2012-07-31T05:05:30.830 回答