3

AVPlayer でオーディオを再生しながらスクラブを実装できるように、プレーヤーに uislider を追加したいと考えています。最初の曲が再生されるとすぐに、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 に答える
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 に答える