我有 five.caf 文件(具有不同音乐家的声音),我想同时播放多个文件。为此,我使用了 AVAudioPlayer 类。我创建了五个 AVAudioPlayer 实例并设置了 url 并播放了歌曲。但它没有正确同步。看起来一两个音乐家的声音延迟了一秒钟。 如何同时同步所有歌曲并播放它们。
下面是我的代码:
// Song1
NSString *FilePath = [NSString stringWithFormat:@"%@/songs/%@", [[GlobalClass sharedInstance] GetDocPath], SongFileName];
NSString *url = [FilePath stringByAppendingString:@"/Song1.caf"];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:url] error:nil];
[player setDelegate:self];
[player prepareToPlay];
player.numberOfLoops = 0;
// Song2
NSString *url1 = [FilePath stringByAppendingString:@"/Song2.caf"];
player1 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:url1] error:nil];
[player1 setDelegate:self];
[player1 prepareToPlay];
player1.numberOfLoops = 0;
// Song3
NSString *url2 = [FilePath stringByAppendingString:@"/Song3.caf"];
player2 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:url2] error:nil];
[player2 setDelegate:self];
[player2 prepareToPlay];
player2.numberOfLoops = 0;
// Song4
NSString *url3 = [FilePath stringByAppendingString:@"/Song4.caf"]; // ALT.caf
player3 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:url3] error:nil];
[player3 setDelegate:self];
[player3 prepareToPlay];
player3.numberOfLoops = 0;
NSLog(@"player3: %f", player3.duration);
// Song5
NSString *url4 = [FilePath stringByAppendingString:@"/Song5.caf"];
player4 = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:url4] error:nil];
[player4 setDelegate:self];
[player4 prepareToPlay];
player4.numberOfLoops = 0;
[player play]; // Band
[player1 play]; // Song1
[player2 play]; // Song2
[player3 play]; // Song3
[player4 play]; // Song4
最佳答案
制作并发调度队列并播放调度队列并发分支中的文件
dispatch_queue_t parallelQueue = dispatch_queue_create("com.unique.name.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(parallelQueue, ^{
[player play];
});
dispatch_async(parallelQueue, ^{
[player1 play];
});
dispatch_async(parallelQueue, ^{
[player2 play];
});
dispatch_async(parallelQueue, ^{
[player3 play];
});
dispatch_async(parallelQueue, ^{
[player4 play];
});
希望这对您有所帮助。
关于ios - 同时播放多首歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41235312/
这里有一个很好的答案解释了如何在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使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
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上
在previousquestion中我想出了如何在多个服务器上启动经过密码验证的sshsession来运行单个命令。现在我需要能够执行“sudo”命令。问题是,net-ssh-multi没有分配sudo需要运行的伪终端(pty),导致以下错误:[127.0.0.1:stderr]sudo:sorry,youmusthaveattytorunsudo根据documentation,可以通过调用channel对象的方法来分配伪终端,但是,以下代码不起作用:它会生成上面的“notty”错误:require'net/ssh'require'net/ssh/multi'Net::SSH::Mul
我一直在寻找一种以编程方式或通过命令行将mp3转换为aac的方法,但没有成功。理想情况下,我有一段代码可以从我的Rails应用程序中调用,将mp3转换为aac。我安装了ffmpeg和libfaac,并能够使用以下命令创建aac文件:ffmpeg-itest.mp3-acodeclibfaac-ab163840dest.aac当我将输出文件的名称更改为dest.m4a时,它无法在iTunes中播放。谢谢! 最佳答案 FFmpeg提供AAC编码功能(如果您已编译它们)。如果您使用的是Windows,则可以从here获取完整的二进制文件。
我如何用ruby编写一个脚本,当从命令行执行时播放mp3文件(背景音乐)?我试过了run="mplayer#{"/Users/bhushan/resume/m.mp3"}-aosdl-vox11-framedrop-cache16384-cache-min20/100"system(run)但它也不起作用,以上是播放器特定的。如果用户没有安装mplayer怎么办。有没有更好的办法? 最佳答案 我一般都是这样pid=fork{exec'mpg123','-q',file} 关于ruby
当我将IO::popen与不存在的命令一起使用时,我在屏幕上打印了一条错误消息:irb>IO.popen"fakefake"#=>#irb>(irb):1:commandnotfound:fakefake有什么方法可以捕获此错误,以便我可以在脚本中进行检查? 最佳答案 是:升级到ruby1.9。如果您在1.9中运行它,则会引发Errno::ENOENT,您将能够拯救它。(编辑)这是在1.8中的一种hackish方式:error=IO.pipe$stderr.reopenerror[1]pipe=IO.popen'qwe'#
当我尝试使用“套接字”库中的方法“read_nonblock”时出现以下错误IO::EAGAINWaitReadable:Resourcetemporarilyunavailable-readwouldblock但是当我通过终端上的IRB尝试时它工作正常如何让它读取缓冲区? 最佳答案 IgetthefollowingerrorwhenItrytousethemethod"read_nonblock"fromthe"socket"library当缓冲区中的数据未准备好时,这是预期的行为。由于异常IO::EAGAINWaitReadab
是否可以同时传递GET和POST参数?uri=URI.parse("http://www.example.com/post.php?a=1&b=2")req=Net::HTTP::Post.new(uri.path,{'Referer'=>"http://www.example.com/referer",'User-Agent'=>"Mozilla/4.0(compatible;MSIE7.0;WindowsNT5.1)",'Cookie'=>$cookie})req.set_form_data({'foo'=>'bar','bar'=>'foo'})http=Net::HTTP.ne