首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >播放声音效果Xcode 5

播放声音效果Xcode 5
EN

Stack Overflow用户
提问于 2014-06-14 22:39:09
回答 2查看 1.2K关注 0票数 0

我想这样做,每次我点击图片,它会发出声音。在“链接框架和库”AudioToolbox和GameViewController.h添加:

代码语言:javascript
复制
#import <AudioToolbox/AudioToolbox.h>
@interface GameViewController : UIViewController
{
SystemSoundID PlaySoundID;
}
- (IBAction)PlayAudioButton:(id)sender;

(用按钮连接)。

在GameViewController.m中添加以下内容:

代码语言:javascript
复制
- (void)viewDidLoad
{

NSURL *SoundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Sound"    ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, &PlaySoundID);
}

- (IBAction)PlayAudioButton:(id)sender {

AudioServicesPlaySystemSound(PlaySoundID);

}

毕竟没有错误但听起来不管用.出什么问题了?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-14 22:46:18

用这个代替:

代码语言:javascript
复制
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                          pathForResource:@"yourSound"
                                          ofType:@"mp3"]];

AVAudioPlayer sound = [[AVAudioPlayer alloc]
                    initWithContentsOfURL:url
                    error:nil];

[sound play];
票数 0
EN

Stack Overflow用户

发布于 2014-06-14 23:04:53

不确定我是否正确地理解了你的问题,但当你按下按钮时,我就会听到定制的声音。

一定要

代码语言:javascript
复制
#import <AVFoundation/AVFoundation.h>

在.h或.m文件中

为类创建一个AVAudioPlayer *audioPlayer属性(在.h或.m中),然后执行以下操作:

代码语言:javascript
复制
-(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];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24224920

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档