Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Stop Sound played

diegoMdiegoM Posts: 39New Users @
Hi, im currently using this code to play a sound, in the .h:
-(ibaction)playSound:(id)sender;

.m:
-(IBAction)playSound:(id)sender {

SystemSoundID soundID;
NSString *buttonName=[sender currentTitle];
NSString *soundFile=[[NSBundle mainBundle]
pathForResource:buttonName ofType:@aiff];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundFile], & soundID);
AudioServicesPlaySystemSound(soundID);
}

i wanted to know, how to make the sound stop, whenever the user leaves the viewcontroller. is it in the viewWillDissapear ? thanks
Tagged:

Replies

  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    diegoM said:

    Hi, im currently using this code to play a sound, in the .h:
    -(ibaction)playSound:(id)sender;

    .m:
    -(IBAction)playSound:(id)sender {

    SystemSoundID soundID;
    NSString *buttonName=[sender currentTitle];
    NSString *soundFile=[[NSBundle mainBundle]
    pathForResource:buttonName ofType:@aiff];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundFile], & soundID);
    AudioServicesPlaySystemSound(soundID);
    }

    i wanted to know, how to make the sound stop, whenever the user leaves the viewcontroller. is it in the viewWillDissapear ? thanks

    I'm not sure you CAN stop a AudioServices sound while it's playing.

    The AVAudioPlayer class is much more flexible, has lots more options (including the ability to stop or pause a sound while it's playing) and is easier to use. I'd suggest rewriting your code to use that class instead.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • diegoMdiegoM Posts: 39New Users @
    Duncan C said:

    diegoM said:

    Hi, im currently using this code to play a sound, in the .h:
    -(ibaction)playSound:(id)sender;

    .m:
    -(IBAction)playSound:(id)sender {

    SystemSoundID soundID;
    NSString *buttonName=[sender currentTitle];
    NSString *soundFile=[[NSBundle mainBundle]
    pathForResource:buttonName ofType:@aiff];
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath: soundFile], & soundID);
    AudioServicesPlaySystemSound(soundID);
    }

    i wanted to know, how to make the sound stop, whenever the user leaves the viewcontroller. is it in the viewWillDissapear ? thanks

    I'm not sure you CAN stop a AudioServices sound while it's playing.

    The AVAudioPlayer class is much more flexible, has lots more options (including the ability to stop or pause a sound while it's playing) and is easier to use. I'd suggest rewriting your code to use that class instead.
    will do. Thanks!
Sign In or Register to comment.