【问题标题】:UISlider not working smoothly in MPMusicPlayerController in iphoneUISlider 在 iPhone 的 MPMusicPlayerController 中无法顺利运行
【发布时间】:2013-07-26 05:47:30
【问题描述】:

我是编程新手,我已经使用 MPMusicPlayerController 在 i-phone 中制作了一个音乐播放器,我能够实现这一点,但我在播放时卡在擦洗音频文件。 我已经使用 OBSLIDER 来实现这一点。一切正常,但不如 Apple Default Player 流畅。下面是我使用的代码。 请帮帮我。

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


}

【问题讨论】:

  • @Rajneesh071 我也删除了它,但它也不像 Apple 中的默认播放器那样流畅。我用它只是为了让 UILabel 更新时间。
  • 使用playbackTimer是什么意思?
  • 播放定时器是更新歌曲时长的剩余时间。

标签: iphone ios media-player xcode4.5


【解决方案1】:

我也遇到过同样的问题。问题在于currentPlaybackTime。 currentPlaybackTime 仅限于每秒发送当前时间数据。因此,无论计时器循环多快,它都只会每秒更新一次滑块。但是,由于您的计时器设置为 1 秒,它可能会触发 currentPlaybackTime 的偏移量(导致有点卡顿)。您可能希望像这样加快时间:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-15
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多