- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试按特定顺序播放声音。我设法做到了,但声音之间有微小的延迟。以彼此适合的方式录制的声音,因此用户将不会注意到音乐已经改变。
最重要的是,我必须避免的声音之间存在延迟..
我不会把所有的声音都集中到一个长的声音中,因为我需要在不同的时间开始不同的声音。
也许有比使用 AVAudioPlayer 更好的方法?
这是我的代码:
-(id)init {
self = [super init];
if (self) {
gameMusicSounds = @[gamePlaySound1,gamePlaySound2,gamePlaySound3,gamePlaySound4,gamePlaySound5,gamePlaySound6,gamePlaySound7,gamePlaySound8,gamePlaySound9,gamePlaySound10];
}
return self;
}
-(void)gameMusicNew {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
currentMusicToPlay = [[defaults objectForKey:CURRENT_GAME_MUSIC] intValue];
if (currentMusicToPlay >= [gameMusicSounds count]){
currentMusicToPlay = 0;
}
NSString * music = [[NSBundle mainBundle] pathForResource:[gameMusicSounds objectAtIndex:currentMusicToPlay] ofType:@"mp3"];
self.backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:music] error:NULL];
self.backgroundMusic.numberOfLoops = 1;
self.backgroundMusic.delegate = self;
[self.backgroundMusic play];
currentMusicToPlay ++;
[defaults setObject:[NSString stringWithFormat:@"%i",currentMusicToPlay] forKey:CURRENT_GAME_MUSIC];
[defaults synchronize];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[self gameMusicNew];
}
----更新-----
我想与大家分享我写的最终代码,因为我实际上并没有在网上找到很多关于这个主题的东西。
基本上我有一组声音需要一个一个地播放,当它们播放完后,我需要重新开始。我不使用一个长的 mp3 文件的原因是,有时,如果用户返回应用程序并且他正在玩游戏,我希望他不要听他上次听的音乐。
代码如下:
- (id)init {
self = [super init];
if (self) {
gameMusicSounds = @[gamePlaySound1,gamePlaySound2,gamePlaySound3,gamePlaySound4,gamePlaySound5,gamePlaySound6,gamePlaySound7,gamePlaySound8,gamePlaySound9,gamePlaySound10];
}
return self;
}
-(void)gameMusicNew{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString * isGameOver =[defaults objectForKey:IS_GAME_OVER];
currentMusic = [[defaults objectForKey:CURRENT_GAME_MUSIC] intValue];
if([isGameOver isEqualToString:@"YES"] || currentMusic >= [gameMusicSounds count]){
currentMusic = 0;
[defaults setObject:[NSString stringWithFormat:@"%i",currentMusic] forKey:CURRENT_GAME_MUSIC];
[defaults synchronize];
}
NSMutableArray* songs = [NSMutableArray arrayWithCapacity:[gameMusicSounds count]];
NSURL* url = [[NSBundle mainBundle] URLForResource:[gameMusicSounds objectAtIndex:currentMusic] withExtension:@"mp3"];
AVPlayerItem* item = [[AVPlayerItem alloc] initWithURL:url];
[songs addObject:item];
self.queue = [AVQueuePlayer queuePlayerWithItems:songs];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[self.queue currentItem]];
[self.queue play];
[self addNextMusic];
}
-(void)addNextMusic{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
currentMusic = [[defaults objectForKey:CURRENT_GAME_MUSIC] intValue];
int nextMusic = currentMusic + 1;
if(nextMusic >= [gameMusicSounds count]){
nextMusic = 0;
}
NSURL* url = [[NSBundle mainBundle] URLForResource:[gameMusicSounds objectAtIndex:nextMusic] withExtension:@"mp3"];
AVPlayerItem* item = [[AVPlayerItem alloc] initWithURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:item];
[self.queue insertItem:item afterItem:nil];
}
- (void)playerItemDidReachEnd:(NSNotification *)notification {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
currentMusic = [[defaults objectForKey:CURRENT_GAME_MUSIC] intValue];
currentMusic++;
if(currentMusic >= [gameMusicSounds count]){
currentMusic = 0;
}
[defaults setObject:[NSString stringWithFormat:@"%i",currentMusic] forKey:CURRENT_GAME_MUSIC];
[defaults synchronize];
[self addNextMusic];
}
-(void)stopGameMusic{
[self.queue removeAllItems];
}
希望它对某人有用。
最佳答案
您可以使用 AVQueuePlayer 来做到这一点。它接受多种声音并按顺序播放。
要使用它,请创建一个 AVPlayerItem 对象数组(使用 initWithURL:
创建指向您的声音的每个 AVPlayerItem),然后只需添加该数组并播放即可。
这是我的一个应用程序的实际代码。它完全符合您的描述,就好像所有的“歌曲”都是一首长长的“歌曲”:
NSMutableArray* songs = [NSMutableArray arrayWithCapacity:self.tiles.count];
for (NSString* fnam in self.songsSorted) {
NSURL* url = [[NSBundle mainBundle] URLForResource:fnam withExtension:@"m4a" subdirectory:@"Songs"];
AVPlayerItem* item = [[AVPlayerItem alloc] initWithURL:url];
[songs addObject:item];
}
self.queue = [AVQueuePlayer queuePlayerWithItems:songs];
[self.queue play];
关于ios - 在 iOS 上没有任何延迟地一个接一个地播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22538976/
是否可以在无需用户点击或鼠标悬停的情况下播放声音文件? 我有一个记分牌,我想在球队得分时播放声音文件。任何指示将不胜感激。我基本上完成了记分牌,但没有声音。 谢谢。 最佳答案 https://gith
我正在创建一个音频应用程序,其中有两个名为 录制音频 浏览音频 当用户单击第一个按钮时,他可以录制音频。这已经实现。 当用户单击第二个按钮时,他可以浏览以查找iPhone库中已经存在的音频/声音。我对
香港专业教育学院一直在使用SoX来将文件修剪为恰好2秒长,但是我注意到音频文件最后总是额外多了32毫秒左右,显然它的额外数据是要告知其他解码器其信息,但是否必须添加放在文件的长度上? 我创建了一个程序
我将使用代码来获取设备的默认音量/声音,该默认音量/声音是使用设备上的音量调高或调低按钮设置的,下面是我要访问声音的代码, 为了解决此错误,我已经进行了研究,发现要访问此代码,我们需要使用CoreAu
我有解码 MP3 并用所有“值”填充数组的代码。 我的问题是:这些值(value)观是什么?它们是频率吗?它们是振幅吗? 这是代码: File file = new File(song.getFile
哈乌乌,我正在尝试实现 Pong。 现在我想播放声音,但它抛出异常(UnsupportedAudioFileException)。我做错了什么? AudioInputStream ainBalk;
我在大学的一个兄弟会中,在这个兄弟会中我们有楼梯。时不时有人从那些楼梯上掉下来。我们通常从吧台后面的电脑播放音乐(通常来自互联网或 iTunes)。我有一个 usb 按钮,想编写一个程序,当有人从楼梯
我想检测来自用户语音的声音/噪音,如果语音输入为空,它会自动停止。 为应用程序点赞 talking tom cat当有声音/语音输入时它会自动开始收听,当没有声音/语音输入时会自动停止。 任何帮助都将
我正在使用 jQuery Sound Plugin在我的网站上创建一些声音效果,但我无法播放。我收到此消息: settings.events.error(null, {msg: "You have n
我有一段代码可以在我点击一个按钮后播放声音。当我第二次单击此按钮时,首先会出现重置之类的东西。 我想要的是:每次单击按钮时我都想立即播放声音而无需重置按钮。 我的代码: -(IBAction)play
我在android studio中制作了一个闹钟。我可以运行该应用程序,除了播放闹钟铃声外,其他一切正常。实际上,当闹钟时间到来时,没有声音播放。我不知道我的代码有什么问题。请帮我找出错误。 主要 A
有什么方法可以在关闭声音的情况下播放 UILocalNotification 声音。实际上,我正在尝试创建一个闹钟,即使用户关闭了声音也能正常工作。或实现此目的的任何替代方法。 最佳答案 如果用户关闭
我试图从字符串创建音频,我试图举一个例子,用户输入他们的名字,然后将其转换为声音/音频 - 声音/音频会根据输入的字符串而有所不同。 (我不想在字符串上执行“文本到语音”,只是创建由字符串生成的声音,
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
对大量二进制文件(例如音频和视频文件)进行版本控制的最佳方法是什么? Git 似乎并不是真正为处理大量二进制文件而设计的。 另一个问题是内容制作者不一定想学习如何使用像 Git 这样的开发人员工具。
我想让一个 python 程序在它完成任务时发出哔声来提醒我。目前,我使用 import os 然后使用命令行语音程序说“处理完成”。我宁愿它是一个简单的“铃铛”。 我知道 Cocoa 应用程序中可以
请原谅这个愚蠢的新手问题,但是:当我(不小心)在命令行窗口中按退格键时,如何关闭 MATLAB 发出的极其烦人的“哔”声? 最佳答案 只是beep off在最新版本中。 https://www.mat
如何找出用户在控制面板中配置了哪些声音文件? 示例:我想播放“设备已连接”的声音。 哪个API可用于查询控制面板声音设置? 我看到控制面板对话框中有一些由第三方程序创建的自定义条目,因此必须有一种方法
我对实现与此人 link 类似的处理方式感兴趣。 据我了解,她将一段视频切成 tiff 格式,然后使用 RiTa 库进行合成 有谁知道如何实现这样的事情,只是改变我正在使用其他扩展名或文件格式的事实。
使用 C#,我试图捕获 PC 正在播放的音频,而不使用 WASAPI 和环回,因为我的声卡似乎不支持它。 TeamViewer 之类的程序是如何做到的?当我使用它时,人们可以从我的 PC 听到音频。
我是一名优秀的程序员,十分优秀!