I want to use the same feature in app like VOYPI in iOS.
Playing a background sound during a call.
I have used the below code for playing background sound during call.
Link for the reference app is here.
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:self.bgAudioFileName ofType: #"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath ];
myAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
myAudioPlayer.numberOfLoops = -1;
NSError *sessionError = nil;
// Change the default output audio route
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
// get your audio session somehow
[audioSession setCategory:AVAudioSessionCategoryMultiRoute error:&sessionError];
BOOL success= [audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:&sessionError];
[audioSession setActive:YES error:&sessionError];
if(!success)
{
NSLog(#"error doing outputaudioportoverride - %#", [sessionError localizedDescription]);
}
[myAudioPlayer setVolume:1.0f];
[myAudioPlayer play];
}
This code will playbackground sound on ongoing carrier call using avaudioplayer.
I have set category AVAudioSessionCategoryMultiRoute.
Using this category receiver will able to listen background sound during ongoing carrier call from app.
But there was one problem with this it will create echo on receiver side.
Help me in this code.
Related
I’m trying to play a music with volume normalization, applyied by iVolume.
The music play 1 or 2 seconds and just after apply the volume normalization. Apparently the music is correctly, on native iPod player works fine.
After two days searching and testing, I can’t find any solution or tips.
I’m using AVAudioPlayer, because I need to get external audios.
Follow the music:
https://db.tt/52qohzJn
Piece of code:
NSError *setCategoryError = nil;
NSError *setAVError = nil;
NSString *path = [NSString stringWithFormat:#"%#/02GreenDay.m4a", [[NSBundle mainBundle] resourcePath]];
NSURL *soundUrl = [NSURL fileURLWithPath: path];
_audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: soundUrl error:&setAVError];
NSLog(#"Error: %#",[setAVError localizedDescription]);
AVAudioSession *session = [AVAudioSession sharedInstance];
if (![session setCategory:AVAudioSessionCategoryPlayback
error:&setCategoryError])
{
NSLog(#"%#",[setCategoryError localizedDescription]);
NSLog(#"Error AudioSession");
}
[session setActive:YES error:nil];
[_audioPlayer prepareToPlay];
[_audioPlayer play];
The _audioPlayer is a property.
Any ideas?
Thanks!
AVAudioPlayer successfully calls play method and is working properly in the simulator but it is not playing sound. The device has iOS 7.1.1. This is the code that I have used.
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *filePath = [mainBundle pathForResource:#"miata-hn" ofType:#"wav"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
NSError *error = nil;
/* Start the audio player */
self.playerAudio = [[AVAudioPlayer alloc] initWithData:fileData error:&error];
self.playerAudio.volume = 1;
if (error)
{
NSLog(#"error localized description %#",[error localizedDescription]);
}
if (self.playerAudio != nil)
{
/* Set the delegate and start playing */
self.playerAudio.delegate = self;
if ([self.playerAudio prepareToPlay] && [self.playerAudio play])
{
NSLog(#"player success");
}
else
{
NSLog(#"sound player failed to play audio");
}
}
else
{
/* Failed to instantiate AVAudioPlayer */
}
I got The solution. After adding this line of code the audio is audible in app.
AVAudioSession should be activated.
**
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&error];
[[AVAudioSession sharedInstance] setActive:YES error:&error];
**
Try using:
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:soundFilePath];
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
I am currently working on an application which should play Audio files through the iPhone receiver. I know it was easily possible before iOS 6/7, but those methods are deprecated now.
So does anybody know how it works on iOS 7?
This is my code, which does not work:
_audioSession = [AVAudioSession sharedInstance];
[_audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[_audioSession overrideOutputAudioPort:AVAudioSessionPortBuiltInReceiver error:nil];
NSString *ringtone = [[NSUserDefaults standardUserDefaults] stringForKey:#"ringtone"];
ringtone = [ringtone stringByReplacingOccurrencesOfString:#".m4r" withString:#""];
NSString *path;
path = [[NSBundle mainBundle] pathForResource:#"abto_ringbacktone" ofType:#"wav"];
NSError *error;
_player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:path] error:&error];
if (error) {
NSLog(#"Error: %#",error.localizedDescription);
}
}
[_player setNumberOfLoops:10];
[_player prepareToPlay];
[_player play];
[_player setVolume:0.1];
You cannot pass AVAudioSessionPortBuiltInReceiver to the overrideOutputAudioPort:error: because it takes enum as an argument, not a string.
Correct way to do this is to configure session as follows for Receiver:
[_audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[_audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideNone error:nil];
and for Speaker:
[_audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[_audioSession overrideOutputAudioPort:AVAudioSessionPortOverrideSpeaker error:nil];
I am creating a sound board app that plays one sound at a time. I would like a UIButton to be able to stop all other sounds playing and start playing its own sound when clicked. I have a single AVAudioPlayer set up to play all sounds (I change the sound file url when clicked and since I only want one sound at a time playing this is not an issue). I would like to know how to stop all other sounds and play the sound according to the button pressed, but if the button clicked is the button with the sound currently playing, the button only stops the sound. I know this is achievable through going to separate audioplayers and triggering the stop event but I want to avoid copy and pasting code and to stay efficient as possible, plus I only want / need a single audioplayer. Here is my code:
- (IBAction)play {
if (audioPlayer.playing == NO) {
NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:#"%#/k.mp3", [[NSBundle mainBundle] resourcePath]]];
NSError *error;
[audioPlayer release];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.numberOfLoops = 0;
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 release];
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];
} else
if (audioPlayer.playing == YES) {
[audioPlayer stop];
[start2 setTitle:#"Start" forState:UIControlStateNormal];
}
}
Any help is appreciated. Thank you in advance!
Just do this
-(void)stopAudio{
if(audioPlayer && [audioPlayer isPlaying]){
[audioPlayer stop];
audioPlayer=nil;
}
}
And call this function on every click..
- (IBAction)play2 {
[self stopAudio];
//Do your Stuffs
}
when your button is clicked stop your audio player and assign your new song url and then play it.Write this code when you click on button to play song.
if(audioPlayer.isPlaying)
[audioPlayer stop];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
NSURL *url = [NSURL fileURLWithPath:YourSoundURL];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
audioPlayer.delegate=self;
[audioPlayer play];
[start2 setTitle:#"Stop" forState:UIControlStateNormal];
My audio plays from the speaker and I want it play from the receiver (with a button).
At first I am trying to force the sound to output from the receiver. (I want to be compatible with iOS 3.2)
Here is what i have just after the [super viewDidLoad];
[[AVAudioSession sharedInstance] setDelegate: self];
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: nil];
// UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
[[AVAudioSession sharedInstance] setActive:YES error:nil];
and later when I call the play :
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:lblText.text
ofType:#"mp3"]];
NSError *error;
audioPlayer = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:&error];
if (error)
{
NSLog(#"Error in audioPlayer: %#",
[error localizedDescription]);
} else {
audioPlayer.delegate = self;
}
[self playAudio];
MPVolumeView *volumeView = [[[MPVolumeView alloc] initWithFrame:[volumeSlider frame]] autorelease];
[volumeSlider removeFromSuperview];
[self.view addSubview:volumeView];
What am I doing wrong ?
Here is the answer :
The override is only applicable to the kAudioSessionCategory_PlayAndRecord category and not to AVAudioSessionCategoryPlayback.
https://developer.apple.com/library/ios/#qa/qa1754/_index.html