在我的项目中,我有一个播放歌曲的 AVAudioPlayer。在我暂停并再次点击播放后,它会重新开始,而不是在我暂停的地方播放。为什么会这样? 此外,当我第一次按下播放键时,歌曲加载时间约为 3-4 秒。我以为我通过在另一个线程上加载它来解决它,但它仍然加载得太慢(至少 View 不再卡住)。我该如何解决?这是代码:
- (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];
}
谢谢!
最佳答案
您在 play:
songIsCurrentlyPaused
设置为 NO
尝试将其注释掉:
- (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];
}
如果你想摆脱最初的停顿,你将不得不完全重新组织你的播放器设置。在用户点击播放按钮并调用之前初始化它:
[self.background prepareToPlay];
这将预加载歌曲。同时将 songIsCurrentlyPaused = NO;
移动到代码中较早的位置。
编辑:
要消除初始延迟,您应该将初始化代码移至 loadView
或 viweDidLoad
等地方。
//initialization code
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 prepareToPlay];
现在这可能会导致 UI 显示滞后,因此您可能不想考虑预加载数据 在后台线程中。
然后应修改您的 IBAction
方法:
- (IBAction)play:(id)sender
{
if (songIsCurrentlyPaused==YES)
{
[self.background play];
}
else
{
playQueue = dispatch_queue_create("volume_change", NULL);
dispatch_async(playQueue, ^{
[self.background setCurrentTime: 0.0];
[self.background play];
});
[self.progressBar setProgress:0.0 animated:YES];
[trackNameLabel setText:@"Currently playing :\n some_song"];
self.timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(updateProgressBar) userInfo:nil repeats:YES];
}
songIsCurrentlyPaused = NO;
}
- (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 暂停问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9692929/