使用带有 AVAudioPlayer 的 playAtTime: 方法的 UISlider,AVAudioPlayer 没有内置的搜索栏。
查看此示例代码,它在类 avTouchController 中实现了您想要的
https://developer.apple.com/library/ios/samplecode/avTouch/Introduction/Intro.html
将 UISLider 添加到您的界面并将 valueChanged: 链接到方法 seekTime:
在.h
@property (nonatomic, weak) IBOutlet UISlider *seekbar;
@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (nonatomic, strong) NSTimer *updateTimer;
在加载 AVAudioPlayer 后 viewDidLoad 中的 .m 中,
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"There's A Long, Long Trail A-Winding" ofType:@"mp3"]];
AVAudioPlayer *audio = [[AVAudioPlayer alloc]
initWithContentsOfURL:fileURL error:nil];
self.audioPlayer = audio;
self.seekbar.minimumValue = 0;
self.seekbar.maximumValue = self.audioPlayer.duration;
[[self audioPlayer] play];
self.updateTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(updateSeekBar) userInfo:nil repeats:YES]
并添加以下方法
- (void)updateSeekBar{
float progress = self.audioPlayer.currentTime;
[self.seekbar setValue:progress];
}
- (IBAction)seekTime:(id)sender {
self.audioPlayer.currentTime = self.seekbar.value;
}