Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 06-15-2010, 08:33 PM   #1 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 3
smerd11 is on a distinguished road
Default Recorder/Player issue: simulator is fine, device does not work

Hi all,
This is issue has been frustrating me for about 2 hours now. I can't figure it out, likely because I'm a newbie. I'm trying to just simply record and playback a sound file. In the simulator, the following works just fine:

Code:
recordedFile = [NSURL fileURLWithPath:[NSString stringWithFormat: @"%@/recordedSound.caf",[[NSBundle mainBundle] resourcePath]]];
...
recorder = [[ AVAudioRecorder alloc] initWithURL:recordedSound settings:recordSetting error:&error];
I'm able to record and playback sounds with the appropriate buttons in the application when in simulator. But when I try it on the device, it doesn't seem to actually record anything, and it does not play back. I've figured out a way to make it work on the device, by replacing the above first line with this:

Code:
recordedSound = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"recordedSound.%@", @"caf"]]];
This tells me that it's not an issue with the recorder, but just with where I'm creating the file. But I don't want the file in a temporary directory. I want it to be located at "Application.app/recordedSound.caf." What am I missing here? I can feel that there's an easy fix, but I can't figure out what that is.
Thanks so much for the help!!
smerd11 is offline   Reply With Quote
Old 06-15-2010, 09:33 PM   #2 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 107
JDave is on a distinguished road
Default

The iPhone is cAsEsensitive to file names. The simulator is not. I would verify filenames and paths with respect to cAsE.
JDave is offline   Reply With Quote
Old 06-15-2010, 11:55 PM   #3 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 3
smerd11 is on a distinguished road
Default

Quote:
Originally Posted by JDave View Post
The iPhone is cAsEsensitive to file names. The simulator is not. I would verify filenames and paths with respect to cAsE.
Hmm, thanks for this--that was news to me, but I looked though and can't identify any typos with respect to upper/lowercase.

I was advised that I need to have an AVAudioSession, which I have added, but the problem remains the same: it plays in simulator but not on the device. Here's what I have currently:

Code:
//Audio Session
AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
[audioSession setActive:YES error: &error];

//Recording settings
NSMutableDictionary * recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

//Create NSURL for recorder
recordedFile = [NSURL fileURLWithPath:[NSString stringWithFormat: @"%@/recordedSound.caf",[[NSBundle mainBundle] resourcePath]]];

//Set up the recorder to use the NSURL and record to file

recorder = [[ AVAudioRecorder alloc] initWithURL:recordedFile settings:recordSetting error:&error];
[recorder setDelegate:self]; //I added this in recently, though I don't understand why it's necessary
[recorder prepareToRecord];
[recorder record];
And the play button is pretty simply (leaving no room for lower/uppercase mistakes):

Code:
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedFile error:&error];
[avPlayer prepareToPlay];
[avPlayer play];
smerd11 is offline   Reply With Quote
Old 06-16-2010, 10:01 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2010
Posts: 3
smerd11 is on a distinguished road
Default

I've messed with this for a few more hours, and still no luck. The bizarre thing is that it works perfectly fine if I create temp directory. In other words, this works flawlessly both on the device and in simulator:

Code:
recordedFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"recordedSound.%@", @"caf"]]];
But this ONLY works in the simulator:

Code:
recordedFile = [NSURL fileURLWithPath:[NSString stringWithFormat: @"%@recordedSound.caf",[[NSBundle mainBundle] resourcePath]]];
Why is this the case?
smerd11 is offline   Reply With Quote
Old 06-09-2011, 10:24 AM   #5 (permalink)
Registered Member
 
Join Date: Jun 2011
Posts: 4
Frollo is on a distinguished road
Default

I assume you figured out that you can't create files in the NSBundle, only in your "Documents" directory.
Frollo is offline   Reply With Quote
Reply

Bookmarks

Tags
avaudioplayer, avaudiorecorder, nsurl

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 323
9 members and 314 guests
Absentia, Domele, fiftysixty, givensur, heshiming, linkmx, michaelhansen, PixelInteractive, Sloshmonster
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:15 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0