ios - 在 viewDidLoad 下播放背景音乐时出现 SIGABRT 错误

标签 ios objective-c

这是我的代码

.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) 

最佳答案

您的崩溃跟踪表明您将 nil 作为参数传递给 -[NSURL initFileURLWithPath:] 方法。这就是它崩溃的原因。

紧接着

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

尝试将其打印为

NSLog(@"%@", path);

你得到它 nil

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

如果这有帮助,请告诉我。

关于ios - 在 viewDidLoad 下播放背景音乐时出现 SIGABRT 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27438947/

相关文章:

ios - RestKit:使用 [[RKClient sharedClient] post: usingBlock:] 时的每个请求对象映射

javascript - 我的 nativescript 应用程序的最佳结构

ios - ConstraintWithItem 中的乘数和常量

ios - 向 View 添加 subview 是保留周期吗?

iphone - 自定义 UITableViewCell 重绘问题

ios - 如何更改 UITabBarItem 角标(Badge)位置?

ios - App Store 版本号 - 更改方案/最佳实践

iphone - 如何为 Nslocal 通知设置自定义重复间隔......?

ios - 如何在swift中递归遍历NSDictionary

ios - UITabBar 内的单个 UIViewController 自动旋转