I have the following code that used to work to play a sound in my project however after iv done some playing around over the past few weeks on other aspects of the project it dose not seem to play.
The audio file is in the main directory and works in preview.
Any ideas?
//playsound
AVAudioPlayer *showsound;
NSString *audiopath = [[NSBundle mainBundle] pathForResource:#"mouse1" ofType:#"wav"];
NSURL *audiourl = [NSURL fileURLWithPath:audiopath];
showsound = [[AVAudioPlayer alloc]initWithContentsOfURL:audiourl error:Nil];
[showsound play];
It's because after the line showsound play your method comes to an end and showsound (your AVAudioPlayer) is released. Hence there is nothing to do any playing any more. Solution: retain it! (For example, set an instance variable to hold on to it.)
Related
I have Array of words which i want to pick one after the other and play using AVAudioPlayer, how can i achieve playing NSString using AVAudioPlayer?
I don't have the audio file and i have list of words to which i want to play each word using AVAudioPlayer programming .
i found to play the audio file using AVAudioPlayer but not able to find to play NSSting or word in AVAudioPlayer
Below is the code using to play the audio clips :
NSString *path = [NSString stringWithFormat:#"%#/audio.mp3", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath:path];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
You need to create an AVSpeechUtterance object using the words that you want the device to "speak," and then pass that object to AVSpeechSynthesizer. Those classes provide the methods you need to control the speech; there's no need to use AVAudioPlayer.
I managed to load an audio file onto a player and play it via the following method:
- (void) play:(NSString*)loop{
NSString* resourcePath = [[NSBundle mainBundle] pathForResource:loop
ofType:#"mp3"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:
[NSURL fileURLWithPath:resourcePath]];
player.delegate = self;
[player play];
player.numberOfLoops = 1;
player.currentTime = 0;
player.volume = 1.0;
}
}
(I removed a couple of lines in this post but the method itself works great)
The problem here is, that the sound only gets loaded when the method play() is being called which results in a little delay in between the button click that calls the method and the player actually playing it.
I have about 200 sounds in my app and one way would be to load each sound in its own AVAudioPLayer in the viewDidLoad, but I doubt that this is considered good code.
On Android, I am using the SoundPool for this concern and it works great but I don't think there is an equivalent for this on iOS.
I now managed to do it via AudioServicesPlaySystemSound()
I use a method that loads all the sounds in a giant array
- (void)prepSounds{
NSString *soundPath =
[[NSBundle mainBundle] pathForResource:#"bl3"
ofType:#"mp3"];
NSURL *soundPathURL = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID(
(__bridge CFURLRef)soundPathURL, &soundID[0]);
AudioServicesPlaySystemSound(soundID[0]);
}
where soundID[] is an array of SystemSoundID declared in the header.
Now, I can just easily fire the sound via AudioServicesPlaySystemSound(soundID[0]) where 0 relates to the first sound I loaded. The latency is close to 0 (I would guess 5ms)
However, this only works for sounds no longer than about 30 seconds.
If you need any assistance with that, just comment here, I'm glad to help.
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 the following code to get a sound on button click.And it works perfect. The problem is this sound is not getting reduced if i reduce the volume on the phone. I want to manage button click sound as per the volume button. I am not using input view.
SystemSoundID soundID;
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"sound1" ofType:#"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AudioServicesCreateSystemSoundID ((CFURLRef) CFBridgingRetain(soundUrl) , &soundID);
AudioServicesPlaySystemSound(soundID);
As Duncan C suggests, try AVAudioPlayer:
Import AVFoundation.framework.
Replace your code snippet with:
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"sound1" ofType:#"mp3"];
NSURL *soundUrl = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
[player play];
What I do is to create an AVAudioPlayer for my button sound and play that. AVAudioPlayer honors the system volume.
I've created a separate IBAction method that plays the sound, and then I link my buttons to both actions. That way it's easy to turn the sound on and off simply by linking the action. If you always want sounds to play then you might want to put a call to a sound play method directly in your IBAction methods.
Check Click here that might help you.
I've reached the point where i want to play some samples in my game/app. My instinct says use openAL ... I will have the situation where I will need to play multiple samples at once (however no more than 2 or 3) and the samples will be short (2 or 3 seconds).
My question is what is the best way of playing samples on the iPhone given that criteria?
This is a piece of sample code I am using:
1st option:
I defined this in my .h file
SystemSoundID topClick;
And in my .m file I firest load the sound (aiff file):
NSBundle* bundle = [NSBundle mainBundle];
NSString *topClickFile = [bundle pathForResource:#"top_click" ofType:#"aiff"];
Then, when I want to play the sound, I use:
AudioServicesPlaySystemSound(topClick);
2nd option:
You could also use the AVAudioPlayer (available since firmware 2.2 I think):
NSString *graffitiSprayFile = [bundle pathForResource:#"sound_effect" ofType:#"aiff"];
AVAudioPlayer* sprayAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:graffitiSprayFile] error:NULL];
sprayAudio.delegate = self;
[sprayAudio prepareToPlay];
[sprayAudio setNumberOfLoops:999];
The first option is very usable for relatively short sound effect.