how to play caf file in iphone device? - iphone

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];

Related

What Is the Easiest way to Play Sound on the Iphone?

What Is the Easiest way to Play Sound on the Iphone? I have an mp3 file, I'd rather keep it and not convert it to other format.
The simplest way I know of to play an MP3 file is to use the AVAudioPlayer class. Basically, just do (skipping error checking, setting delegate for detecting completion, etc):
NSURL* soundUrl = [[NSBundle mainBundle] URLForResource:#"soundFile" withExtension:#"mp3"];
AVAudioPlayer* player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&err];
[player play];
First add AVFoundation Framework to the project [Goto Target> Right click on project>Build Phases>Link Binary with Library> Click on +> select AVFoundation> Add]
// get file path
NSString *soundPath = [[NSBundle mainBundle] pathForResource: fileNameToPlay ofType:#"mp3"];
// allocate and refer to file
AVAudioPlayer *player =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: soundPath] error: NULL];
// set delegate
player. delegate = self;
// play
[player play];
Apparently this turned out to be quite a mess, but I found a solution. I'm running XCode 4.4.1.
Here are the steps that worked for me:
Drag mp3 file from finder into XCode project -> Supporting Files. Make sure to copy the file. let's assume the file name is sound.mp3.
Add AVFoundation framework to your project.
import < AVFoundation/AVFoundation.h>
#property (strong) AVAudioPlayer* soundPlayer;
use the following code to init the player and play the mp3:
NSURL *chemin = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/sound.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
self.soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:chemin error:&error];
[self.soundPlayer play];
Now at this point, I got an exception which is described here: AVFoundation iOS 5
In short it means that there is probably a bug in the simulator which shouldn't happen on the device. In my case I turned all exception on, which stopped my code from executing. What you should do is to remove the option of throwing all exceptions.
In addition, in my case it's 3-5 seconds until it first play the sound, so be patient when you don't get the sound right away. After it play for the first time, it will play instantly.

Unable to play audio on device (iOS)

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.

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];

How to play the iPod Library file in the AVPlayer

How can I get an ipod library music file into AVAudioPlayer?
As David mentions there is more work to do than this, for example you have to manage playing the next track in a collection of media items, but here is one way to do it with a set of MPMediaItems that a user selected from the iPod Picker. The AssetURL is what you use, it gives you a path to the MP3 file (e.g. ipod-library://item/item.mp3?id=-6889145242935454020)
NSURL *anUrl = [[mediaItems objectAtIndex: 0] valueForProperty:MPMediaItemPropertyAssetURL];
self.audioPlayerMusic = [[[AVPlayer alloc] initWithURL:anUrl] retain];
[self.audioPlayerMusic play];
Yes, you can play songs from the iPod library using the SDK without resorting to the MPMusicPlayerController class.
The more basic AVPlayer class can handle audio files from the iPod library by using the NSUrl value from the song's MPMediaItemPropertyAssetURL property. You have to do a lot more work to get everything setup properly, but it can be done.
The SDK has no provision for reading files from the iPod library (as you'd need to do to use AVAudioPlayer with it), probably for anti-piracy reasons. To play iPod library items, use the MPMusicPlayerController class.
Edit: This is no longer accurate. See the below answers which describe the use of the AVPlayer class.
Is there any possibility to get information about dB-metering in MPMusicPlayerController? Perhaps initiating an AVAudioSession for Recording in parallel would do the job?? I need dB-values to build some kind of volume-spectrograph.
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {
NSURL *url = [[mediaItemCollection.items objectAtIndex:0] valueForProperty:MPMediaItemPropertyAssetURL];
NSError *error;
self.player = [[AVAudioPlayer alloc] url error:&error];
if (!error) {
[self.player prepareToPlay];
[self.player play];
}
[mediaPicker dismissModalViewControllerAnimated:YES];
}

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.