Unable to play audio on device (iOS) - iphone

I am using the following code to play a audio in iOS using **AVAudioPlayer** class.
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"myAudio" ofType:#"mp3"];
NSURL *audioURL = [[NSURL alloc] initFileURLWithPath:audioPath];
NSError *error;
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer play];
This is working fine in simulator. But, i am not getting the sound output when i run it on the device. The delegate methods ofAVAudioPlayerDelegate are also being called but there is no volume output. Where am i going wrong here.... Another important aspect i'v noticed here is that, i am getting a large message in my GDB while playing the audio file on simulator. But the same thing is not happening on device... Please refer the screenshot below for GDB messages while running on the simulator.

Check if your device is on loudspeaker mode or not..
to listen the sound through Loundspeaker use this -
// set audio to loudSpeaker mode for iphone
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
or you can increase the volume of your audio file using this -
Player.volume = 10.0;

Have you tried the .caf or .wav file? might possible that .mp3 is not working on iPhone. Also check the duration of your file not greater than 30 sec.

I was so so stupid.... My volume button was turned off. That was the reason.
Really sorry for wasting your time guys... My sincere apologies.

Related

Playing continuos sound in iPhone app

I have an iPhone application which requires me to play a looping mp3 sound during the entire lifetime of app.
But based on some actions performed by user this mp3 should stop/pause for while & another mp3 should play & then this mp3 should resume playing again.
I havent really used any audio API on iPhone yet, I've just used AudioToolbox for playing sounds on button taps thats all.
So what do you guys recommend I should do.
Any pointers...? suggestions....?
just as I wrote in this post, you can use AVAudioPlayer
code extract:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"mySound" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL URLForString:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:soundFileURL];
player.numberOfLoops = -1; //infinite
[player play];
and you can pause it with:
[player pause];

iPhone OS 4 Multitasking - Playing Audio In background

I am trying to use iPhone OS 4.0's multitasking capability. I tried to play audio in the background with no luck. I added UIBackgroundModes property in info.plist and mentioned requires audio to play in background. Also I added the code for playing the audio.
`
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"someday" ofType:#"mp3"]];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer play];
`. The audio starts playing once i click on button in the app. But when I shut the app it stops. How can I make it play in the background?
Thanks,
Tony
It sounds like you didn't set up your Audio Session correctly. From http://developer.apple.com/iphone/library/documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html :
For example, when using the default audio session, audio in your application stops when the Auto-Lock period times out and the screen locks. If you want to ensure that playback continues with the screen locked, include the following lines in your application’s initialization code:
NSError *setCategoryErr = nil;
NSError *activationErr = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error:&setCategoryErr];
[[AVAudioSession sharedInstance] setActive:YES error:&activationErr];
The AVAudioSessionCategoryPlayback category ensures that playback continues when the screen locks.
Activating the audio session puts the specified category into effect.
HI,
I think this video helps u in solving ur problem...
IN WWDC videos they have clearly explained how u can enable the back ground audio...
http://developer.apple.com/videos/wwdc/2010/
to view or download these videos u need to have a apple account...
and in that see Session 109-Adopting multitasking on iPhone OS, Part2...
Hope this will help u..
~Raviraja

AVAudioPlayer only plays in simulator but not device why?! (iPhone-SDK)

I have the following simple code that plays a sound
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"wav"];
player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: soundPath] error:nil];
player.volume = 1;
player.numberOfLoops = 0;
[player prepareToPlay];
[player play];
For some reason this code only plays in the simulator but not the device, any ideas?
Update: Actually I installed AvTouch, a sample app from Apple onto 3.0 and that doens't work either. Is something wrong with AVAudioPlayer?
This can happen when the codec used by the audio file is available on OS X, but not the iPhone. It is also caused when the extension doesn't match the actual content of the file. Step through with the debugger and watch the debug console for codec errors or reconvert your file to a supported format with afconvert.

AVAudioPlayer not responding

I initialize an AVAudioPlayer instance:
NSString* path = [[NSBundle mainBundle] pathForResource:#"foo" ofType:#"wav"];
AVAudioPlayer* player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil]; //Returned object not nil, indicating that the file has loaded successfully
BOOL b = [player prepareToPlay]; //returns TRUE
BOOL b2 = [player play];
//returns TRUE, goes immediately to the next line since asynchronous
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]; //wait 1 sec
printf("%10.4f\n",player.duration); //duration is 2s
printf("%10.4f\n",player.currentTime); //position remains 0
/* There is no sound output, player is also stuck since position==0 */
Does anyone know why the AVAudioPlayer is not responding? Is there something that I am overlooking while initializing, playing the audioplayer? Boilerplate code maybe?
I am running this on the iPhone simulator. Also, AudioServicesPlaySystemSound() works for the same file, which is puzzling, but this indicates that sound playback might work with AVAudioPlayer as well.
I have 2 quick suggestions:
Check to see if that path actually gives you data. Alloc a NSData object using [[NSData alloc] initWithContentsOfFile:path] and inspect it in the debugger to see if you're actually loading that wav file.
I wrote a sample project that uses AVAudioPlayer here: AVAudioPlayer Example. As the code is pretty much the same as yours, the only thing I can imagine is that there is a problem with your data.
Check those out and see if it gets you anywhere!
I have just had a similar problem. I am not sure if it is the same solution, but my application records audio through the iphone and then plays it back to the user.
I thought I was doing the correct thing by telling my audio session that it was going to record data, so I did the following:
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
Using this setting playback worked on the Simulator but not on the device.... the didFinishPlaying event never got raised.
I removed this line after realising I didn't need it an audio playback started working correctly. Now I just need to work out why it is so quiet :)
Paul

how to play caf file in iphone device?

i have used following code to play caf sound.but it works in simulator ,not in device...anyone can give solution?
NSString *pathe=[[NSBundle mainBundle] pathForResource:#"Watermovement3" ofType:#"caf"];
CFURLRef url =(CFURLRef)[NSURL fileURLWithPath:pathe];
AudioServicesCreateSystemSoundID(url,&okk);
AudioServicesPlaySystemSound(okk);
Check the capitalization of your filename: is it possible it's "watermovement3" or "WaterMovement3"?
You need to check the codecs of the caf file. The simulator supports more codecs than the device!
Try this way :
AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedFileNameURL error:&error];
[avPlayer prepareToPlay];
[avPlayer play];