xcode - 如何将背景音乐添加到我的 spritekit 文件中

标签 xcode audio sprite

有人可以给我一个快速简单的步骤,以便在我的应用程序加载后添加背景 m4a 音乐。它是一个 Sprite 套件Xcode文件,音乐是m4a格式。谢谢

最佳答案

试试这个:

@import AVFoundation;

...

AVAudioPlayer * backgroundMusicPlayer;
NSError *error;
NSURL * backgroundMusicURL = [[NSBundle mainBundle] URLForResource:@"song" withExtension:@"m4a"];
backgroundMusicPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:backgroundMusicURL error:&error];
backgroundMusicPlayer.numberOfLoops = -1; //-1 = infinite loop
[backgroundMusicPlayer prepareToPlay];
[backgroundMusicPlayer play];

并简单地停止

[backgroundMusicPlayer stop];

注意:我不使用SKAction来播放背景音乐,因为你想停止的时候都无法停止

关于xcode - 如何将背景音乐添加到我的 spritekit 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21947117/

相关文章:

ios - 测试飞行构建显示无法连接到服务器

ios - UIPageViewController 和删除当前 View Controller

audio - 在AS2中循环播放声音

android - 使用 FFMPEG 仅更改其中一个音频 channel 的音量

swift - 如何在 SpriteKit swift 中将 NSTimer 作为参数传递给函数

python - 在 Pygame 中每隔几秒移动一个对象

python - Pygame - 让 Sprite 朝它所面对的方向移动

ios - UITableView 崩溃无法识别的选择器发送到实例

ios - 错误构建 : Undefined symbol: _SKStoreProductParameterAdNetworkSourceIdentifier

iOS闹钟,保持闹铃播放