如何添加背景音乐我spritekit文件 [英] How do I add background music to my spritekit file
本文介绍了如何添加背景音乐我spritekit文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能有人给我一个快速简单的一步一步地添加背景音乐M4A,一旦我的应用程序加载。这是一个精灵套件X code文件,而音乐是M4A格式。谢谢
Could someone give me a quick easy step by step to adding background m4a music once my app has loaded. It is a sprite kit Xcode file, and the music is in m4a format. Thanks
推荐答案
您可以使用 AVAudioPlayer
为此:
在您的.H:
#import <AVFoundation/AVFoundation.h>
和以下内容添加到接口
AVAudioPlayer *player;
在.M,初始化播放器,音频誓言网址:
In .m, initialize player with audio oath url:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"bg_music"
ofType:@"mp3"]];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
player.numberOfLoops = -1;
当你需要播放音频,您可以拨打:
and when you need to play the audio, you can call:
[player play];
注意:
numberOfLoops是声音会在达到结束返回到开始的次数。
Note: "numberOfLoops" is the number of times that the sound will return to the beginning upon reaching the end.
- 的值为零表示以播放声音一次。
- 之一的值会导致在播放声音的两倍,依此类推......
- 任何负数将无限循环播放,直到停止。
请编码................:)
Keep Coding................ :)
这篇关于如何添加背景音乐我spritekit文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文