Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 05-23-2009, 08:49 PM   #8 (permalink)
deansx
Objective-C Code Monkey
 
deansx's Avatar
 
Join Date: May 2009
Location: Mountain View, CA
Posts: 127
deansx is on a distinguished road
Default

Ahh, then you've already got myAction hooked up to a button in your view? If so, I'd do the code for more like this (Note that I'm not compiling this, so I might have some syntax errors, sorry...):

THE .h file


Code:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AVFoundation/AVAudioPlayer.h>

@interface MainView :UIView {
    // the player
    AVAudioPlayer* theAudio;
}
- (IBAction)myAction:(id)sender;

@end
The source file:

Code:
#import "MainView.h"


@implementation MainView
- (IBAction)myAction:(id)sender {
   if (theAudio == nil) {
      // Set up the player.
      NSString *path = [[NSBundle mainBundle] pathForResource:@"boom" ofType:@"wav"];
      theAudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPathath] error:NULL];
      theAudio.delegate = self;
      [theAudio play];
   }
   else if ([theAudio isPlaying]) {
      // there's no point in stopping it, if it's not playing...
      [theAudio stop];
   }
   /* Uncomment this if you want the button to start the sound again...
   else {
      [theAudio play];
   }
   */
}	
@end
... at least I hope that this is close. To what you want. I didn't realize that you were using myAction as the target for the button. Also, note that this board seems to be automatically replacing colon open paren with a frowny face...

Last edited by deansx; 05-23-2009 at 09:25 PM.
deansx is offline   Reply With Quote
 

» Advertisements
» Online Users: 469
16 members and 453 guests
bignoggins, buggen, ClerurcifeDer, Clouds, Duncan C, iAppDeveloper, ipodphone, krisk, linkmx, ryantcb, sacha1996, Sami Gh, stanny, Steven.C, Studder, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,638
Threads: 94,102
Posts: 402,827
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Studder
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:48 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.