tags:

views:

262

answers:

4

I am writing an application and part of it is recording and playing the sound using AVAudioRecorder and AVAudioPlayer class. I set up the sound file as below.

NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/sound.caf"];    
self.soundFileURL = [NSURL fileURLWithPath:resourcePath];

When i run the app in simulator, it works fine.However when i load the app into the device it does not work.Does anyone have any idea what the problem could be.


Hi coob. i tried with your answer .But no sound in device .It works in simulator. When i tested in distribution device i got a crash report "unknown kernal[0]:ERROR:AMC reset[non-fatal error]:could not lock BSU "

A: 

I don't think you can write to the resourcePath on the device, it's protected. Find the App's documents directory and write somewhere there:

NSArray *filePaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *recordingDirectory = [filePaths objectAtIndex: 0];
NSString *resourcePath = [recordingDirectory stringByAppendingString:@"/sound.caf"];
self.soundFileURL = [NSURL fileURLWithPath:resourcePath];
coob
A: 

Is it possible that you have done the same thing that I did the first time I tried to get sound to work on an iPhone? Is the sound muted?

Another possibility:

I have discovered that filenames within the bundle are case-sensitive on the device but not in the simulator. (Or more case-sensitive -- I haven't experimented with exactly what works on each.) So if the case is wrong on a file, it may simply be sliding through on the simulator, but returning nil instead of the file when it runs on the device.

I know, it sounds odd, but I just confirmed it in my own project. Try taking a file that you know loads properly, change the case of the filename, and see how it works on the device. -- clarification -- change the case of the name by which you retrieve the file so it no longer matches the filename within the bundle.

Doesn't mean that's your problem, but something to watch out for nonetheless.

Amagrammer
Tried with above solution. Then also the same thing happend.Works in simulator and no sound in device. Any other alternatives?
diana
So you are saying that you checked the case (capitalization) of the filenames, to make sure that what you ask for matches exactly what is in the bundle? (I assume the answer is yes, but I just want to make sure.)
Amagrammer
A: 

I just reviewed some of my code. Matching your intentions, I come up with:

NSString *soundName = @"sound"; // without extension -- mimicing your naming
NSString *resourcePath = [[NSBundle mainBundle] pathForResource: soundName ofType: @"caf"]]
self.soundFileURL = [NSURL fileURLWithPath:resourcePath];

Does that work?

Amagrammer
A: 

Make sure your device is not in Silent Mode. Short sounds don't work in silent mode.

cocoaholic