Localization of Audio - iphone

First of all i am a n00b.
After 3 days of trying and research i decided to get some external help.
What i did and my Project:
i make a book for children.
Now i am localizing my app.
for a Image i did it like this:
UIImage *schrift = [UIImage imageNamed:NSLocalizedString (#"Schrift_en.png", nil)];
image = [[UIImageView alloc] initWithImage:schrift];
image.frame = CGRectMake(75.5, 80, 617, 137);
[self.view addSubview:image];
[image release];
works great for me :)
Now i am trying to do the same with my Narrator Audiofile but can´t figure out how.
NSString *path = [[NSBundle mainBundle] pathForResource:#"Vorwort" ofType:#"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
self.audioPlayer = theAudio;
[theAudio release];
How can i change the code above to make it work and how to define ofType:#"mp3" in Localizable.strings-file?
Google can´t help me.
Any ideas?
slowly but surely i am losing my motivation.
It would be really cool if anyone could help me!
Thanks in advance
Planky

This isn't really what Localizable.strings is for. Check out the docs for NSBundle's pathForResource:ofType:
The method first looks for a matching resource file in the non-localized resource directory of the specified bundle. (… in iOS, it is the main bundle directory.) If a matching resource file is not found, it then looks in the top level of any available language-specific “.lproj” directories. (The search order for the language-specific directories corresponds to the user’s preferences.) It does not recurse through other subdirectories at any of these locations.
So if you previously had your audio file here:
YourBook.app/Vorwort.mp3
you should instead arrange it like this:
YourBook.app/en.lproj/Vorwort.mp3
YourBook.app/fr.lproj/Vorwort.mp3
No Volwort.mp3 should exists at the top level of the .app folder.
You will need to create the en.lroj/fr.lproj directories and drag them to your XCode project. Check your app bundle structure by right clicking on the .app in the Finder and clicking 'Show bundle contents'.

Try this
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%#", [[NSBundle mainBundle] resourcePath], NSLocalizedString (#"audiofilename.mp3", nil)]];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
self.audioPlayer = theAudio;
[theAudio release];

Related

alert sound when start recording

First time i am capturing video in my App, but when i start capturing i want that button click sound as native application. I have searched a lot and came to know that it is a system sound. Can anyone please tell me how can i add that sound in my application when i start capturing video and when i stop capturing.
Currently i am using AVAudioPlayer to play sound as shown below
NSURL *url1 = [[NSBundle mainBundle] URLForResource:str withExtension:#"wav"];
_startAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:nil];
_startAudioPlayer.delegate= self;
[_startAudioPlayer prepareToPlay];
[_startAudioPlayer play];
Thanks in advance.....
Just import AudioToolbox.framework.
Create the URL for the source audio file
NSURL *tapSound = [[NSBundle mainBundle] URLForResource: #"tap"
withExtension: #"aif"];
// Store the URL as a CFURLRef instance
self.soundFileURLRef = (CFURLRef) [tapSound retain];
// Create a system sound object representing the sound file.
AudioServicesCreateSystemSoundID (
soundFileURLRef,
&soundFileObject
);
Play sound
AudioServicesPlayAlertSound (soundFileObject);
Here is a nice tutorial link you can follow this
When you want to start capturing video at that time call this bellow method like this...
[self playSound];
use my this bellow method for play sound...
-(void)playSound
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"adriantnt_release_click" ofType:#"mp3"];
AVAudioPlayer* theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate=self;
[theAudio play];
}
first store any mp3 or any audio file in your project folder For Ex: i add adriantnt_release_click.mp3 file.
Also add AVAudioPlayerDelegate in your .h file and add AudioToolbox.framework in your project..

How can I get relative path of the folder in my project ?(iPhone)

Suppose, I have added one folder name "Images" in my project.How can I get the path to that folder? My main intention is to get the number of pictures in "Images" folder.
You should work a bit more on your question: it assumes a lot and requires the reader to guess.
I have added one folder name "Images" in my project
So I guess this means you added it as a folder reference
and I want to get it's path
And I guess that you want to do that at run time from your application, not at build-time from Xcode.
If so, you could do something like:
NSURL *containingURL = [[NSBundle mainBundle] resourceURL];
NSURL *imageURL = [containingURL URLByAppendingPathComponent:#"Images" isDirectory:YES];
NSFileManager *localFileManager = [[NSFileManager alloc] init];
NSArray *content = [localFileManager contentsOfDirectoryAtURL:imageURL includingPropertiesForKeys:nil options:NSDirectoryEnumerationSkipsSubdirectoryDescendants error:NULL];
[localFileManager release];
NSUInteger imageCount = [content count];
This code does not assume that all images are of the same kind.
[[[NSBundle mainBundle] pathsForResourcesOfType:#"jpg" inDirectory:#"Images"] count];
This returns the number of jpg images from the Images folder. This is the case if you added the images to your application bundle.

I am trying to play a MP4 file from my resources , but its not playing . Help me to fix this

I am trying to play a MP4 file from my resources , but its not playing . ... what else i have to do . Help me to fix this ....
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"line.mp4" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:urlStr];
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(0, 0, 300, 400);
[moviePlayer play];
Make sure that your movie file is not only listed in your Project Explorer, but also included in your "Copy Bundle Resources" list.
You can check by clicking on the root node in project explorer, select your target, and then click on the "Build Phases" tab. Once doing that, you should see four lists, the second one is your "Copy Bundle Resources" list. You can use the search field in the Build Phases tab to quickly see if your movie is included in this list. If it is not, drag and drop your movie file to this list.
I'm currently using Xcode 4.3.2, but these steps haven't change in a while. Hopefully these steps help.
Try instead of
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"line.mp4" ofType:nil];
this:
NSString *urlStr = [[NSBundle mainBundle] pathForResource:#"line" ofType:#"mp4"];

How do I loop a sound for an iPhone app?

I am trying to loop my sound file and I cannot find the parameter to loop the sound file! I'm about to pull out the last standing hair on my head. It loads fine....it plays....I just can't figure out the code on how to make it loop.
Here is my code:
NSString *eyeBGPath = [[NSBundle mainBundle] pathForResource:#"theeye" ofType:#"caf"];
CFURLRef eyeBGURL = (CFURLRef ) [NSURL fileURLWithPath:eyeBGPath];
AudioServicesCreateSystemSoundID(eyeBGURL, &eyeBackGroundID);
AudioServicesPlaySystemSound(eyeBackGroundID);
What am I doing wrong here?
I found this:
loops
Indicates whether the receiver restarts playback when it reaches the end of its content. Default: NO.
- (BOOL)loops
Return Value
YES when the receiver restarts playback when it finishes, NO otherwise.
Availability
Available in Mac OS X v10.5 and later.
See Also
– setLoops:
Declared In
NSSound.h
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSSound_Class/Reference/Reference.html%23//apple_ref/occ/instm/NSSound/loops
I'm confused on how to implement this.
This is the code that worked for me. If anyone needs help with this, let me know. Someone on the Apple Dev forum pointed me to the AVFoundation docs.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/theeye3.caf", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
Here is code to pick out a sound file based on a random number from an image array.
NSString *audioWiz = [[NSString alloc]initWithFormat:#"%d.caf", randNum];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:[#"%#/" stringByAppendingString:audioWiz], [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = 0;
audioPlayer.volume = 0.1;
[audioPlayer play];
I think the answer you are looking for is here.
Short answer, use a MPMusicPlayerController.

How to play audio file on websites on iPhone?

I want to play an audio file which is posted on a website. I'm trying to use AVAudioPlayer and set the url to its website link. But the bgPlayer turns out to be nil. I think I can download this audio file and play it afterwards, but is there any way to play it while I'm downloading it?
NSURL *bgURL = [NSURL URLWithString:#"http://www.radioslots.com/iphone/test/tmp/1.mp3"];
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:nil];
bgPlayer.numberOfLoops = -1;
if (bgPlayer) {
[bgPlayer play];
}
AVAudioPlayer isn't made to open network streams, I could be mistaken but I'm pretty sure it's not possible.
Just off the top of my head, I can think of two ways that would probably work:
1. Use MPMoviePlayer, as this can open network streams and despite the name works with audio. The only thing is this pops up the modal player with controls.
2. Download the network file and store it locally, then use AVAudioPlayer to play the local file.
I have just written answer here.Check it and replace the url used there with yours..
you just replace
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"songname" ofType:#"mp3"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
with
NSString *soundFilePath = #"Your URL";
NSURL *newURL = [[NSURL alloc] initWithString: soundFilePath];
There is a proper documentation available at this link
But I think you should also check the error code you are getting by this way:
NSError *error;
bgPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:bgURL error:&error];
Hope this helps.