我正在尝试使用 AVFoundation 框架在按钮单击时播放声音。以前,我使用音频工具箱,但是当我使用音频播放器时,什么都没有。奇怪的是,当我在手机上测试它并按下按钮时,音量控制不是正常的铃声音量控制。这表明它应该正在播放,但我在手机或模拟器上听不到任何声音。我使用了不同的文件,创建了新项目更改了代码,但没有任何帮助。
这是.h中的代码:
@interface MainViewController : UIViewController <AVAudioPlayerDelegate>
- (IBAction)test;
这是.m中的代码:
- (IBAction)test {
NSString *path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"mp3"];
AVAudioPlayer* theAudio = [[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL];
theAudio.delegate = self;
[theAudio play];
}