Hey Guys,
im having some trouble passing a sound effect that i have on my game view to the settings view so it can but turned off it the uiswitch is moved to off.
in the gameview ive created an instance for theaudio like so:
then in .m ive added the code to call the file i want too play.
and on the settings page ive declared theaudio and created a property for it:
.h
and in the .m ive added the code to stop the audio if the switch is moved.
But i cant get it to work correctly can anyone tell me what im missing here please.
SunnyLi
im having some trouble passing a sound effect that i have on my game view to the settings view so it can but turned off it the uiswitch is moved to off.
in the gameview ive created an instance for theaudio like so:
Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import "SettingsViewController.h"
#import "CollisionViewController.h"
@interface CollisionViewController : UIViewController <UITextFieldDelegate, AVAudioPlayerDelegate>{
[B]AVAudioPlayer *theAudio;[/B]
}
then in .m ive added the code to call the file i want too play.
Code:
#import "CollisionViewController.h"
#import <AudioToolbox/AudioServices.h>
#import "SettingsViewController.h"
@implementation CollisionViewController
if (!stopdetection && CGRectIntersectsRect(ball.frame, paddle.frame))
{
score +=10;
scoreLabel.text = [NSString stringWithFormat:@"%d", score];
stopdetection = YES;
[B]NSString *filePath = [[NSBundle mainBundle] pathForResource:@"short" ofType:@"mp3"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath:filePath];
theAudio= [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
[theAudio play];
theAudio.numberOfLoops = -1;
SettingsViewController *second = [[SettingsViewController alloc] initWithNibName:nil bundle:nil];
second.theAudio = theAudio;
[filePath release];
[fileURL release];
[/B]
}
and on the settings page ive declared theaudio and created a property for it:
.h
Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVAudioPlayer.h>
#import "CollisionViewController.h"
@interface SettingsViewController : UIViewController{
IBOutlet UISwitch *muteSwitch;
AVAudioPlayer *theAudio;
IBOutlet UISlider *slider;
}
@property (nonatomic,retain) AVAudioPlayer *theAudio;
@property (nonatomic,retain) UISwitch *muteSwitch;
- (IBAction) volume;
- (IBAction) switchValueChanged;
- (IBAction) toggleButtonPressed;
and in the .m ive added the code to stop the audio if the switch is moved.
Code:
#import "SettingsViewController.h"
#import "CollisionViewController.h"
@implementation SettingsViewController
@synthesize theAudio;
@synthesize muteSwitch;
- (IBAction)volume{
theAudio.volume = slider.value;
}
- (IBAction) switchValueChanged{
if ([muteSwitch isOn])
[muteSwitch setOn:NO animated:YES];
else
[muteSwitch setOn:YES animated:YES];
}
- (IBAction) toggleButtonPressed{
if(muteSwitch.on){
[theAudio play];
}
else{
[theAudio stop];
}
}
But i cant get it to work correctly can anyone tell me what im missing here please.
SunnyLi