我在viewDidLoad下播放背景音乐时遇到SIGABRT错误



这是我的代码

.h 文件

#import <AVFoundation/AVFoundation.h>
@interface ViewController : UIViewController {
AVAudioPlayer *theAudio;
}

.m file
  -(void)viewDidLoad {
  [self backgroundMusic];
  }
   -(void)backgroundMusic {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"FirstQuestionGameSoundtrack" ofType:@"m4a"];
    NSURL *url = [[NSURL alloc] initFileURLWithPath:path];
    theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [theAudio prepareToPlay];
    [theAudio setVolume:1];
    theAudio.numberOfLoops = -1;
    [theAudio play];

}

这是我的代码,我收到一个线程 1:信号 SIGABRT 错误。

如果有更好的方法在 viewController 下的应用程序上播放背景音乐,我很想知道该怎么做。谢谢!

这是错误

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); //Thread 1: signal SIGABRT error.    
}
}

控制台日志

libsystem_kernel.dylib`__pthread_kill:
0x3a364df4:  mov    r12, #0x148
0x3a364df8:  svc    #0x80
0x3a364dfc:  blo    0x3a364e14                ; __pthread_kill + 32  //Thread 1: SIGABRT
0x3a364e00:  ldr    r12, [pc, #4]             ; __pthread_kill + 24
0x3a364e04:  ldr    r12, [pc, r12]
0x3a364e08:  b      0x3a364e10                ; __pthread_kill + 28
0x3a364e0c:  rsbeq  r5, lr, #0x80000001
0x3a364e10:  bx     r12
0x3a364e14:  bx     lr

*** ImageIO - could not find ColorSync function 'ColorSyncProfileCreateSanitizedCopy'
2014-12-11 23:59:39.500 The Wild Game[3413:563583] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'
*** First throw call stack:
(0x2c275c1f 0x39d1ec8b 0x2c275b65 0x2cf0ae6d 0xfd9eb 0xfd7af 0x2f734f8f 0x2f734cfd 0x2f73abcb 0x2f73863d 0x2f7a283d 0x2f99468b 0x2f996afb 0x2f9a1379 0x2f995387 0x329d90e9 0x2c23c39d 0x2c23b661 0x2c239de3 0x2c188211 0x2c188023 0x2f7993ef 0x2f7941d1 0xfe61d 0x3a29eaaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Crash 的堆栈跟踪显示您正在-[NSURL initFileURLWithPath:]方法中将nil作为参数传递。这就是它崩溃的原因。

紧接着

NSString *path = [[NSBundle mainBundle] pathForResource:@"FirstQuestionGameSoundtrack" ofType:@"m4a"];`

尝试将其打印为

NSLog(@"%@", path);

你正在得到它nil

确保将该文件添加到项目中,并且该文件显示在 Xcode 中可执行文件目标的Copy Bundle Resources构建阶段。

让我知道这是否有帮助。

相关内容

最新更新