How to Keep Sound Turned Off - ios

I have a two buttons. One that shows when the sound is playing and one when the sound isn't playing. However, when I switch it off and switch view controllers. And i come back to the main menu view controller the sound is back on. I want to keep the sound on or off based on the user setting.
(IBAction)pauseSound
{
BOOL shouldPlaySound = YES;
[[NSUserDefaults standardUserDefaults] setBool:shouldPlaySound forKey:#"shouldPlaySound"];
if (![sound isPlaying]) {
[sound play];
[soundButton setBackgroundImage:[UIImage imageNamed:#"SoundOn.png"] forState:UIControlStateNormal];
}else {
[sound pause];
[soundButton setBackgroundImage:[UIImage imageNamed:#"SoundOff.png"] forState:UIControlStateNormal];
}
[[NSUserDefaults standardUserDefaults] synchronize];
}

A good way to do this is saving wether or not to play the Sound in the NSUserDefaults, so the setting is remembered by your app. Set it i.e. via
BOOL shouldMuteSound = ...//set it corresponding to your button presses/visibility
[[NSUserDefaults standardUserDefaults] setBool:shouldMuteSound forKey:#"shouldMuteSound"];
//call this to immediatly update the userDefaults
[[NSUserDefaults standardUserDefaults] synchronize];
And load it via
BOOL shouldMuteSound = [[NSUserDefaults standardUserDefaults] boolForKey:#"shouldMuteSound"];
I didn't get it at first that the buttons are inside the same viewController that plays the sound. That code should help you:
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
BOOL shouldMuteSound = [[NSUserDefaults standardUserDefaults] boolForKey:#"shouldMuteSound"];
if(!shouldMuteSound){
[self playSound];
}else{
[self stopSound];
}
}
- (void)playSound{
if(!sound){
NSString *path = [[NSBundle mainBundle] pathForResource:#"Main Menu" ofType:#"mp3"];
sound = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
sound.numberOfLoops = -1;
}
if (![sound isPlaying]){
[sound play];
}
[soundButton setBackgroundImage:[UIImage imageNamed:#"SoundOn.png"] forState:UIControlStateNormal];
}
- (void)stopSound{
[sound pause];
[soundButton setBackgroundImage:[UIImage imageNamed:#"SoundOff.png"] forState:UIControlStateNormal];
}
- (IBAction)soundButtonTapped{
BOOL shouldMuteSound = [[NSUserDefaults standardUserDefaults] boolForKey:#"shouldMuteSound"];
shouldMuteSound = !shouldMuteSound;
[[NSUserDefaults standardUserDefaults] setBool:shouldMuteSound forKey:#"shouldMuteSound"];
[[NSUserDefaults standardUserDefaults] synchronize];
if(shouldMuteSound){
[self stopSound];
}else{
[self playSound];
}
}

Related

iOS - Turn SFX off

I'm very new to iOS development. I've currently got an app with some SFX in there. I've found a couple of other threads which explain the approach / logic, which I full understand, but it's the code I'm after as I have no idea of the syntax.
If I were to use a UI Switch, how would I turn off any SFX that's used within the app?
Any help and assistance would be greatly appreciated.
Thank you all.
How are you using your sfx? If you could upload your code it would help. But you can try this?
- (IBAction)SwitchValueChanged:(id)sender
{
if([sender isOn])
{
NSLog(#"Switch is ON");
//On Your SFX
}
else
{
NSLog(#"Switch is OFF");
//Off Your SFX
}
}
I don't really understand what you mean by Some SFX So its hard to provide help
There might be an option for you. So in the
.h file where the switch to turn it off, put this code.
#interface SettingScreen : UIViewController<AVAudioPlayerDelegate,AVAudioSessionDelegate>
{
AVAudioPlayer *audioplayer;
}
in .m file
-(void)viewDidLoad
{
NSString* BS_path_blue=[[NSBundle mainBundle]pathForResource:#"Click" ofType:#"mp3"];
audioplayer =[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:BS_path_blue] error:NULL];
audioplayer.delegate=self;
[audioplayer prepareToPlay];
UISwitch *soundsButton = [[UISwitch alloc]initWithFrame:CGRectMake(180, 8, 130, 27)];
[self.view addSubview:soundsOnOffButton];
[soundsOnOffButton addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
soundsButton.on = [[NSUserDefaults standardUserDefaults] boolForKey:#"sound"];
}
-(void)buttonAction:(UIButton *)sender
{
if (soundsButton.on)
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"sound"];
[[NSUserDefaults standardUserDefaults] synchronize];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"sound"]==YES)
{
[audioplayer play];
}
}
else
{
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:#"sound"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
Now if you want to play a sound for something. (Button used as example) in another screen , add delegate and first four lines to your file then add this line to their action.
-(void)buttonAction:(UIButton *)sender
{
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"sound"]==YES)
{
[audioplayer play];
}
// other code
}
Hope it works.

Applovin Interstitials Not Coming Up Every Time, Only Sparingly

So I set up Applovin SDK in my app and Applovin ads only come up sparingly. All of my other apps with Applovin installed have the ads coming up accordingly. Anyone heard of this kind of error?
Here is what I have in the Appdelegate:
[ALSdk initializeSdk];
Chartboost *cb = [Chartboost sharedChartboost];
cb.appId = #"TAKEN OUT";
cb.appSignature = #"TAKEN OUT";
// Required for use of delegate methods. See "Advanced Topics" section below.
cb.delegate = self;
[cb startSession];
[cb cacheMoreApps];
[cb showInterstitial];
//AppLovin Interstitial Ad
if (self.theTimer == nil)
{
self.theTimer = [NSTimer scheduledTimerWithTimeInterval:1000 target:self selector:#selector(countdownTracker) userInfo:nil repeats:YES];//Repeat to yes
}
}
-(void)didCloseInterstitial:(NSString *)location
{
[ALInterstitialAd showOver:self.window];
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"preferenceName"];
if ([savedValue isEqualToString:#"yes"]) {
}
NSString *valueToSave = #"yes";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:#"preferenceName"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)stopTimer
{
if (self.theTimer != nil)
{
[self.theTimer invalidate];
self.theTimer = nil;
}
}

Making a View for the first use of an app

I wish to make an app that, when the user opens it for the first time he selects his country from a picker in order to make his flag appear on the main screen. If the user closes the app and opens it again I want the app to start the menu screen with his flag on it directly.
I am using the following code now but it doesn't work at all. Every time the app is opened it takes him to the picker VIew (TappViewController)
- (void)viewDidLoad
{
[super viewDidLoad];
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"FirstLaunch"]) {
secViewController *menu = [[secViewController alloc] init];
[self presentViewController:menu animated:YES completion:^{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];}];
}
else{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Change your code like this:
- (void)viewDidLoad
{
[super viewDidLoad];
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"isPickerOpened"]) {
secViewController *menu = [[secViewController alloc] init];
[self presentViewController:menu animated:YES completion:^{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"isPickerOpened"];
[[NSUserDefaults standardUserDefaults] synchronize];
}];
}
}

Save checkbox values

As many others here I am new to Xcode and would like to ask a basic question. When checking a box and leaving Xcode, this value is not saved (unchecked box) when I reopen the Xcode project.
I have seen different discussions here: e.g. how to save a checkbox value check/uncheck to NSUserDefaults but it is not working in my code. Do you have a hint where the problem is?
In my .h I have created an instance of a BOOL (called “checked“) and a UIButton is connected both as an IBAction and as an IBOutlet.
- (void)viewDidLoad{
checked = NO;
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"Negative"];
BOOL checked = [[NSUserDefaults standardUserDefaults] boolForKey:#"Negative"];
[[NSUserDefaults standardUserDefaults] synchronize];
[super viewDidLoad];
NSUserDefaults *defaults70 = [NSUserDefaults standardUserDefaults];
checked = [defaults70 boolForKey: #"boxIsChecked70"];
[self checkTheBox];
[self updateLabels];}
- (IBAction)CheckCOUTCheckButton:(id)sender {
NSUserDefaults *defaults70 = [NSUserDefaults standardUserDefaults];
if (! checked) {
[_CheckBoxCOUTButton setImage:[UIImage imageNamed:#"Checkboxcheckedimage.png"] forState:UIControlStateNormal];
checked = YES;
[defaults70 setBool:checked forKey:#"boxIsChecked70"];}
else if (checked) {
[_CheckBoxCOUTButton setImage:[UIImage imageNamed:#"Checkboxuncheckedimage.png"] forState:UIControlStateNormal];
checked = NO;
[defaults70 setBool:checked forKey:#"boxIsChecked70"];}
[defaults70 synchronize];}
- (void) checkTheBox {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (! checked) {
[_CheckBoxCOUTButton setImage:[UIImage imageNamed:#"Checkboxuncheckedimage.png"] forState:UIControlStateNormal];
}
else if (checked) {
[_CheckBoxCOUTButton setImage:[UIImage imageNamed:#"Checkboxcheckedimage.png"] forState:UIControlStateNormal];
}
- (IBAction)ClearAllFields:(id)sender {
[_CheckBoxCOUTButton setImage:[UIImage imageNamed:#"Checkboxuncheckedimage.png"] forState:UIControlStateNormal];
checked = NO;}
Your problem is an problem of scope. You are defining a second variable called checked and confusing which one you're setting.
- (void)viewDidLoad{
checked = NO;
in this line you are setting the property - ie the self.checked value to NO.
BOOL checked = [[NSUserDefaults standardUserDefaults] boolForKey:#"Negative"];
[[NSUserDefaults standardUserDefaults] synchronize];
in this line however you are declaring a NEW local variable that will only exist within viewDidLoad.
try removing the BOOL before it. When viewDidLoad returns this local variable will go out of scope and disappear.
checked = [[NSUserDefaults standardUserDefaults] boolForKey:#"Negative"];
[[NSUserDefaults standardUserDefaults] synchronize];
so later on when you set the checkbox as part of checkTheBox you're reading the property, not your local variable that you set.
let me know if this isn't clear.
I am not sure if this is your only problem, but in your viewDidLoad, you are basically setting a value before you're reading it:
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"Negative"];
BOOL checked = [[NSUserDefaults standardUserDefaults] boolForKey:#"Negative"];
You probably want to do this:
BOOL checked = [[NSUserDefaults standardUserDefaults] boolForKey:#"Negative"];

How To save Recorded audio to my app?

Please help me i tired to getting answer using following Answer in stackOverflow
Ans 1
Ans 2
My code is
- (IBAction)RecordBtnClicked:(UIButton *)sender {
if (sender.isSelected) {
if (!recorder.recording) {
[session setActive:YES error:nil];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
temporaryRecFile = [prefs URLForKey:#"Test1"];
player = [[AVAudioPlayer alloc]initWithContentsOfURL:temporaryRecFile error:nil];
player.delegate = self;
player.volume = 1;
[player prepareToPlay];
[player play];
[sender setSelected:false];
}
`[recordBtn setImage:[UIImage imageNamed:#"a_recordp.png"] forState:UIControlStateSelected];
[recorder stop];
}`else{
if (player.playing) {
[player stop];
}`
if (!recorder.recording) {
[recordBtn setImage:[UIImage imageNamed:#"a_records.png"] forState:UIControlStateSelected];
NSString *documentPath = [pathComponent objectAtIndex:0];
NSString *pathTosave =[documentPath stringByAppendingPathComponent:[self fileNameForRecording]];
NSURL *url = [NSURL fileURLWithPath:pathTosave];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setURL:url forKey:#"test1"];
session = [AVAudioSession sharedInstance];
[session setActive:YES error:Nil];
// temporaryRecFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByDeletingLastPathComponent]];
[recorder record];
recordTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(updateTimeInterval) userInfo:nil repeats:YES];
}
[sender setSelected:true];
[discardBtn setHidden:false];
[saveBtn setHidden:false];
}
}
I have One button On first tap Recording is start and on 2 tap same button recording is stop and also third tap on same button i want to play same sound but i don't get desired file.
temporaryRecFile = [prefs URLForKey:#"Test1"];
it gives nil value ?
please provide me some suggestions for this if i do code in wrong way ?
any answer will be appreciated
Thanks in advance
Sometimes you cannot rely on iOS to save your data, you need to force it to do so by synchronizing.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setURL:url forKey:#"test1"];
[prefs synchronize]; //this will ensure it is written to the disk
Also one advice I can give you is to split that button functionality, you are doing way too much (record, stop record and play) in this IBAction. Perhaps it will be better for you to have 2 buttons first one recordOrStop() and playOrStop() but that's just my point of view as it will avoid problems.

Resources