Here is a very useful code snippet and short tutorial for localize your audio-files in your application.
I am using the AVAudioPlayer Class. You can find the class reference of Apple here:
Let’s assume that you have a View with a ViewController that contains a button. If you click on the button your application should start to play a sound for example a .mp3-file.
1. First open your project:
2. Check the box “Use Base Internationalization”. After that click the “+” and add a language. (In my example the default/Base language is English and I add German.) The next screen asks you which files you want to localize. Keep them all unselected and click Finish. (In this example I only localize the audio-file so I unselected all items.)
3. Now I create a folder for my english audio file. After that I drag and drop the audio file to this folder.
4. Select your audio file and click on “Localize..” in the right pane. (File inspector)
Choose Base and click on “Localize”. Now the audio-file is your Base-Audio-File.
5. Now the File Inspector panel will show which languages this file belongs to. Currently, as you can see, the file is only localized for Base at the moment. Add your language by checking the box of your language. (In my case German)
6. Now your audio-file was duplicated. Right-Click on the new created file and choose “Open in Finder”. Now drag and drop your second audio-file with the other language to this folder. It is important that this audio-file is named same as the first one. Now make sure you select the “Replace File” option in the Finder prompt.
7. That was all the magic of the localization of audio-files. Now I create an IBAction for my button to play the audio-file, if you click on it.
Here is the code of my IBAction:
- (IBAction)playPauseButton:(id)sender { //My audio-file has the name crunch_wdh8 with the type mp3 NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"crunch_wdh8" ofType: @"mp3"]; NSURL *url = [[NSURL alloc] initFileURLWithPath:soundFilePath]; NSError *error; audioPlayer.delegate = (id)self; audioPlayer = [[AVAudioPlayeralloc] initWithContentsOfURL:url error:&error]; audioPlayer.delegate = (id)self; if (audioPlayer== nil) { NSLog(@"%@", error ); } else { [audioPlayer play]; }
Code works fine. I am doing exactly the same in my application “Female Fitness: Bikini Body”.
© Sebastian Steer 05.09.2014