Here is my code under the AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"PLAY SOUND CLIP WHILE LOADING APP");
NSURL *clip = [[NSBundle mainBundle] URLForResource: #"project" withExtension:#"m4a"];
self.startipPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
[self.startipPlayer play]
}
This only plays the audio once and not repeat. How do I make this code repeat itself or repeat the song at least?
Or any alternatives to create play background music than can loop would be helpful. Thank you.
Looking at the documentation for AVAudioPlayer, it appears to have property numberOfLoops, which, if set to negative number, repeats the song indefinitely until you call stop.
Probably something like this (untested):
NSURL *clip = [[NSBundle mainBundle] URLForResource: #"project" withExtension:#"m4a"];
self.startipPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:clip error:NULL];
startipPlayer.numberOfLoops = -1
[self.startipPlayer play]
Related
I have read some threads about this topic, and currently have this:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *soundFilePath = [NSString stringWithFormat:#"%#/testsoundsr.aiff",
[[NSBundle mainBundle] resourcePath]];
NSURL *soundFileURL = [NSURL fileURLWithPath: soundFilePath];
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
player.numberOfLoops = -1; //Infinite
[player play];
}
The sound is not played though, and I can only imagine that the problem is with the NSString definition which is used to reference the file
The file I'm trying to play is named 'testsoundsr.aiff' and I have dragged that into the project. Also I have added several frameworks to the project, including AudioToolBox.framework
---UPDATE---
I have now tried using an .mp3 file instead, but still getting no sound through.
Try player.delegate = self before [player play]
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.
I Have a question, I'm playing an AVAudioPlayer in a view controller(appMusicViewController), I set my session as AVAudioSessionCategorySoloAmbient, it runs good, when I navigate to other views the boundle music sounds in the background, but when I'm back in the appMusicViewController and I touch the play button, it doesn't stop the last play, so it plays the music at the same time. how can appMusicViewController knows the info of the last AVAudioPlayer allocated ?
-(void)viewDidLoad
{
[super viewDidLoad];
playList=[[NSArray alloc] initWithObjects:#"David Keller - Klangkissen (Extended Version)",#"Karmon - Wowshit (Original Mix) HQ",#"Phonique - Feel What You Want Feat. Rebecca",#"Toky - About You (Original Mix)", nil]; // 4 SONGS
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategorySoloAmbient error: nil];
artWork=[[NSArray alloc] initWithObjects:#"David Keller - Klangkissen (Extended Version).jpg",#"Karmon - Wowshit (Original Mix) HQ.jpg",#"Phonique - Feel What You Want Feat. Rebecca.jpg",#"Toky - About You (Original Mix).jpg", nil];
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:[playList objectAtIndex:count] ofType: #"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer.numberOfLoops=0;
myAudioPlayer.delegate=self;
myAudioPlayer.volume=self.volume.maximumValue/2;
}
- (IBAction)play:(id)sender
{
[myAudioPlayer play];
playing=YES;
}
In my app I use AVAudioPlayer to play some mp3. My code is:
- (void) play{
NSString *path = [NSString stringWithFormat:#"%#/%#",
[[NSBundle mainBundle] resourcePath],
[arraysound objectAtIndex:appDelegate.indexSound]];
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
soundID = [[AVAudioPlayer alloc] initWithContentsOfURL:filePath error:nil];
soundID.delegate = self;
[soundID prepareToPlay];
[soundID play];
}
but I have 40 sounds to play; I use everytime soundID to play my sounds
example I have sound1.mp3, sound2.mp3, sound3.mp3......
How can I release my soundId?
When my sound change I call "play" method but this method alloc a new AVAudioPlayer everytime
I Know
- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *) player
successfully: (BOOL) flag {
but I don't understand how to release my sound id inside this delegate method, because if I release inside soundId my app crash
after some minutes that I use my app I have with crash this message "shm_open failed: "Apple Audio Queue"
please help me...thanks
You don't want to release soundID in your delegate method. You want to release player. Judging from your code, soundID is an ivar. When the delegate gets called, soundID may have been set to another value. But the player parameter will be the AVAudioPlayer that has just finished playing.
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.