1

Can't figure out what is wrong here or what I am missing to hear sound. It builds fine with no errors.

From what I am reading, it has something to do with ARC.... ? But when I look under the "Build Settings: Apple LLVM 7.0: ARC" ARC is listed as "NO"


.h

@property (nonatomic, strong) AVAudioPlayer *player;

.m

@synthesize player;

...

NSURL *soundFileURL = [[NSBundle mainBundle] URLForResource:@"abc" withExtension:@"m4a"];

player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //Infinite

[player play];

This works:

AudioServicesPlaySystemSound(1005);

Trying this: (ARC __bridge modifiers demystified)

SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"m4a"];
NSLog(@"Filename:  %@", soundFile);
AudioServicesCreateSystemSoundID((__bridge  CFURLRef) [NSURL fileURLWithPath:soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);

This crashes out with: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSURL initFileURLWithPath:]: nil string parameter'
...so looking in I can't see a filename... Filename: (null)

4
  • 1
    Try this way to get sound's path url: NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"m4a"]; NSURL * soundFileURL = [NSURL fileURLWithPath:soundFilePath]; Jan 2, 2016 at 20:58
  • 1
    @azimov Why? The OP's code to get the URL is fine.
    – rmaddy
    Jan 2, 2016 at 21:31
  • how and where is the player declared. you have to retain the player while you use it. e.g. via a strong property
    – Daij-Djan
    Jan 2, 2016 at 21:31
  • did the "strong" in property but same nothing. Suspend a thread perhaps? ... tried and nothing.
    – jdl
    Jan 2, 2016 at 21:37

1 Answer 1

0

Had to goto:

Project:
Targets:
Build Phases:
Copy Bundle Resources: and add the abc.m4a file to it.

The other files I needed were there except that one.

More info: NSBundle pathForResource is NULL

1
  • Some simple debugging would have shown you that soundFileURL was nil in your original code. Which also means that player was probably nil as well. Making use of the error parameter would have helped there as well.
    – rmaddy
    Jan 3, 2016 at 3:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.