开发者

Simple sound player function call

I would like to ask what should I write down to play function e1play when button "start" is pressed. I want to play cat.wav sound. How to call it. When I make for example [MainView e1play] it shows me some errors. Please help.

Here is my h file.

#import <UIKit/UIKit.h>

#import <Foundation/Foundation.h>

#import <AVFoundation/AVAudioPlayer.h> 
@interface MainView : UIView {
AVAudioPlayer *avPlayer1;
}
- (void)e1play:(id)sender;
- (IBAction)start:(id)sender;

And m file

@implementation MainView
- (void)e1play:(id)sender{ //function to play sound of the cat
NSString *path =开发者_如何转开发 [[NSBundle mainBundle] pathForResource:@"cat" ofType:@"wav"];
avPlayer1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
[avPlayer1 play];
}
- (IBAction)start:(id)sender{  //button to lunch function e1play
//??????
}
@end


If you need something simple like playing sound, you probably should go this way:

  • create system sound with AudioServicesCreateSystemSoundID
  • play system sound with AudioServicesPlaySystemSound
  • dispose system sound with AudioServicesDisposeSystemSoundID

... you only need to store SystemSoundID for each sound you would like to play.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜