jxdwinter
jxdwinter

Reputation: 2369

How can I close all my app sound?

There are somoe buttons in a view of my app, and it will play a sound when user click one button . I want to add a swich in app settings view, when user close the swich, no sound will be played when user clicked the buttons. Then the swich is on, sound will be played again. What should I do? Thank you in advance!

Upvotes: 0

Views: 181

Answers (2)

mrityunjay kumar
mrityunjay kumar

Reputation: 66

MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
if ([switch isOn]){
   [musicPlayer setVolume:1.0];
} else {
    [musicPlayer setVolume:0.0];
}

Hope this helps.

Upvotes: 2

zoul
zoul

Reputation: 104065

One option is to route all sound playback through a single class and add a property to this class for muting or sound skipping:

@interface Jukebox : NSObject
@property(assign, getter=isMuted) BOOL muted;
- (void) playSoundWithID: (NSString) soundID;
@end

@implementation Jukebox
@synthesize muted;

- (void) playSoundWithID: (NSString) soundID
{
    if (!muted) {
        // …
    }
}

@end

Or you can wrap AVAudioPlayer and make it check some app-wide boolean flag before playing:

@implementation CustomAudioPlayer

- (BOOL) play
{
    return ([[AppSettings sharedInstance] playSounds]) ?
        [super play] : NO;
}

Upvotes: 1

Related Questions