Skip to content

Instantly share code, notes, and snippets.

@VictorZhang2014
Created April 8, 2017 03:17
Show Gist options
  • Save VictorZhang2014/9a18b27a3af9f28e4baa65e6db0b1ac5 to your computer and use it in GitHub Desktop.
Save VictorZhang2014/9a18b27a3af9f28e4baa65e6db0b1ac5 to your computer and use it in GitHub Desktop.
iOS short sound playback , AudioServicesPlaySystemSound and AudioServicesPlaySystemSoundWithCompletion
#import "VLAudioSoundControl.h"
@implementation VLAudioSoundControl
+ (void)RingTone:(NSString *)ringName type:(NSString *)type {
[self pauseBackgroundSoundWithError:nil];
SystemSoundID send_voice_sound_id = 0;
//Load sound file
NSString *path = [[NSBundle mainBundle] pathForResource:ringName ofType:type];
//Register the sound to the system
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&send_voice_sound_id);
if ([[UIDevice currentDevice].systemVersion floatValue] >= 9.0) {
AudioServicesPlaySystemSoundWithCompletion(send_voice_sound_id, ^{
//Remove the sound from the system
AudioServicesRemoveSystemSoundCompletion(send_voice_sound_id);
[self resumeBackgroundSoundWithError:nil];
});
} else {
//playback
AudioServicesPlaySystemSound(send_voice_sound_id);
AudioServicesAddSystemSoundCompletion(send_voice_sound_id, CFRunLoopGetMain(), kCFRunLoopDefaultMode, soundCompletion, NULL);
}
}
//Underneath iOS 9.0
void soundCompletion(SystemSoundID sound,void * clientData) {
//Remove the sound from the system
AudioServicesRemoveSystemSoundCompletion(sound);
[GDAudioSessionControl resumeBackgroundSoundWithError:nil];
}
+ (void)Virbra {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
/*
* Resume the sound from background and deactivate current app audio session
* @param error Failure message
**/
+ (void)resumeBackgroundSoundWithError:(NSError **)error {
//Deactivate audio session in current app
//Activate audio session in others' app
//See here https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioGuidelinesByAppType/AudioGuidelinesByAppType.html#//apple_ref/doc/uid/TP40007875-CH11-SW1
[[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:error];
}
/*
* Pause the sound from the background and activate current app audio session
* @param error Failure message
**/
+ (void)pauseBackgroundSoundWithError:(NSError **)error {
//See here https://developer.apple.com/library/content/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/ConfiguringanAudioSession/ConfiguringanAudioSession.html#//apple_ref/doc/uid/TP40007875-CH2-SW1
AVAudioSession *session = [AVAudioSession sharedInstance];
//Set AVAudioSessionCategoryPlayback category mode for current app
[session setCategory:AVAudioSessionCategoryPlayback error:error];
//Activate audio session in current app
//Deactivate audio session in others' app
[session setActive:YES error:error];
}
+ (void)pauseBackgroundSoundWithCategoryRecord {
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryRecord error:nil];
[session setActive:YES error:nil];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment