I want to localize the audio-files of my application. I doing this the same way as images, that means
select mp3-file
click on localize
Choose main
Add some language for example german
File is duplicated in de.lproj folder
overwrite this file with the german-audio-file (named exactly like the first one)
Now I clean and build it to the simulator, but the simulator is only playing the base-audio-file every time. (Also if the simulator languague is GERMAN!)
I delete the app a view times on the simulator and build it again, but there is only the base-audio-file playing.
My code for playing the audio-file:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/crunch_wdh8.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer.delegate = (id)self;
//NSLog(#"Entered the callback function");
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.delegate = (id)self;
if (audioPlayer== nil) {
NSLog(#"%#", error );
} else {
[audioPlayer play]; }
How can I fix this??
My problem was the wrong initalisation of the AVAudioPlayer.
This wasn't working with localized files!
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/crunch_wdh8.mp3", [[NSBundle mainBundle] resourcePath]]];
Here I found good tutorial for localize audio files with xcode:
Localization of audio-files in Xcode
Related
I have an appcelerator module written in Xcode. Everything works, except it throws an error when I try to play a sound file. The code works in Xcode in a standalone app, but not as a module in appcelerator. I am not sure if the sound file is simply not building into the module, or if I have to change the path to something else. Where is the best place to store the sound files? Do I need to adjust the path? Please assist. Below is the playSound method that causes the error.
-(void)playSound:(NSString*)fileName
{
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:#"wav"];
NSURL *soundFileURL = [NSURL fileURLWithPath:path];
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
[_audioPlayer play];
}
I think the module you have used in xcode doesnt work in appcelerator. Rather you can get sound file from below code. Hope it helps.
var player = Ti.Media.createSound({url:"sound.wav"});
player.play();
I am using an AVAudioPlayer to play audio in my app, and it worked for a while. Suddenly, it just stopped working and I cannot figure out why. I created the AVAudioPlayer at the class level in the .h file. This is the code I use to init the player
-(void)initAudio
{
NSURL *song = [[NSBundle mainBundle] URLForResource:#"Prelude" withExtension:#"m4a"];
NSError *error = nil;
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:song error:&error];
[_audioPlayer prepareToPlay];
}
I call this method in the initWithSize function. error is nil, and I have verified that the player object contains the actual audio file. The audio file plays back fine in my OS environment and I have tried adding it to the build phase compile sources just to be safe, even though beforehand it worked fine without it explicitly there. When I trigger the play event, it will play the first snippet of the audio and then nothing. I log the current time of the player and it is 0. Everything else about the app is continuing on and functions without issue. I do not interfere with the avaudioplayer object in any way except to play/pause/preparetoplay.
I am completely stumped as it worked fine and then suddenly no longer works. I have tried to clean the project, I have even dumped the derivedData.
Any ideas?
I think your player has an error when you try to play the song.
You need to check the error first.
In my opinion, it seems like OSStatus error -43.
When you update an app, file path can be changed.
For example,
old path : /var/mobile/Applications/F28E0C2C-4AE2-4C9D-906F-414AE1669D90/Documents/AAA.mp3
new path: var/mobile/Applications/6086783B-A410-42C3-9BAE-7BA755D0E528/Documents/AAA.mp3
Try this code instead:
NSError *error;
NSURL *song = [[NSBundle mainBundle] URLForResource:#"Prelude" withExtension:#"m4a"];
_audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:song error:&error];
if(error){
NSLog(#"[error localizedDescription] %#", [error localizedDescription]);
NSString *fileName = [[song absoluteString] lastPathComponent]; //this code is just example. get your fileName like "AAA.m4a"
NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", docDirPath , fileName];
_audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:filePath] error:&error];
}
_audioPlayer.delegate = self;
[_audioPlayer prepareToPlay];
[_audioPlayer play];
}
I hope this will help you.
Cheers
Hi in my application, audio is playing when user press the button but now I have two audio buttons like audio1 and audio2, now I want to play second audio after a time interval once the audio1 is completed by clicking the same button.
- (IBAction)btn1:(id)sender {
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"audio" ofType:#"mp3" ];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
}
The above code I have used for play one audio now I want to play second audio once the first audio completed please tell me how to make it.
Thanks.
Try this:
You have to add a bool variable with the name 'firstAudioPlayed' on start the variable should be NO or FALSE.
- (IBAction)btn1:(id)sender {
if (![self.audioPlayer isPlaying]) {
NSString *audioPath;
if (self.firstAudioPlayed) {
audioPath = [[NSBundle mainBundle] pathForResource:#"audio2" ofType:#"mp3" ];
} else {
audioPath = [[NSBundle mainBundle] pathForResource:#"audio1" ofType:#"mp3" ];
}
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
self.firstAudioPlayed = !self.firstAudioPlayed;
}
}
I'm not sure if it's working like this, else just ask with a comment...
Explication of the code:
First you check if the player is already playing a song, if he isn't, you check if the first audio already has played (self.firstAudioPlayed) with this you can give the correct path to load the audio file. Now you play this audio file.
(Sorry for my grammar and my english, corrections are welcome)
You probably should use player's delegate. Set audioPlayer.delegate=self; and implement audioPlayerDidFinishPlaying:successfully: method to see when a player finished playing.
Then you can do whatever you want in there, for example start the second audio.
I play a sound effect using:
-(void)playCorrectSound
{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"Yes" ofType:#"mp3"];
NSURL *soundURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
if(! self.myAudioPlayer || ![soundURL isEqual:self.myAudioPlayer.url]){
self.myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:nil];
self.myAudioPlayer.delegate = self;
}
[self.myAudioPlayer setVolume:1.0];
[self.myAudioPlayer prepareToPlay];
[self.myAudioPlayer play];
}
I've added this code to three devices, all iPhone 5, all iOS 7.0.6 Two of them play the sound at significantly lower volume than another phone. All System preferences appear the same across all three devices. Would anyone know why one would be so much louder (and how I want it)?
hi does anyone know how to add audio to buttons i have added the framework creatd an action that place the sounds but when i press the button no sound plays?
heres what i have done so far
-(void)buttonSound{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/ButtonSound.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer2.numberOfLoops = -1;
[audioPlayer2 play];
}
- (IBAction)backToMenu:(id)sender {
[self buttonSound];
[self.navigationController popViewControllerAnimated:YES];
}
For short system sounds, you should use something like:
SystemSoundID sound;
CFURLRef alertURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)#"ButtonSound", CFSTR("mp3"), NULL);
OSStatus status = AudioServicesCreateSystemSoundID(alertURL, &sound);
AudioServicesPlayAlertSound(sound);
your audioplayer is getting released by immediately popping up navigation controller.
if you remove this line:
[self.navigationController popViewControllerAnimated:YES];
you should be able to hear the audio file (if the path is correct).