我是 XCode 和 Objective-C 的新手。当我让应用程序在模拟器中运行时,我弹出了这个异常。在 iPad 上它工作正常。当分配 AVaudioPlayer 时,我会停止并发出警告 SIGSTOP。
有人有什么建议吗?这是一个严重的例外吗?提前致谢!
我不知道在哪里可以找到“堆栈跟踪”,所以我将 RUN 调试器放到 gdb:来自此的信息:Pending breakpoint 1 - ""soundScape.m":112" resolved
rndmitem
amount
url
SIGSTOP 之前的行是:
libc++abi.dylib`__cxa_throw:
0x46e4a44: pushl %ebp
0x46e4a45: movl %esp, %ebp
当前语言:自动;目前objective-c
0x1 似乎没有指向一个有效的对象。
0x2 似乎没有指向有效对象。
file://localhost/Users/myName/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/E92E5F03-0B07-4AF0-BFB4-E1F7009A4E78/babyschets.app/02Seawash.mp3
[切换到进程81163线程0x1720b]
[ Switching to process 81163 thread 0x15803]
under Exception State Registers:
trapno = (unsigned int) 0x000003
err = (unsigned int) 0x000000
faultvaddr = (unsigned int) 0x087df732
例如,当值如下时会发生这种情况:
randomItem :
(NSUInteger) $0 = 1 [没有可用的 Objective-C 描述]
amountOfAudioFiles :
(int) $1 = 2 [没有可用的 Objective-C 描述]
url :
(NSURL *) $2 = 0x07485860
file://localhost/Users/myName /Library/Application%20Support/iPhone%20Simulator/5.1/Applications/E92E5F03-0B07-4AF0-BFB4-E1F7009A4E78/babyschets.app/02Seawash.mp3
audioFiles1 :
(NSArray *) $4 = 0x0747d720 <__NSArrayI 0x747d720>(
01Seawash.mp3,
02Seawash.mp3
)
int amountOfAudioFiles = [audioFiles1 count];
NSUInteger randomItem = arc4random()%(int)amountOfAudioFiles;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:[NSString stringWithString:[audioFiles1 objectAtIndex:randomItem]]
ofType:nil]];
NSError *error;
audioPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
好的,我已经清理了这段代码,这是我的解决方案。现在它像魅力一样运行:
在 .h 文件中:@property (nonatomic, retain) AVAudioPlayer *audioPlayer1;
在 .m 文件中:
int amountOfAudioFiles = [audioFiles1 count];
int randomItem = (int)(arc4random()%amountOfAudioFiles);
if (randomItem < 0) randomItem = 0;
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:[NSString stringWithFormat:@"%@", [audioFiles1 objectAtIndex:randomItem]]
ofType:nil]];
if (audioPlayer1) {
if (audioPlayer1.isPlaying) {
[audioPlayer1 stop];
audioPlayer1 = nil;
}
} else {
audioPlayer1 = [AVAudioPlayer alloc];
}
NSError *error;
audioPlayer1 = [audioPlayer1 initWithContentsOfURL:url error:NULL];