2013-07-26 52 views
1

我編程的新手我已經在使用MPMusicPlayerController的i-phone中製作了一個音樂播放器,我可以實現這一點,但是我一直在播放時拖動音頻文件。 我已經使用OBSLIDER來實現這一點。一切工作正常,但它不像蘋果默認Player.Below是我已經使用的代碼。 請幫我解決這個問題。UISlider在MPMusicPlayerController中不能正常工作iPhone

-(IBAction)sliderChanged:(id)sender 
{ 
    //player is object of MPMusicPlayerController 
    //total seconds is total length of song that is playing in player 
    if(self.slider.value<totalseconds) 
    { 
     [playbackTimer invalidate]; 
     [self.player setCurrentPlaybackTime:slider.value]; 
     NSNumberFormatter *percentFormatter = [[NSNumberFormatter alloc] init]; 
     [percentFormatter setNumberStyle:NSNumberFormatterPercentStyle];   
     self.labelScrubbing.text = [NSString stringWithFormat:@"Scrubbing speed: %@", 
            [percentFormatter stringFromNumber:[NSNumber numberWithFloat:self.slider.scrubbingSpeed]]]; 
     //to update the time when slider moves 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ 
      float totalDuration=slider.value; 
      float mins = floor(totalDuration/60); 
      float secs = round(totalDuration - mins * 60); 
      int roundedSecs = lroundf(secs); 
      int roundedMins = lroundf(mins); 

      dispatch_sync(dispatch_get_main_queue(), ^{ 
       NSString *timeInfoString = [[NSString alloc] 
              initWithFormat:@"%0d.%02d", 
              roundedMins, roundedSecs]; 
       currrentDurationSong.text=timeInfoString; 
    }); 

     }); 

     playbackTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                 target:self 
                 selector:@selector(updateTime) 
                 userInfo:nil 
                 repeats:YES]; 

    } 
    else if(self.slider.value>=totalseconds){ 

    } 

} 

我用計時器來更新剩餘時間,這裏是該

-(void)updateTime 
{ 

    NSTimeInterval currentProgress = self.player.currentPlaybackTime; 
    self.slider.value = currentProgress; 
    NSNumberFormatter *percentFormatter = [[NSNumberFormatter alloc] init]; 
    [percentFormatter setNumberStyle:NSNumberFormatterPercentStyle]; 
    self.labelScrubbing.text = [NSString stringWithFormat:@"Scrubbing speed: %@", 
           [percentFormatter stringFromNumber:[NSNumber numberWithFloat:self.slider.scrubbingSpeed]]]; 
    MPMediaItem *nowPlayingMediaItem = [[MPMusicPlayerController iPodMusicPlayer] nowPlayingItem]; 
    NSString* title = [nowPlayingMediaItem valueForProperty:MPMediaItemPropertyTitle] ; 
    NSString* artist = [nowPlayingMediaItem valueForProperty:MPMediaItemPropertyArtist] ; 
     NSString* albumname = [nowPlayingMediaItem valueForProperty:MPMediaItemPropertyAlbumTitle] ; 
    NSString *duration=[nowPlayingMediaItem valueForProperty:MPMediaItemPropertyPlaybackDuration]; 
    float totalDuration=[duration floatValue]; 
    float mins = floor(totalDuration/60); 
    float secs = round(totalDuration - mins * 60); 
    int roundedSecs = lroundf(secs); 
    int roundedMins = lroundf(mins); 
    NSLog(@"%02d",roundedMins); 


    NSString* _albumString = [[NSString alloc]initWithFormat:@"%@",(albumname.length>0 ? albumname : @"Unknown") ]; 
    NSString* _artistString = [[NSString alloc]initWithFormat:@"%@",(artist.length>0 ? artist : @"Unknown") ]; 

    NSString *timeInfoString = [[NSString alloc] 
           initWithFormat:@"%0d.%02d", 
           roundedMins, roundedSecs]; 
    totalDurationSong.text=[NSString stringWithFormat:@"%@",timeInfoString]; 
    titleOfSong.text=title; 
    ArtistOfSong.text=_artistString; 
    albumNameofSong.text=_albumString; 
    float min = floor(currentProgress/60); 
    float sec = round(currentProgress - min * 60); 
    int roundedSec = lroundf(sec); 
    int roundedMin = lroundf(min); 
    NSString *timeInfoStrings = [[NSString alloc] 
           initWithFormat:@"%0d.%02d", 
           roundedMin, roundedSec]; 
    currrentDurationSong.text=[NSString stringWithFormat:@"%@",timeInfoStrings]; 

    if(min+sec==totalseconds){ 

     [playbackTimer invalidate]; 
    } 


} 
+0

爲什麼dispatch_async? – Rajneesh071

+0

@ Rajneesh071我也刪除了,但它也不像Apple.Python中的默認播放器那樣流暢。我已經使用它來讓UILabel更新時間。 – ankyy

+0

使用playbackTimer的意思是什麼? – Rajneesh071

回答

0

我有同樣的問題掙扎的代碼。問題在於currentPlaybackTime。 currentPlaybackTime限於每秒發送當前時間數據。所以無論定時器循環多快,它只會每秒更新一次滑塊。但是,由於您的計時器設置爲1秒,因此可能會觸發當前播放時間的偏移量(導致一連串口吃)。你可能想加快這樣的時間:

playbackTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 
                 target:self 
                 selector:@selector(updateTime) 
                 userInfo:nil 
                 repeats:YES]; 
相關問題