我正在为 iOS 7 创建一个 iPhone 游戏。我有一个名为 sharedResources 的类,它包含游戏中反复使用的某些数据的副本,包括大约 4 秒长的音效。
所以我正在创建这样的音效:
filePathTD = [[NSBundle mainBundle]
pathForResource:@"towerDamage" ofType:@"aiff"];
fileURLTD = [NSURL fileURLWithPath:filePathTD];
towerDamge = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURLTD
error:&error];
if (error)
NSLog(@"Error: %@", [error localizedDescription]);
然后像这样在其他地方调用它:
[towerDamage play];
我的问题是与我想要调用它的速度相比,音效太长了。因此,当我在它正在播放时调用它时,它不会同时播放,因为它已经在使用中,而且我只有一个副本。
所以我问:在不占用太多内存的情况下同时多次播放一个音效的最佳方法是什么?
我知道我可以制作多个副本,但如果我为每个需要它的角色制作一个副本,我可能有 30 个副本,而我可能最多只需要 5 个。我在想的是只制作 5 个副本,并且只使用当前未使用的副本。但是因为我是 iOS 开发的新手,所以我不确定除了这个 hack 之外是否还有更好的方法来完成这个任务?
最佳答案
在 AVAudioPlayer 实例上使用 prepareToPlay 将预加载声音文件。也许你可以将它们中的一些合理数量加载到一个数组中,然后根据需要循环遍历它们。如果您需要的数量超过分配的数量,那么您当时可以选择将一个新的加载到内存中,或者重新开始播放列表中的第一个。一个更复杂的系统可以根据因素或应用程序中发生的事情在阵列中存储更多或更少的声音。
也许是这样的? (为了简洁起见,我省略了错误检查和其他内容)
//setup the managing class to be
//<AVAudioPlayerDelegate>
///--- instance variables or properties
int nextIndexToPlayInArrayOfSounds = 0;
NSMutableArray *arrayOfSounds = [NSMutableArray array];
int numberOfSoundsNeeded = 10;
//make a method to setup the sounds
for (int i = 0; i < numberOfSoundsNeeded; i++) {
AVAudioPlayer *mySound = [[AVAudioPlayer alloc] initWithContentsOfURL:[[NSBundle mainBundle] URLForResource:[NSString stringWithFormat:@"soundNameOnDisk"] withExtension:@"aiff"] error:NULL];
mySound.delegate = self;
[arrayOfSounds addObject:mySound];
[mySound prepareToPlay];
}
//make a method to play a sound
//and then play the next sound in the list, or if past the end of the array, or play the first one
AVAudioPlayer *nextSoundToPlay;
if (numberOfSoundsNeeded > nextIndexToPlayInArrayOfSounds) {
nextIndexToPlayInArrayOfSounds++;
}else{
nextIndexToPlayInArrayOfSounds = 0;
}
//obviously I'm skipping error checking and nil pointers, etc
nextSoundToPlay = [arrayOfSounds objectAtIndex:nextIndexToPlayInArrayOfSounds ];
[nextSoundToPlay playAtTime:0];
关于iOS 在完成播放前播放相同的音效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23467579/
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
让多条路线去同一条路的最优雅的方式是什么ControllerAction?我有:get'dashboard',to:'dashboard#index'get'dashboard/pending',to:'dashboard#index'get'dashboard/live',to:'dashboard#index'get'dashboard/sold',to:'dashboard#index'这很丑陋。有什么“更优雅”的建议吗?一个类轮的奖励积分。 最佳答案 为什么不只有一个路由和一个Controller操作,并根据传递给它的参数来
在我做的一些网络开发中,我有多个操作开始,比如对外部API的GET请求,我希望它们同时开始,因为一个不依赖另一个的结果。我希望事情能够在后台运行。我找到了concurrent-rubylibrary这似乎运作良好。通过将其混合到您创建的类中,该类的方法具有在后台线程上运行的异步版本。这导致我编写如下代码,其中FirstAsyncWorker和SecondAsyncWorker是我编写的类,我在其中混合了Concurrent::Async模块,并编写了一个名为“work”的方法来发送HTTP请求:defindexop1_result=FirstAsyncWorker.new.async.
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
在我的路线文件中我有:match'graphs/(:id(/:action))'=>'graphs#(:action)'如果是GET请求(工作)或POST请求(不工作),我想匹配它我知道我可以使用以下方法在资源中声明POST请求:post'/'=>:show,:on=>:member但是我怎样才能为比赛做到这一点呢?谢谢。 最佳答案 如果你同时想要POST和GETmatch'graphs/(:id(/:action))'=>'graphs#(:action)',:via=>[:get,:post]编辑默认值可以设置如下match'g
一边学习thisRailscast我从Rack中看到了以下源代码:defself.middleware@middleware||=beginm=Hash.new{|h,k|h[k]=[]}m["deployment"].concat[[Rack::ContentLength],[Rack::Chunked],logging_middleware]m["development"].concatm["deployment"]+[[Rack::ShowExceptions],[Rack::Lint]]mendend我的问题是关于第三行。什么是传递block{|h,k|h[k]=[]}到Has
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:HowdoIgeneratealistofnuniquerandomnumbersinRuby?我想做的事:Random.rand(0..10).timesdoputsRandom.rand(0..10)end但如果随机数已经显示过,则无法再次显示。如何最轻松地做到这一点?