I searched everywhere but I couldn't find an answer for my problem.
I play multiple sounds on my app and I let the user to adjust the volume with a button. So if the user wants, he can play with .5 volume. So I have this code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Orchestra" ofType:#"mp3"];
theOrchestras = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
[theOrchestras play];
and to reduce the volume this:
- (IBAction) volumeReduced {
theOrchestras.volume = 0.5;
}
When the sounds is playing the volume is reduced, no problem. But when the sounds stops, and plays again, the volume always goes back to 1.
To fix this I tried to save the volume as an Integer, but for some reason the value is always 0, I couldn't make it work:
.m
int64_t * volumeSaved;
.h
- (IBAction) volumeReduced {
volumeSaved = .5;
}
NSString *path = [[NSBundle mainBundle] pathForResource:#"Orchestra" ofType:#"mp3"];
theOrchestras = [[AVAudioPlayer alloc] initWithContentsOfURL: [NSURL fileURLWithPath: path] error: NULL];
theOrchestras.volume = volumeSaved;
[theOrchestras play];
But the audio now always plays with 0 volume, muted.
Is there any way to do this with a button?
Thanks!
You're declaring volumeSaved as a pointer to an integer instead of an integer, but then assigning to it like it's an integer. That's one oddity.
But your real problem is that the volume property in AVAudioPlayer is a float, not a int64_t. So change the type to:
float volumeSaved;
Try storing the volume with NSNumber.
Related
After the app starts, and I press the start button, it s "lagg" but I used this code into viewdidload and viewdidappear too:
gombhang = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"button4" ofType:#"mp3"]];
gombha = [[AVAudioPlayer alloc] initWithContentsOfURL:gombhang error:nil];
[gombha prepareToPlay];
gombha.delegate = self;
How can I fix this lagg? If I can t fix it apple will reject it?
As I know Audioplayer takes few seconds to load the audio file .
NSString *soundFile = [[NSBundle mainBundle] pathForResource:#"button4" ofType:#"wav"]; NSURL *soundFileURL = [NSURL fileURLWithPath:soundFile]; AudioServicesCreateSystemSoundID((CFURLRef) CFBridgingRetain(soundFileURL), &_MySound); AudioServicesPlaySystemSound(_MySound);
And another way: I made an empty/ muted 0.1 sec sound file, and played in viewdidload, and this method loaded the avaudioplayer before I clicked on the start button, so it doesn t lagg anymore.
I've implemented AVAudioPlayer into my iPhone app but the audio being played is far too loud. To quieten the sound slightly, I changed the volume to 0.2f to see if it would have any effect. The volume stayed exactly the same. Here's the code I'm using:
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/Blip.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
[audioPlayer setVolume:0.2f];
if (audioPlayer == nil)
NSLog(#"Whoopsies, there was an error with the sound!");
else
[audioPlayer play];
Why won't this turn the volume down?
Can somebody explain how I can?
Thanks!
Replace your code:
[audioPlayer setVolume:0.2f];
with following code:
audioPlayer.volume = 0.2;
Hope this helps you.
As explained in Apples Audio & Video Coding How-To's: You can't set the hardware volume.
How do I access the hardware volume controller?
Global system volume, including your application's volume, is handled
by iPhone OS and is not accessible by applications.
The volume of an AVAudioPlayer instance is a relative volume. You can play two sounds with two AVAudioPlayer instances and set their volume relative to each other.
EDIT:
If you want to control the global volume of your device, you can use MPVolumeView:
MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame:mpVolumeViewParentView.bounds];
[mpVolumeViewParentView addSubview:myVolumeView];
[myVolumeView release];
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.
I am using AVAudioPLayer to do the audio for my game. I have the following code in a method in a seperate class, which is called every time I want the 'boing' sound to play. Problem is, it leaks huge amounts of memory, to the point where the game becomes unplayable. Now i'm not releasing any of the pointers in the method, because at the time I only had one. But now I have 10 of these methods.
What is the best way to release the pointers in tis case? (Yes, I have tried releasing straight after [boing play];, this solves the leak (obviously) but the sound dosen't play so theres no point.
-(void)playBoing {
int x = (arc4random()%3)+1;
NSString *path = [NSString stringWithFormat:#"/boing_0%i.aif", x];
NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:path];
AVAudioPlayer *boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:nil];
boing.delegate = self;
boing.volume = 1;
[boing play];
}
It's possible there's a better solution, but I've had similar problems where releasing right away killed my process. This is the solution I took. Again, there may be better out there, but for a quick fix, it should do the trick.
In your header file, create the following:
AVAudioPlayer *boing;
Then in -(void)playBoing, do as you did, but change
AVAudioPlayer *boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:nil];
to
if (boing != nil) {
boing = nil;
[boing release];
}
boing = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath] error:nil];
This should ensure that there's only one instance of boing allocated at a time
-(IBAction)playSound{ AVAudioPlayer *myExampleSound;
NSString *myExamplePath = [[NSBundle mainBundle] pathForResource:#"myaudiofile" ofType:#"caf"];
myExampleSound =[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:myExamplePath] error:NULL];
myExampleSound.delegate = self;
[myExampleSound play];
}
I want to play a beep sound when a button is clicked. I had used the above code. But it is taking some delay in playing the sound.
Anyone please help.
There are two sources of the delay. The first one is bigger and can be eliminated using the prepareToPlay method of AVAudioPlayer. This means you have to declare myExampleSound as a class variable and initialize it some time before you are going to need it (and of course call the prepareToPlay after initialization):
- (void) viewDidLoadOrSomethingLikeThat
{
NSString *myExamplePath = [[NSBundle mainBundle]
pathForResource:#"myaudiofile" ofType:#"caf"];
myExampleSound =[[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:myExamplePath] error:NULL];
myExampleSound.delegate = self;
[myExampleSound prepareToPlay];
}
- (IBAction) playSound {
[myExampleSound play];
}
This should take the lag down to about 20 milliseconds, which is probably fine for your needs. If not, you’ll have to abandon AVAudioPlayer and switch to some other way of playing the sounds (like the Finch sound engine).
See also my own question about lags in AVAudioPlayer.
AudioServicesPlaySystemSound is an option. Tutorial here, sample code here.