Here is my code
.h file
#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];
}
That is my code and I am getting a Thread 1: signal SIGABRT error.
If there is a better way of playing background music on an app under a viewController, I would love to know how to do that. Thank you!
This is the error
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); //Thread 1: signal SIGABRT error.
}
}
console log
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)
Your stack trace for Crash says you are passing nil
as a parameter in -[NSURL initFileURLWithPath:]
method. That's why it's crashing.
Right after
NSString *path = [[NSBundle mainBundle] pathForResource:@"FirstQuestionGameSoundtrack" ofType:@"m4a"];`
Try printing it as
NSLog(@"%@", path);
You are getting it nil
Make sure that you add that file to your project, and it appears in a Copy Bundle Resources
build phase in your executable's target in Xcode.
Let me know if this helps.
User contributions licensed under CC BY-SA 3.0