简单的声音播放器函数调用
我想问一下,当按下“开始”按钮时,我应该写什么来播放功能e1play。我想播放 cat.wav 声音。怎么称呼呢。例如,当我制作 [MainView e1play] 时,它会显示一些错误。请帮忙。
这是我的 h 文件。
#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;
和m文件
@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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您需要一些简单的东西,例如播放声音,您可能应该这样做:
AudioServicesCreateSystemSoundID
AudioServicesPlaySystemSound
播放系统声音 使用AudioServicesDisposeSystemSoundID
处理系统声音...您只需为要播放的每个声音存储 SystemSoundID 即可。
If you need something simple like playing sound, you probably should go this way:
AudioServicesCreateSystemSoundID
AudioServicesPlaySystemSound
AudioServicesDisposeSystemSoundID
... you only need to store SystemSoundID for each sound you would like to play.