簡體   English   中英

每次觸摸屏幕時在Cocoa中播放聲音

[英]Playing a Sound in Cocoa every time you Touch the screen

每當我在游戲中點擊屏幕時,我都會嘗試播放聲音...

基本上,每次角色跳躍時,我都不會發出任何聲音。 不幸的是,它似乎僅在循環(來自上一個跳轉)已從上一個會話開始播放時才播放循環...這意味着我實際上並沒有在每次觸摸屏幕時都受到聲音的影響...

使用此框架:

 #import <AVFoundation/AVFoundation.h>

這是我加載文件的方式:

 -(void)LoadMusic{
     NSString *jumpSound = [[NSBundle mainBundle] pathForResource:@"Burst" ofType:@"mp3"];
     jumpAffect = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:jumpSound] error:NULL];
     jumpAffect.delegate = self;
 }

在viewDidLoad中調用它:

 - (void)viewDidLoad{
     [self LoadMusic];
     ...
     [super viewDidLoad];
 }

這是我要播放的時間(每次我觸摸屏幕(使角色跳動)時):

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *) event{
     [jumpAffect play];
     jump = 16;
 }

我是否忘記了使循環能夠相互播放的功能? 還是可以停止循環並在下次觸摸時仍在播放而重新啟動它?

嘗試這樣的事情:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  [self playJumpSound];
}

- (void)playJumpSound {
  AVAudioPlayer *jumpEffect = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:jumpSound] error:NULL];
  [jumpEffect play];
}

如果您發現加載聲音的速度太慢,請提前加載其中的一些聲音,然后重新使用播放器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM