以下代码给出了AVAudioPlayer分配行标题的异常: 线程1断点1.2 我是否在实例化AVaudioPlayer时犯了错误? 或者问题是由于玩家在不同的线程上播放的事实?
此处代码:
#import "songPlayer.h"
@implementation songPlayer
@synthesize audioPlayer;
@synthesize isPlaying;
@synthesize lastSongNumber;
@synthesize currentTimeSong;
@synthesize volume;
@synthesize audioFilesArray;
-(void)loadSoundFiles:(NSArray*)soundFiles{
audioFilesArray = [[NSArray alloc]initWithArray:soundFiles];
}
-(void)loadSounds:(int)tagNumber{
NSString *audiofile = [NSString stringWithFormat:@"%@", [audioFilesArray objectAtIndex:(tagNumber-201)]];
if ([audiofile hasSuffix:@".mp3"]) {
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:audiofile
ofType:nil]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if (error)
{
NSLog(@"Error in audioPlayer: %@",
[error localizedDescription]);
} else {
audioPlayer.delegate = self;
if (!volume) {
volume = 1.0;
}
[audioPlayer setVolume:volume];
[audioPlayer prepareToPlay];
if (!currentTimeSong) {
currentTimeSong = 0;
}
audioPlayer.currentTime = currentTimeSong;
}
}
}
-(void)playOrStop:(int)tagNumber{
if (audioPlayer.isPlaying > 0) {
currentTimeSong = audioPlayer.currentTime;
[audioPlayer stop];
audioPlayer = nil;
if (tagNumber-200 != isPlaying) {
[self playOrStop:tagNumber];
} else {
isPlaying = 0;
return;
}
} else {
isPlaying = tagNumber - 200;
[self loadSounds:tagNumber];
audioPlayer.currentTime = currentTimeSong;
// Create an asynchronous background queue
NSOperationQueue *queueSong = [[NSOperationQueue alloc] init];
[queueSong addOperationWithBlock:
^{
[audioPlayer play];
}];
}
}
-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
NSLog(@"restart...");
lastSongNumber = isPlaying + 200;
audioPlayer.currentTime = 0;
currentTimeSong = 0;
isPlaying = 0;
[self playOrStop:lastSongNumber];
}
@end