mmoore410
mmoore410

Reputation: 427

Want to keep sound from playing when view loads

I am having a user move an object and when the move is ended it will update a display and play a sound. It all works good but I don't want the sound to play when the view first loads. How do I keep this from happening? Would I need to put something in the viewDidLoad? Here is the code that fires the event. Thanks for any help. `

// Updates the text field with the current rotation angle and play sound.
- (void) updateTextDisplay
{
NSError *error = nil;
if (imageAngle >=0 && imageAngle <=180){
    ComplimentPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"nice" ofType:@"m4a"]] error:&error];
    ComplimentPlayer.delegate = self;
            textDisplay.text = [NSString stringWithFormat:@"blah blah blah"];
} else if (imageAngle >180 && imageAngle <=360){
    ComplimentPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]
        pathForResource:@"good" ofType:@"m4a"]] error:&error];
    ComplimentPlayer.delegate = self;
            textDisplay.text = [NSString stringWithFormat:@"blah blah"];
}
NSTimeInterval playbackDelay = 0.5;   
[ComplimentPlayer playAtTime: ComplimentPlayer.deviceCurrentTime + playbackDelay];

} `

Upvotes: 0

Views: 98

Answers (2)

DetartrateD
DetartrateD

Reputation: 74

EDIT: Does the whole view reload OR just the UILabel ?

Set an int value to check for when view first loads, store the value in user defaults and update the value when the view reloads. Then use a further compare in your if logic when playing the sounds.

In viewWillAppear

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];//Add this or make it an ivar
int a = 0;  
int b = ++a; //set int to add 1 to itself
[defaults setInteger:b forKey:@"viewNumber"];
[[NSUserDefaults standardUserDefaults]synchronize];



   - (void) updateTextDisplay
  {
NSError *error = nil;
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; //Add this or make it an ivar
if (imageAngle >=0 && imageAngle <=180 && [defaults integerForKey@"viewNumber" >=2) //Add this here
{
ComplimentPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:          [[NSBundle mainBundle] pathForResource:@"nice" ofType:@"m4a"]] error:&error];
ComplimentPlayer.delegate = self;
        textDisplay.text = [NSString stringWithFormat:@"blah blah blah"];
  } else if (imageAngle >180 && imageAngle <=360 && [defaults integerForKey@"viewNumber">=2) //Add this here
{
ComplimentPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:   [[NSBundle mainBundle]
    pathForResource:@"good" ofType:@"m4a"]] error:&error];
ComplimentPlayer.delegate = self;
        textDisplay.text = [NSString stringWithFormat:@"blah blah"];
 }
 NSTimeInterval playbackDelay = 0.5;   
  [ComplimentPlayer playAtTime: ComplimentPlayer.deviceCurrentTime + playbackDelay];

In your AppDelegate add

- (void)applicationWillTerminate:(UIApplication *)application
{
 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 [defaults setInteger:0 forKey:@"viewNumber"];  //this restores the value to 0 upon termination 
 [[NSUserDefaults standardUserDefaults]synchronize];
  }

Upvotes: 1

Doon
Doon

Reputation: 97

separate updateTextDisplay method into two methods

- (void) updateTextDisplay;
- (void) playUpdateTextSound;

And do not call playUpdateTextSound() when the view first loads. And do call playUpdateTextSound() after call updateTextDisplay()

Upvotes: 1

Related Questions