Play a sound with silent mode iOS 5 and iOS 6 - objective-c

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 ?

Related

UIPrintInteractionController not working properly in iOS 13 beta device

When I try to load printer in objective c code the printer view goes out of the screen. Can anybody look into this?
I have tried with delegate methods of UIPrintInteractionController
but presenting the printer controller goes to out of the screen.
This code works properly in ios 12 I want the same printer screen in ios 13
UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];
[printController presentFromRect:self.actionsButton.frame inView:self.view animated:NO
completionHandler:^(UIPrintInteractionController
*printInteractionController, BOOL completed, NSError *error) {
if (completed) {
[self.actionsPopoverController dismissPopoverAnimated:YES];
self.actionsPopoverController = nil;
}
}];
Screen Short Below
I have faced the same issue. But this an OS issue for iOS 13. This issue has been fixed by Apple for iOS 13.1 beta. Try to run your code in iOS 13.1 beta

System sound becomes louder when AVAudioPlayer is initiated

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..

Iphone flashlight not working while app is in background

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.

Mute and UnMute in Objective-C on iOS

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 */ }

MPMoviePlayerController cannot play some 3gp format video

I use MPMoviePlayerViewController to play a video with format 3gp. some video can played,but others cannot be played. MPMoviePlayerViewController will loading and end immediately,just like the video is 0 second duration.
i thought there must be sty wrong with the video. but i use the iphone's video player inside the album, it can play the video!
So i think maybe MPMoviePlayerViewController is not powerful.and i wanna try to use avfoundation framework avplayer to play video. add a sublayer into my content view. failed..
that drive me crazy! does anyone met the some problem?
here is my code
-(void) playMovieWithSDK: (NSURL*) movieUrl
{
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) {
MPMoviePlayerViewController *mSdk4Player = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl];
mSdk4Player.navigationController.navigationBar.tag = 1000;
if (mSdk4Player) {
[self presentMoviePlayerViewControllerAnimated:mSdk4Player];
mSdk4Player.moviePlayer.scalingMode= MPMovieScalingModeAspectFit;
mSdk4Player.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
mSdk4Player.moviePlayer.shouldAutoplay = YES;
[mSdk4Player release];
[[NSNotificationCenter defaultCenter]addObserver: self
selector: #selector(movieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: mSdk4Player.moviePlayer];
}
}
else if ([[[UIDevice currentDevice] systemVersion] doubleValue] < 3.2) { // sdk 3.0 播放器
MPMoviePlayerController* mSdk3player = [[MPMoviePlayerController alloc] initWithContentURL: movieUrl];
mSdk3player.scalingMode = MPMovieScalingModeAspectFit;
[[NSNotificationCenter defaultCenter]addObserver: self
selector: #selector(movieFinishedCallback:)
name: MPMoviePlayerPlaybackDidFinishNotification
object: mSdk3player];
[mSdk3player play];
}
}
The video you are trying to play might not be compatible with the basic Codecs supported by MPMoviePlayerController.
According to the documentation:
This class plays any movie or audio file supported in iOS. This includes both streamed content and fixed-length files. For movie files, this typically means files with the extensions .mov, .mp4, .mpv, and .3gp and using one of the following compression standards:
H.264 Baseline Profile Level 3.0 video, up to 640 x 480 at 30 fps.
(The Baseline profile does not support B frames.)
MPEG-4 Part 2 video (Simple Profile)
If you use this class to play audio files, it displays a white screen with a QuickTime logo while the audio plays. For audio files, this class supports AAC-LC audio at up to 48 kHz, and MP3 (MPEG-1 Audio Layer 3) up to 48 kHz, stereo audio.