Hi guys ive been trying to find a way to mute and unmute (that is play audio) via Objective-C in-code, regardless of whether the app is in mute mode or not (physical mute toggled)
I see Instagram does that, where you can press the mute icon even if your phone is mute, and it will play video audio.
You want to look at audio session categories
With this code you can play audio when the Ring/Silent switch to silent.
NSError *setCategoryError = nil;
BOOL success = [[AVAudioSession sharedInstance]
setCategory: AVAudioSessionCategoryPlayback
error: &setCategoryError];
if (!success) { /* handle the error in setCategoryError */ }
Related
I've found 1 question on SO about the same issue and it remains unanswered.
AVAudioPlayer affects system sounds
AVAudioSessionCategoryAmbient is not the answer as that only allows the app to play its audio simultaneously with another app's audio, that's not it.
I play sound effects such as button clicking sounds etc with SystemSound and background music with AVAudioPlayer. When AVAudioPlayer starts while SystemSound is being played, that particular system sound becomes like 5 times louder all of a sudden. Has anyone else experienced that? What could be the fix?
These are in SoundControl.m and I'm playing gameBG right after transition.
-(void)gameBG {
self.sharedSoundControl = [SoundControl sharedSoundControl];
if (self.sharedSoundControl.musicOff == NO) {
self.audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:#"bgm" withExtension:#"mp3"] error:nil];
self.audioPlayer.numberOfLoops = -1;
self.audioPlayer.volume = 0.3f;
[self.audioPlayer play];
}}
-(void)transition {
if (self.sharedSoundControl.effectOff == NO) {
NSString *soundPath = [[NSBundle mainBundle] pathForResource:#"transition" ofType:#"caf"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:soundPath], &soundID);
AudioServicesPlaySystemSound(soundID);
}}
EDIT
Just realized that if another instance of AVAudioPlayer is already being played anywhere, such behavior does not occur. It happens when there's no active AVAudioPlayer and you play system sound followed by AVAudioPlayer then voila, that system sound you just played became REALLY loud all of a sudden.
I'm thinking, if I get no answers, I'm just gonna have to play some random muted AVAudioPlayer right before playing system sound so this won't happen but I'm hoping to get some "real" fix for this.
Please do not suggest to play some random muted AVAudioPlayer right before playing system sound because that's what I just said..
I am sharing audio file after getting it from iPhone Music Library. After successfully getting path of audio, I implemented following lines of code
dispatch_async(dispatch_get_main_queue(), ^{
if ( [self isWhatsAppInstalled] )
{
_docControll = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:songPath]];
_docControll.UTI = #"net.whatsapp.audio";
_docControll.delegate = self;
[_docControll presentOpenInMenuFromRect:CGRectMake(0, 0, 0, 0) inView:view animated: YES];
} else {
[self alertWhatsappNotInstalled];
}
});
It opens dialogue in whatsapp to send audio, so I press to send and whatsapp shows progress that audio is sending, but meanwhile it fails.
Here is the path: "/var/mobile/Containers/Data/Application/9FE7FD43-6521-4E9E-A400-21C9FF03EE27/Documents/audio.waa"
I tried two formats of audio waa and mp3.
Audio successfully sent if it is being sent from Main Bundle.
Any Help will really be appretiated.
Blockquote
Hi i am using a location based app and wants to use iphone camera flashlight while in background.Unfortunately flashlight is working only in foreground ,it automatically turns off the flash in background even though the code is executing .
The code i used is working only in foreground
#import <AVFoundation/AVFoundation.h>
//flashcode
Class captureDeviceClass = NSClassFromString(#"AVCaptureDevice");
if (captureDeviceClass != nil) {
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([device hasTorch] && [device hasFlash]){
[device lockForConfiguration:nil];
if (device.torchMode == AVCaptureTorchModeOff)
{
[device setTorchMode:AVCaptureTorchModeOn];
[device setFlashMode:AVCaptureFlashModeOn];
//torchIsOn = YES;
}
else
{
[device setTorchMode:AVCaptureTorchModeOff];
[device setFlashMode:AVCaptureFlashModeOff];
// torchIsOn = NO;
}
[device unlockForConfiguration];
}
}
This is a normal behavior.
Apple's sandboxing will not allow you to keep the flash on while your app is in the background.
There is no workaround unless we are talking about a jailbroken app.
Edit:
Apple is very strict on it's system's APIs usage. Especially when it comes to:
- User privacy
- Battery life
- User experience
In the case of the flashlight, the last two items are relevant. Apple will not let an app drain the battery when not in the foreground and iOS users are used to the fact that flashlight, camera, microphone... are immediately disabled when going to the background (with an exception for the microphone in some background modes cases).
To answer the comment on your initial post, iOS controls your hardware. So since Apple decided they don't want the light to stay on when the user closes the app, iOS will just power off the flash when your app goes in the background. Your app has no authority to prevent it.
I'm trying to test to play a song in with silence mode activate.
I have this code which I picked up from the answers in this web:
if(CFStringGetLength(state) == 0) {
//SILENT
NSLog(#"Silent switch is on");
//create vibrate
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
//this 2 line below use to play audio even in silent/vibrator mode too
UInt32 audioCategory = kAudioSessionCategory_MediaPlayback;
AudioSessionSetProperty( kAudioSessionProperty_AudioCategory, sizeof(UInt32), &audioCategory);
NSError *error;
AVAudioPlayer *player = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:soundPath] error:&error];
[player play];
NSLog(#"error %#",error);
}
else {
//NOT SILENT
NSLog(#"Silent switch is off");
AudioServicesPlaySystemSound(finishedSound);
}
I'm testing in a iPod touch 4 generation ios 6 the sound sounds when the app is in background and foreground (with volume to a minimum).
But with iPad one with iOS 5.1.1 it doesn't sound none of two situations.
The sound sounds with the silence mode off in both devices.
Do you know what can I change to achieve that the app sounds in both situations ?
I am using the following to play a video named intro
- (IBAction)PlayIntro:(id)sender {
NSString *videoPath = [[NSBundle mainBundle] pathForResource:#"intro" ofType:#"m4v"];
introplayer = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:videoPath]];
[self presentMoviePlayerViewControllerAnimated:introplayer];
}
I am having trouble setting up a notification so that once the video is finished playing the following would take place [self performSegueWithIdentifier:#"IntroS" sender:sender]; any help would be appreciated.
You should set up KVO for your MPMoviePlayerController. When the state of the movie changes, this fires giving you a chance to compare total playback time with the current playback time.
Given your snippet of code, this line will give you the current playback value:
NSLog(#"Movie player state: %g", introplayer.currentPlaybackTime);
I provided a brief overview on how KVO works with an example unrelated to video playback.
Though the topic is unrelated to video, it works just the same regardless of what variable you're watching using KVO.