The FoneHome iPhone app has a feature where you can play a sound as part of a local notification. That sound is loud regardless of what the iPhone's volume level is set at.
How is it possible to get a local notification (or push) to play an audio alert that is loud independent of what the current iPhone volume level is? I tried just setting the soundName to a WAV file but it plays at whatever the current volume is, and I see no options to set it otherwise.
Try using the following code
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/audiofile.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[audioPlayer setVolume:x];
if (audioPlayer == nil)
NSLog([error description]);
else
[audioPlayer play];
Here 'x' is a floating point value which normally ranges from 0.0 to 1.0. However to increase the Volume level assign x value >10.0. But doing this may distort the sound a bit.
Related
I'm attempting to use the below code to play an mp3 file. It seems to run fine but no sound comes from the phone. The volume is up, the file is found (if I change the requested file to a name that doesn't exist I get an error), and no errors show up (even in the "playerError"). The only thing I see that might be a problem is perhaps the player deallocating before it finishes playing, but I'm not sure off hand how to fix that. Any suggestions as to what the problem might be and how to fix it? Thank you much!
...
if([responseString isEqual: #"Stop"]){
[self playStop];
}
...
- (void) playStop{
NSString *soundFilePath =
[[NSBundle mainBundle] pathForResource: #"stop"
ofType: #"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];
NSError *playerError;
AVAudioPlayer *newPlayer =
[[AVAudioPlayer alloc] initWithContentsOfURL: fileURL
error: &playerError];
[newPlayer play];
}
If you make your "AVAudioPlayer" an instance variable or a property (instead of a local variable within the function), so that it sticks around instead of being immediately released by ARC when "playStop" finishes, you will likely have much better luck.
I am trying to create buttons that play single sound files and one button that stops all of the sounds that are currently playing. If the user clicks multiple buttons or the same button in a short amount of time, the app should be playing all of the sounds simultaneously. I have accomplished this without much difficulty using the System Sound Services of iOS. However, the System Sound Services play the sounds through the volume that the iPhone's ringer is set to. I am now trying to use the AVAudioPlayer so that users can play the sounds through the media volume. Here is the code that I am currently (yet unsuccessfully) using the play the sounds:
-(IBAction)playSound:(id)sender
{
AVAudioPlayer *audioPlayer;
NSString *soundFile = [[NSBundle mainBundle] pathForResource:#"Hello" ofType:#"wav"];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile] error:nil];
[audioPlayer prepareToPlay];
[audioPlayer play];
}
Whenever I run this code in the iPhone Simulator, it does not play the sound but does display a ton of output. When I run this on my iPhone, the sound simply does not play. After doing some research and testing, I found that the audioPlayer variable is being released by Automatic Reference Counting. Also, this code works when the audioPlayer variable is defined as an instance variable and a property in my interface file, but it does not allow me to play multiple sounds at once.
First thing's first: How can I play an infinite number of sounds at once using the AVAudioPlayer and sticking with Automatic Reference Counting? Also: When these sounds are playing, how can I implement a second IBAction method to stop playing all of them?
First off, put the declaration and alloc/init of audioplayer on the same line. Also, you can only play one sound per AVAudioPlayer BUT you can make as many as you want simultaneously. And then to stop all of the sounds, maybe use a NSMutableArray, add all of the players to it, and then iterate though and [audioplayer stop];
//Add this to the top of your file
NSMutableArray *soundsArray;
//Add this to viewDidLoad
soundsArray = [NSMutableArray new]
//Add this to your stop method
for (AVAudioPlayer *a in soundsArray) [a stop];
//Modified playSound method
-(IBAction)playSound:(id)sender {
NSString *soundFile = [[NSBundle mainBundle] pathForResource:#"Hello" ofType:#"wav"];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:soundFile] error:nil];
[soundsArray addObject:audioPlayer];
[audioPlayer prepareToPlay];
[audioPlayer play];
}
That should do what you need.
Lets say I am dragging my finger on screen and I have 1 second .caf sound file in my bundle.
So what is the best way to play my sound file in a loop till i am dragging my fingers. And it should stop whenever I remove touches.
I know touches implementation. Just post your views about using sound file.
See AVAudioPlayer class, it worked pretty well for me for similar behaviour described in your question.
This is how I did it. At the moment, I don't know of a more efficient method...but here's what I used to loop my background sound. Hope this helps.
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/yourAudio.caf", [[NSBundle mainBundle] resourcePath]]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
In my application, I am displaying multiple images in rows and columns when i click on a image i will play corresponding sound using
someName = [someName stringByAppendingFormat:#".mp3"];
[audioPlayer stop];
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/%s", [[NSBundle mainBundle] resourcePath],[someName UTF8String]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.delegate = self;
When i am clicking on the one image i will get particular image sound. When i click on the another image the previous sound will be stopped and new sound corresponding the image will be played.
My requirement is when i click on the first image the sound must come and when i click on the second image the first sound must not stop and both must play and when i click on the third one the three sounds must play.
Please help me out
Apple recommends using an IMA4 encoded *.caf file for playing multiple sounds at once.
i am creating a application in which sound is played when a button is pressed and using a UISlider with which volume can be adjusted.Sometimes the volume of sound is too high and sometimes its too low even after increasing the volume of iphone to the full.How can i keep the volume to always high?? any possible way to integrate system volume with slider volume?? Using MPVolumview will get my app rejected i guess..
the code i am using on button touch is this
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:#"/sound.mp3"];
NSLog(#"Path to play: %#", resourcePath);
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:&err];
player.delegate = self;
[player play];
player.volume=.50;
player.numberOfLoops=-10;
-(IBAction)slidervaluechanged
{ player.volume=slider.value; }
}
Hemant,
The maximum volume you can have is when player.volume is equal to 1.0 (so long as the ringer volume is all the way maximum as well).
If the ringer volume is not at maximum, you can only go as high as whatever it is at by again using the value of 1.0
However, you could implement the MPVolumeView (I'm almost positive, as Pandora does it) and then you use that slider in your app instead. Then you can just set player.volume always equal to 1.0 and let the slider change the ringer volume.
More information on if you want to use MPVolumeView: http://developer.apple.com/iphone/library/documentation/MediaPlayer/Reference/MPVolumeView_Class/Reference/Reference.html and How do you implement an MPVolumeView?
I may be wrong, but I think Apple allows MPVolumeView if you use it as-is. They say in the class reference that it now changes the device ringer volume when you move the slider (which it didn't used to and that's why people were having to access private API to do). I'm going to try and implement it on my next update here in a week, so if I get rejected, I'll come back here and update this post to let everyone know.