私はプログラミングが初めてで、MPMusicPlayerControllerを使用してiPhoneで音楽プレーヤーを作成しましたが、それを達成することはできますが、再生中にオーディオファイルをスクラブすることに固執しています。これを実現するために 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];
}
}