AVAudioPlayer plays music in iPod touch 2G and 3G - ios

I'm trying to play an audio file on iPhone and iPod touch with AVAudioPlayer. The code works fine in iPhone and iPod touch 4G, I can hear the music. But when I test it on iPod touch 2G, iPod touch 3G, I can't hear anything.
Below is the code I use to play audio file:
NSString *zeroAudioPath = [[NSBundle mainBundle] pathForResource:#"TimerBell"
ofType:#"aif"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:zeroAudioPath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file
error:nil];
[file release];
self.zeroAudioPlayer = audioPlayer;
zeroAudioPlayer.delegate = self;
[audioPlayer release];
[zeroAudioPlayer prepareToPlay];
[zeroAudioPlayer play];

I found that changing from kAudioSessionCategory_PlayAndRecord to kAudioSessionCategory_AmbientSound corrected this issue. I suspect this has something to do with a hardware difference on the iPod touch related to recording.

You should use audiosession like this before you play:
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:&error];

Related

Reduce / decrease background ipod sound in my App

I need to make an application in iOS who make different sound like "beep" during her fonctionnement.
I have implemented interaction to background ipod with MPMusicPlayerController.
The probleme:
I do not hear the "beep" due to the volume of music from ipod.
I need to reduce the volume of the ipod but NOT reduce the volume of my application.
The tomtom application make that when the speaker give information about the direction to take. But I do not know how.
I have try this code:
- (void)playSoundCountDown{
NSError *errorObject = nil;
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryAmbient error:&errorObject];
if (errorObject) {
NSLog(#"Error setting category! %#",errorObject);
}
NSString *path = [[NSBundle mainBundle] pathForResource:#"countDownFiveToOne" ofType:#"caf"];
theSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
theSound.delegate = self;// (AVAudioPlayer *theSound;)
[theSound setVolume:0.4f];
[theSound play];
float volumeDecrease = 0.2f;
MPMusicPlayerController* iPodMusicPlayer = [MPMusicPlayerController iPodMusicPlayer];
[iPodMusicPlayer setVolume:volumeDecrease];
}
But "setVolume" reduce the volume of all sound of the device, included the sound of my app...
I need your help please.
Thanks.
Instead of decreasing the volume, try enabling ducking:
- (void)playSoundCountDown{
NSError *errorObject = nil;
[[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryAmbient
withOptions: AVAudioSessionCategoryOptionDuckOthers
error: nil];
NSString *path = [[NSBundle mainBundle] pathForResource:#"countDownFiveToOne" ofType:#"caf"];
theSound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:nil];
theSound.delegate = self;
[theSound setVolume:0.4f];
[theSound play];
}

iOS - Sending one audio stream to the built-in speaker and a different stream to a Bluetooth HFP speaker

I am building an alarm clock app which works in conjunction with a physical Bluetooth device. I need to send one audio file (a song selected from the iTunes Library) to the built-in speaker and a separate audio file to the Bluetooth device (which is, effectively, a Bluetooth HFP speaker) at the same time.
My first thought in completing this would be to use AVAudioSession's new AVAudioSessionCategoryMultiRoute, but iOS 7 does not detect my speaker as a possible route; it will detect either the built-in speaker or the HFP speaker, but it will not detect both simultaneously. Detecting both is required to send two files at the same time.
My second thought was to use AVAudioSessionCategoryPlayAndRecord (even though the app has no need for a microphone) and to use overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker and overrideOutputAudioPort:AVAudioSessionPortOverrideNone like so:
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:#"alarm"
ofType:#"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
self.bluetoothAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL
error:nil];
self.bluetoothAudioPlayer.delegate = self;
[self.bluetoothAudioPlayer play];
NSError *error = nil;
NSURL *selectedSongURL = [self.selectedSong valueForProperty:MPMediaItemPropertyAssetURL];
NSLog(#"selectedSongURL: %#", selectedSongURL);
if (selectedSongURL) {
if (![[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:&error]) {
NSLog(#"overrideOutput error: %#", error);
}
self.internalAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:selectedSongURL error:nil];
self.internalAudioPlayer.delegate = self;
[self.internalAudioPlayer play];
}
After this alarm is silenced, I run [[AVAudioSession sharedInstance] overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil]; to reset the override call.
However, this doesn't work because iOS just puts both audio signals on the same speaker.
Is there a way to send one audio stream to a Bluetooth HFP speaker and a different audio stream to the built-in speaker?

MPMoviePlayerController unmutes hardware mute in AVAudioPlayer

I've set my phone to muted state by switching vibrate/sound hardware button. In whole application some background music is playing unless the device is in muted state. So in this case, my music is playing but muted. Now in some point I want to present video (which is without sound but this shouldn't matter), however the video in MPMoviePlayerController turns music from AVAudioPlayer on even though the device is is 'muted' state. The AVAudioPlayer continues to play loudly even when i quit MPMoviePlayerController.
Some code samples, but i doubt it will help:
// movie without sound
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategorySoloAmbient error:nil];
NSURL *movieURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"myVideo" ofType:#"mp4"]];
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[_scrollView addSubview:_moviePlayerController.view];
[_moviePlayerController prepareToPlay];
[_moviePlayerController play];
// music
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
[_audioPlayer prepareToPlay];
[_audioPlayer play];
To fix this, I had to rewrite the code but instead of using MPMoviePlayerController I've used AVPlayer. Everything works like a charm now.

AVAudioplayer not playing song from iPod library in iOS 5

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

Audio playing in background not working in iOS Simulator

I've seen lots of questions about playing audio in the background. These questions often ask why the audio plays in the background of the simulator, yet the device simply wouldn't.
In my case, it's reversed.
When I tested my sample app in my iPad, it worked perfectly. However, when I began testing in the simulator, it simply wouldn't continue playing in the background.
Here are the codes I used:
NSURL *url = [NSURL URLWithString:firstSong];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audioPlayer setNumberOfLoops:-1];
[audioPlayer setCurrentTime:0];
[[AVAudioSession sharedInstance]setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance]setDelegate:self];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
[audioPlayer play];
Can anyone tell me why this won't work? Is it a bug or something?
Background audio is not supported in iOS Simulator afaik.

Resources