【发布时间】:2012-03-13 22:04:35
【问题描述】:
在我的项目中,我有一个播放歌曲的 AVAudioPlayer。在我暂停它并再次点击播放后,它会重新开始,而不是在我暂停的地方播放。为什么会这样? 此外,当我第一次按播放时,它会加载大约 3-4 秒的歌曲。我以为我通过在另一个线程上加载它来解决它,但它仍然加载得太慢(至少视图不再冻结)。我该如何解决?代码如下:
- (IBAction)play:(id)sender {
songIsCurrentlyPaused = NO;
if(songIsCurrentlyPaused==YES){
[self.background play];
} else {
playQueue = dispatch_queue_create("volume_change", NULL);
dispatch_async(playQueue, ^{ NSString *filePath =
[[NSBundle mainBundle]pathForResource:@"some_song" ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
self.background = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
self.background.delegate = self;
[self.background setNumberOfLoops:1];
[self.background setVolume:0.5];
[self.background play]; });
[trackNameLabel setText:@"Currently playing :\n some_song"];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
}
}
- (IBAction)pause:(id)sender {
songIsCurrentlyPaused = YES;
[self.background pause];
[trackNameLabel setText:@"Currently playing : some_song (paused)"];
[self.progressBar setProgress:self.background.currentTime/self.background.duration animated:YES];
}
谢谢!
【问题讨论】:
标签: ios avaudioplayer resume performance