The application I'm building is essentially an instrument with ten buttons, each linked to a separate AVAudioPlayer. When I try and allocate and initialize memory for each AVAudioPlayer in the ViewDidLoad method, I get no errors however the responsiveness of each button lags and there is considerable lag time before the same button can be triggered/heard again. I've rectified this situation by implementing the code below, however I'm not convinced it's the proper way of handling memory, especially because I have 10 of these methods.
- (IBAction)note1:(id)sender {
url1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"note1" ofType:#"wav"]];
unit1 = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:nil];
[unit1 play];
}
One thing you can try,
Call prepareToPlay method of AVAudioPlayer before playing that file.
The problem with your current code is that you are creating new instance of AVAudioPlayer every time you call a -note1: You can try this improvement:
- (IBAction)note1:(id)sender {
if (!url1)
url1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"note1" ofType:#"wav"]];
if (!unit1)
unit1 = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:nil];
[unit1 play];
}
However best way is to initialise your AVAudioPlayers at one place, when your UIViewController is loaded:
- (void) initializePlayers {
url1 = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:#"note1" ofType:#"wav"]];
unit1 = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:nil];
}
- (void) viewDidLoad {
[self initializePlayers];
}
- (IBAction)note1:(id)sender {
[unit1 play];
}
Do any additional setup after loading the view, typically from a nib.
NSURL* url = [[NSBundle mainBundle] URLForResource:#"Rondo_Alla_Turka_Short" withExtension:#"aiff"];
NSAssert(url, #"URL is valid.");
NSError* error = nil;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
if(!self.player)
{
NSLog(#"Error creating player: %#", error);
}
Related
I want to play a audio on Button click. I have tried this code but audio is not playing:-
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"sound" ofType:#"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:soundFileURL error:nil];
audioPlayer.delegate = self;
audioPlayer.volume=1.0;
[audioPlayer setNumberOfLoops:1];
[audioPlayer play];
you haven't created a player item
//declare in .h file
AVPlayerItem *playerItem;
//then in .m file
player = [AVPlayer playerWithPlayerItem:playerItem];
after that play it
[player play];
First, it appears you are using initWithData to access an audio file pointed to by a URL.
You should instead use initWithContentsOfU‌RL.
Another issue could be due to a common error in implementing the audio player.
If the AVAudioPlayer you are creating is inside a function, it will be released, or deallocated, at the end of the function. This is why the audio does not play.
To fix this problem, make the AVAudioPlayer a property or instance variable in your class so that it does not get immediately deallocated.
For example, a property is declared in your header as
#property(strong, nonatomic) AVAudioPlayer *audioPlayer;
and initialized in your implementation with
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:soundFileURL error:nil];
If this does not solve your problem, then the issue is likely to do with the file pointed to by your URL.
NSString *soundFilePath = [NSString stringWithFormat:#"%#/test.mp3",
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
print "soundFileURL" . check whether it is null(in case soundFileURL contains spaces it results/prints null ) and if it not a null value check the file name is spelled exactly correct or not(i.e., here your file name is "sound")
var asyncaAudioPlayer: AVAudioPlayer!
let audioFilePath = NSBundle.mainBundle().pathForResource(fileName, ofType: "mp3")
let audioFileUrl = NSURL.fileURLWithPath(audioFilePath!)
asyncaAudioPlayer = AVAudioPlayer(contentsOfURL: audioFileUrl, fileTypeHint: nil)
audioPlayerArray.append(asyncaAudioPlayer)
asyncaAudioPlayer.prepareToPlay()
asyncaAudioPlayer.play()
Your problem is you set NSURL for initWithData -> Wrong.
in case it sound play multi time, you should prepare your sound Data and keep it as strong ref.
and you should use CGD and global queue instead of main queue.
-(void)onTouchUpInside{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithData:self.soundFileData error:nil];
audioPlayer.delegate = self;
audioPlayer.volume=1.0;
[audioPlayer setNumberOfLoops:1];
if ([audioPlayer prepareToPlay] && [audioPlayer play])
{
NSLog(#"play Success!");
}
else
{
NSLog(#"Failed to play the audio file.");
}
});
}
I am trying to make a background music in my game with AVAudioPlayer, so I set this code at the initWithSize method of my SKScene:
//SETTING BACKGROUND MUSIC//
NSError * err = nil;
NSURL * musicFile = [[NSBundle mainBundle] URLForResource:#"Cosmic Rush" withExtension:#"m4a"];
music = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:&err];
if(err)
{
NSLog(#"%#", [err userInfo]);
return;
}
[music prepareToPlay];
music.numberOfLoops = -1;
[music setVolume:0.5];
//SETTING BACKGROUND MUSIC//
Then, in another method: [music play]; However, the sound never plays, and I have tried all sort of extensions: .mp3, .caf, .mp4 etc. I have also checked people who had the same problem, but none of the solutions that solved their problems worked with mine.
there is a easier way to add background music to your scene. i use the following steps and works just as good as any other methods and it is easier.
import the #import AVFoundation; at the top of your scene.
add it as a property right after your #implementation.
AVAudioPlayer *_backgroundMusicPlayer;
then declare a method for the music
- (void)playBackgroundMusic:(NSString *)filename
{
NSError *error;
NSURL *backgroundMusicURL =
[[NSBundle mainBundle] URLForResource:filename
withExtension:nil];
_backgroundMusicPlayer =
[[AVAudioPlayer alloc]
initWithContentsOfURL:backgroundMusicURL error:&error];
_backgroundMusicPlayer.numberOfLoops = -1;
[_backgroundMusicPlayer prepareToPlay];
[_backgroundMusicPlayer play];
}
after that create a reference to that method in your -(id)initWithSize:(CGSize)size like the following
[self playBackgroundMusic:#"bgMusic.mp3"];
that should take care of the background music for you.
Hi in my application, audio is playing when user press the button but now I have two audio buttons like audio1 and audio2, now I want to play second audio after a time interval once the audio1 is completed by clicking the same button.
- (IBAction)btn1:(id)sender {
NSString *audioPath = [[NSBundle mainBundle] pathForResource:#"audio" ofType:#"mp3" ];
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
}
The above code I have used for play one audio now I want to play second audio once the first audio completed please tell me how to make it.
Thanks.
Try this:
You have to add a bool variable with the name 'firstAudioPlayed' on start the variable should be NO or FALSE.
- (IBAction)btn1:(id)sender {
if (![self.audioPlayer isPlaying]) {
NSString *audioPath;
if (self.firstAudioPlayed) {
audioPath = [[NSBundle mainBundle] pathForResource:#"audio2" ofType:#"mp3" ];
} else {
audioPath = [[NSBundle mainBundle] pathForResource:#"audio1" ofType:#"mp3" ];
}
NSURL *audioURL = [NSURL fileURLWithPath:audioPath];
NSError *error;
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioURL error:&error];
[self.audioPlayer play];
self.firstAudioPlayed = !self.firstAudioPlayed;
}
}
I'm not sure if it's working like this, else just ask with a comment...
Explication of the code:
First you check if the player is already playing a song, if he isn't, you check if the first audio already has played (self.firstAudioPlayed) with this you can give the correct path to load the audio file. Now you play this audio file.
(Sorry for my grammar and my english, corrections are welcome)
You probably should use player's delegate. Set audioPlayer.delegate=self; and implement audioPlayerDidFinishPlaying:successfully: method to see when a player finished playing.
Then you can do whatever you want in there, for example start the second audio.
hi does anyone know how to add audio to buttons i have added the framework creatd an action that place the sounds but when i press the button no sound plays?
heres what i have done so far
-(void)buttonSound{
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/ButtonSound.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer2 = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer2.numberOfLoops = -1;
[audioPlayer2 play];
}
- (IBAction)backToMenu:(id)sender {
[self buttonSound];
[self.navigationController popViewControllerAnimated:YES];
}
For short system sounds, you should use something like:
SystemSoundID sound;
CFURLRef alertURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), (CFStringRef)#"ButtonSound", CFSTR("mp3"), NULL);
OSStatus status = AudioServicesCreateSystemSoundID(alertURL, &sound);
AudioServicesPlayAlertSound(sound);
your audioplayer is getting released by immediately popping up navigation controller.
if you remove this line:
[self.navigationController popViewControllerAnimated:YES];
you should be able to hear the audio file (if the path is correct).
I'm student working on a coding exercise and I am stumped!
I have two instances of AVAudioPlayer in my app - I can load the songs into each player using a direct path no problem.
What I'd like to be able to do is play multiple songs held within an array on each player.
Is this possible? and if so how would I go about doing this?
In my plist I have the key set to "One.mp3" as a string and the value as the path to the file... (was guessing at that part).
Thanks for any insight.
- (void)viewDidLoad {
[super viewDidLoad];
//multiple song array
NSString *soundsPath = [[NSBundle mainBundle] pathForResource:#"soundslist"
ofType:#"plist"];
soundsList = [[NSArray alloc] initWithContentsOfFile:soundsPath];
NSString* filename = [soundsList objectAtIndex:0];
NSString *path = [[NSBundle mainBundle] pathForResource:filename
ofType:#"mp3"];
AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path]
error:NULL];
self.audioPlayer = newAudio; // automatically retain audio and dealloc old file if new file is loaded
[newAudio release]; // release the audio safely
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer setNumberOfLoops:0];
[audioPlayer play];
}
It sounds like you want to try to play multiple songs on a single player. This isn't possible as stated in the AVAudioPlayer Overview (4th bullet). It is possible to play multiple songs with multiple AVAudioPlayers. Something like this:
NSMutableArray *audioPlayers = [NSMutableArray arrayWithCapacity:[soundsList count]];
for (NSString *soundPath in soundsList) {
NSError *error = nil;
AVAudioPlayer *audioPlayer = [AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
[audioPlayers addObject:audioPlayer];
audioPlayer.delegate = self;
[audioPlayer prepareToPlay];
[audioPlayer setNumberOfLoops:0];
[audioPlayer play];
}
I would suggest that you hardcode the file path instead of reading it in with a plist. Once you get that working, then you can try reading it in with the plist. Something like this would work:
NSURL *soundURL = [[NSBundle mainBundle] URLForResource:#"One" withExtension:#"mp3"];
AVAudioPlayer *soundPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error]
This way, you remove one level of indirection and one point of failure.