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)

Community
  • 1
  • 1
jdl
  • 6,151
  • 19
  • 83
  • 132
  • 1
    Try this way to get sound's path url: NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"abc" ofType:@"m4a"]; NSURL * soundFileURL = [NSURL fileURLWithPath:soundFilePath]; – nsinvocation Jan 02 '16 at 20:58
  • 1
    @azimov Why? The OP's code to get the URL is fine. – rmaddy Jan 02 '16 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 02 '16 at 21:31
  • did the "strong" in property but same nothing. Suspend a thread perhaps? ... tried and nothing. – jdl Jan 02 '16 at 21:37

1 Answers1

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

Community
  • 1
  • 1
jdl
  • 6,151
  • 19
  • 83
  • 132
  • 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 03 '16 at 03:48