Swift SpriteKit在后台播放音频 [英] Swift SpriteKit playing audio in the background

查看:172
本文介绍了Swift SpriteKit在后台播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要在后台播放音频的应用程序...
这是否可以使用Swift和SpriteKit与SKActions ...
或者它可能是另一种方式..

I have an app which needs to play audio in the background... Is this possible using Swift and SpriteKit with SKActions... Or is it possible another way..

向正确的方向微调会非常有帮助。

A nudge in the right direction would be very helpful.

推荐答案

SKAction 非常容易使用声音,但有时你可能想要做更多。

SKAction is really easy to use with sounds, but sometimes you might want to do more.

在这种情况下,你会想要使用 AVAudioPlayer 代替它。

In that case, you would want to use AVAudioPlayerinstead of it.

为了不写自己的播放器,我建议你使用现有的。这是我已经使用的(SKTAudio): https:// github .com / raywenderlich / SKTUtils / blob / master / SKTUtils / SKTAudio.swift

In order to not write your own "player", I suggest you to use an existing one. Here is one I've already used (SKTAudio) : https://github.com/raywenderlich/SKTUtils/blob/master/SKTUtils/SKTAudio.swift

以下是如何使用它:

// For background audio (playing continuously)
SKTAudio.sharedInstance().playBackgroundMusic("yourBackgroundMusic.mp3") // Start the music
SKTAudio.sharedInstance().pauseBackgroundMusic() // Pause the music
SKTAudio.sharedInstance().resumeBackgroundMusic() // Resume the music

// For short sounds
SKTAudio.sharedInstance().playSoundEffect("sound.wav") // Play the sound once

正如你所看到的,你可以播放短音(就像你可能已经用 SKAction 那样),甚至可以播放循环播放的背景音乐。正在寻找。

As you can see, you'll be able to either play short sound (as you might already have done with SKAction) and even background music that will play in loop as you're looking for.

这篇关于Swift SpriteKit在后台播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆