我想这样做,每次我点击图片,它会发出声音。在“链接框架和库”AudioToolbox和GameViewController.h添加:
#import <AudioToolbox/AudioToolbox.h>
@interface GameViewController : UIViewController
{
SystemSoundID PlaySoundID;
}
- (IBAction)PlayAudioButton:(id)sender;(用按钮连接)。
在GameViewController.m中添加以下内容:
- (void)viewDidLoad
{
NSURL *SoundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Sound" ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, &PlaySoundID);
}
- (IBAction)PlayAudioButton:(id)sender {
AudioServicesPlaySystemSound(PlaySoundID);
}毕竟没有错误但听起来不管用.出什么问题了?
谢谢。
发布于 2014-06-14 22:46:18
用这个代替:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"yourSound"
ofType:@"mp3"]];
AVAudioPlayer sound = [[AVAudioPlayer alloc]
initWithContentsOfURL:url
error:nil];
[sound play];发布于 2014-06-14 23:04:53
不确定我是否正确地理解了你的问题,但当你按下按钮时,我就会听到定制的声音。
一定要
#import <AVFoundation/AVFoundation.h>在.h或.m文件中
为类创建一个AVAudioPlayer *audioPlayer属性(在.h或.m中),然后执行以下操作:
-(void)viewDidLoad{
[super viewDidLoad]
NSError *error;
NSString *soundPath =[[NSBundle mainBundle] pathForResource:@"Sound" ofType:@"wav"];
NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
AVAudioPlayer *theAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundURL error:&error];
[theAudioPlayer prepareToPlay];
self.audioPlayer = theAudioPlayer;
}
- (IBAction)PlayAudioButton:(id)sender{
[self.audioPlayer play];
}https://stackoverflow.com/questions/24224920
复制相似问题