I use [MPMusicPlayerController iPodMusicPlayer] setting the ipod volumn before playing a local "beep.caf" file which use AVAudioPlayer.
Strange things happened, if I kill the system ipod app before running the following code, the beep.caf played just 1~2 second which should be a 30 second audio.
The code like this:
self.ipodPlayer = [MPMusicPlayerController ipodMusicPlayer];
self.ipodPlayer.volumn = .5;
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: fileName];
NSError *err = nil;
self.trackPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&err];
if(err)
{
//LOG ERR
}
else
{
if ([trackPlayer prepareToPlay]) {
if (![trackPlayer play]) {
//ALog(#"Error:play sound failed.");
}
}
else
{
//ALog(#"Error:prepare play sound failed.");
};
}
This code works well under iOS4.3. I doubt if there is something special done when the iPodMusicPlayer init under iOS 6.0.1.
I need to set ipodPlayer's volumn before play local audio, how can I do this correctly?
Related
I'm using this code to load and begin playing a sound:
- (void) buzz {
if (buzzSound == nil) {
[[AVAudioSession sharedInstance] setActive: YES error: nil];
NSError *error;
NSBundle *mainBundle = [NSBundle mainBundle];
NSURL *soundFile = [[NSURL alloc] initFileURLWithPath: [mainBundle pathForResource: #"buzzer_8"
ofType: #"wav"]];
buzzSound = [[AVAudioPlayer alloc] initWithContentsOfURL: soundFile error: &error];
buzzSound.delegate = self;
if (DEBUG_BLE && error != nil)
printf("Error loading sound: %s\n", [[error localizedDescription] cStringUsingEncoding: NSUTF8StringEncoding]);
}
// Play the sound.
buzzSound.numberOfLoops = -1;
[buzzSound prepareToPlay];
[buzzSound setVolume: 1.0];
if (![buzzSound play]) {
self.buzzSound = nil;
[self buzzSound];
}
}
This code is triggered by a touch down event on a button. When the button gets a touch up event (on or off of the button), it stops the sound with:
[buzzSound stop];
The buzz sound itself is a short .wav file.
The first time buzz is called, the sound plays once. It does not repeat. On subsequent calls it does repeat. Why?
Intermittently, if I play the sound for a short period of time that is about the same length of time as one cycle through the sound file, the sound fails on subsequent calls to buzz, and play returns NO. Thereafter, play will always return NO until the sound is reloaded, after which is plays the sound once as noted above and then starts working correctly until the sound is played for a short time, again.
I do have audioPlayerDecodeErrorDidOccur:error: set up; no error is reported.
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 am trying to play partially downloaded file (I want the audio to start when it has enough downloaded data).
I have tried the following:
self.mutableData = nil;
self.mutableData = [NSMutableData data];
self.mutableData.length = 0;
GTMHTTPFetcher *fetcher =
[[self driveService].fetcherService fetcherWithURLString:song.filePath];
[fetcher setReceivedDataBlock:^(NSData *dataReceivedSoFar) {
[self.mutableData appendData:dataReceivedSoFar];
if (!self.audioPlayer.playing && self.mutableData.length > 1000000)
{
self.audioPlayer = nil;
self.audioPlayer = [[AVAudioPlayer alloc] initWithData:self.mutableData error:nil];
[self.audioPlayer play];
}
}];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
if (error == nil)
{
NSLog(#"NSURLResponse: %#", fetcher.response.);
}
else
{
NSLog(#"An error occurred: %#", error);
}
}];
But it starts playing the first 1-3 seconds and then the app either crashes or it plays that 1-3 seconds over and over again until the download finishes and then it plays the whole song.
Can I somehow achieve this with AVPlayer, or fix this problem?
AVAudioPlayer cannot play files that are open for writing / downloading, but AVPlayer can. Switch to AVPlayer.
To stream audio, you should use AVPlayer or Audio Queue Services.
initWithData isn't intended for this purpose. You can use initWithData after you have downloaded the entire file.
*This is my first stackoverflow question so apologies if I am doing something wrong.
I am trying to create a simple sound board app. I'm having trouble getting my AVAudioPlayer on xCode 4.1 (iOS 6.1.2) to update where the sound file to play is located. I wanted to avoid having multiple audio players (audioPlayer2, audioPlayer3, etc) so I am trying to use the same audio player but instead, update where the sound file is located. I only want one sound playing at a time so multiple sounds is obviously not an issue. Here is my code:
- (IBAction)play {
if (audioPlayer.playing == NO) {
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/k.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
//I added the next two lines to allow me to play AVAudio with MPiPodPlayer (which I found on this site, too) simultaneously.
AVAudioSession *audiosession = [AVAudioSession sharedInstance];
[audiosession setCategory:AVAudioSessionCategoryAmbient error:nil];
[audioPlayer play];
[start setTitle:#"Stop" forState:UIControlStateNormal];
}
else
if (audioPlayer.playing == YES) {
[audioPlayer stop];
[start setTitle:#"Start" forState:UIControlStateNormal];
}
}
- (IBAction)play2 {
if (audioPlayer.playing == NO) {
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/chicken.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
AVAudioSession *audiosession = [AVAudioSession sharedInstance];
[audiosession setCategory:AVAudioSessionCategoryAmbient error:nil];
[audioPlayer play];
[start2 setTitle:#"Stop" forState:UIControlStateNormal];
clicked = 1;
clicked2 = 0;
} else
if (audioPlayer.playing == YES) {
[audioPlayer stop];
[start2 setTitle:#"Start" forState:UIControlStateNormal];
clicked = 0;
}
}
My IBActions are linked to UIButtons and the 'start's are UIButton references. Whenever I click on 'start' it plays 'k.mp3' fine, however if I then click on 'start2' AFTER it starts to play the 'k.mp3' file, and not the chicken file. Whichever button I click on first is what the url is set to. This is my first iPhone OS application project so I realize there are probably some embarrassing coding mistakes in there (feel free to correct me). I'd like an answer that would be applicable to multiple buttons, even for some 30 buttons so I do not have to copy and paste stop audio player 1, 2, and 3 for each button.
To summarize: I have multiple buttons that play one sound each, I would like no more than one sound playing at a time; when I click on a button it plays 1 sound and all other audio players stop. I would prefer having only one AVAudioPlayer instance for simplicity. Thanks in advance!
Check if application control goes into - (IBAction)play2
And if control goes in it and it is still not working then you might try [audioPlayer release] before
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
that might do the trick for you
I am creating an iPhone application using AVAudioPlayer. It is working fine in iOs 6 devices. But it is not playing song from iPod library in iOs 5 and lower versions. When I tried to play a music file from NSBundle it is working, so the issue is only when I try to play the song from iPod library. Here is my code, please check
if (isPlaying) {
[audioPlayer stop];
isPlaying = NO;
}
extern NSMutableArray *songsUrlArray;
extern int selectedSongIndex;
audioPlayer.volume = 1.0;
NSString *urlString = [NSString stringWithFormat:#"%#",[songsUrlArray objectAtIndex:selectedSongIndex]];
NSURL *url = [NSURL URLWithString:urlString];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
audioPlayer.delegate = self;
audioPlayer.numberOfLoops = 0;
if (audioPlayer == nil)
NSLog(#"%#",[error description]);
else{
[audioPlayer play];
isPlaying = YES;
}
Edit:
Here is the error message that I got,
Error Domain=NSOSStatusErrorDomain Code=-43 "The operation couldn’t be completed. (OSStatus error -43.)"
When I googled, I found that OSStatus error -43 is getting when there is no contents available in the specified path. Here is the path to a song from iPod library in the 3GS device, ipod-library://item/item.mp3?id=564371652689620079. But its not playing. Please some
Please help
AVAudioPlayer only gained support for iPod URLs in iOS6. You'll have to use AVPlayer or AVQueuePlayer if you want to support target pre-iOS6 devices.
https://developer.apple.com/library/prerelease/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS6.html#//apple_ref/doc/uid/TP40011812-SW1