gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:35:26 26 4
gpt4 key购买 nike

这是我的代码

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com