Reputation: 141
I have an app in Obj-C that makes calls, I would like to play a sound when the user presses a button to start a call. Will the sound keep playing in the background when the call is made? If so, how can I achieve this?
Thank you in advanced
Upvotes: 3
Views: 1718
Reputation: 528
Create a AudioSession and play the audio file in following way- Hope it works for you!
func playBackgroundAudioSample() {
let path = Bundle.main.path(forResource:"Morningsun", ofType: "mp3")
self.customAudioSession = AVAudioSession()
do {
self.customAudioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: path!))
try self.customAudioSession?.setCategory(.playAndRecord, mode: .default, options: [.allowBluetooth, .allowBluetoothA2DP, .mixWithOthers])
self.customAudioPlayer?.play()
print("Playback OK")
}
catch let error {
print("Error while playing Bg sound:\(error)")
}
}
Upvotes: 0
Reputation: 1450
find sound in .caf or use dafault like this
@property (assign) SystemSoundID pewPewSound;
@property (nonatomic) NSArray * soundArray;
self.soundArray = @[@"DECKALRM",@"LOW_GONG10"];
// sound - url path to your sound,(NSString*)[self.soundArray objectAtIndex:0];
// @"DECKALRM" - you custom sound name in your project
NSURL * pewPewURL = [[NSBundle mainBundle]URLForResource:sound withExtension:@"caf"];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)pewPewURL, &_pewPewSound);
AudioServicesPlaySystemSound(self.pewPewSound);
to stop sound
AudioServicesRemoveSystemSoundCompletion(self.pewPewSound);
AudioServicesDisposeSystemSoundID(self.pewPewSound);
try for example this sound
Upvotes: 1
Reputation: 534
Please try this code this will play background sound during ongoing call.
But there was one problem with this it will create echo on receiver side.
Please share your feedback and try to resolve the issue of echo
Happy coding :)
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];
}
Upvotes: 1
Reputation: 7506
There is absolutely no way to play a sound while a Carrier phone call is in progress.
Sometimes, currently playing audio is interrupted by audio from a different app. On iPhone, for example, an incoming phone call interrupts the current app’s audio for the duration of the call. In a multitasking environment, the frequency of such audio interruptions can be high.
It is the same behavior with outgoing phone call. It will interrupts the current app's audio (playing/recording) during the duration of the call.
You can read more here : iOS Human Interface Guide
Upvotes: 3