iOS (Swift) App Fails to Load 251st Sound Resource - ios

After loading two or more audio files 250 times, the app fails to load further files. This appears to happen regardless of the length of the audio files or the duration between consecutive plays.
Error:
SKAction: Error loading sound resource: "audio/1.mp3"
Code (GameScene.swift):
import GameplayKit
class GameScene: SKScene {
var count = 0
override func didMove(to view: SKView) {
infiniteLoop()
}
func infiniteLoop() {
let number = count % 3 + 1
let playLetter = SKAction.playSoundFileNamed("audio/\(number).mp3", waitForCompletion: true)
self.run(playLetter, completion: {
self.count += 1
print(self.count)
self.infiniteLoop()
})
}
}
Audio Files (example):
http://s000.tinyupload.com/index.php?file_id=87262242252758332431
Reproducing The Error:
The error appears to be reproducible by simply making a new iOS game in Xcode, copying the "audio" (assets) folder linked above into the root of the project, and copying the code above into the GameScene.swift file. After getting to "250" in the console the error will likely appear.
Analysis:
After cycling through two or more audio files 250 times, the app fails to load the audio resource. I encountered this problem while making a spelling game which cycles through images and audio files in a similar manner. The problem originally presented itself by failing to load both the image files and the audio files after the app being used for ~15 minutes. I've managed to strip the app down to this core code and still trigger the error regardless of whether I simulate on my computer, simulate on my iPad, or even export and install (Ad Hoc) on my iPad.
I'm new to Swift and am hoping there is something simple at play here. Please let me know if you have any suggestions on how to fix this error.

Turns out the answer is to use AVAudioPlayer instead of SKAction for playing the audio file if it is to be played a large number of times.

Related

Unity/Videoplayer - Game sometimes crash if I load next video

I use Video Player which attached 3D game object. This game object contain "Mesh Renderer" component too. Video Player Renderer-> this game object.
I try many method, but always crash game.
I try use in Update: Videoplayer().isplaying if false, then I load from Resources next video and play, but not working. (Unity Editor working fine, but in iPhone sometimes crash game.)
Then I try other method..
But sometimes crash too..
Okey, I think maybe loading and start playing too fast, maybe this cause the crash.
Then I try use Ienumerator waitforsecond every step, for debugging I set it 5 seconds.
I try first set video player.clip = null, maybe this is the problem.. But not..
Now the code:
void Start()
{
index = 0;
videoPlayer = gameObject.GetComponent<VideoPlayer>();
videoPlayer.loopPointReached += CheckOver;
}
IEnumerator Waitforseconds()
{
Debug.Log("Waitforseconds start");
videoPlayer.clip = null;
Debug.Log("Videoplayer clip null);
yield return new WaitForSeconds(5);
videoPlayer.clip = theVideo;
Debug.Log("Videoplayer set the video");
yield return new WaitForSeconds(5);
Debug.Log("Next play video player");
videoPlayer.Play();
Debug.Log("Play video player");
isPlaying = true;
}
void CheckOver(UnityEngine.Video.VideoPlayer vp)
{
Debug.Log("Checkover");
index += 1;
if (index >= videos.Length)
{
index = 0;
}
theVideo = videos[index];
Debug.Log("Set the video");
StartCoroutine(Waitforseconds());
}
The videos size 5-10 MB and 10-60 seconds.
The result: Unity Editor working good, I build to iOS and sometimes crash the game if I play next video (iPhone 12 Pro Max, supposedly the fastest smartphone chip, so if that doesn't work, there's a big problem..)
Debug:
I get last Debug.log before the game crash:
"Videoplayer clip null"
So I think the game crash here: videoPlayer.clip = theVideo;
But only iOS! Unity editor working good.
Xcode error:
I try find solution in google, but I can't find..
Maybe memory error? should I somehow delete the previous video from memory?
Or how can I solve this problem? Please help me.
Thank you very much!
EDIT: I see the problem is Audio. (Audio Output mode: Audio Source, this component attached to same gameobject. So same game object contain audio source and video player.) If I set Audio Output mode to "none", then game not crash, workin good. So the problem when "PrepareAudioTap". How can I solve this problem?
Solved!
I change Video Player component "Audio Output mode": Audio Source to Direct.
Then not crash game.
But I would like to use Audio Source, because I need 3D Sound. (not important, I'm glad it works, but if you have idea how to work with 3D sound, I would be glad.)
Thanks!

AudioKit export song pre iOS 11

Note that this is NOT a duplicate of this SO Post because in that post only WHAT method to use is given but there's no example on HOW should I use it.
So, I have dug into AKOfflineRenderNode as much as I can and have viewed all examples I could find. However, my code never seemed to work correctly on iOS 10.3.1 devices(and other iOS 10 versions), for the result is always silent. I try to follow examples provided in other SO posts but no success. I try to follow that in SongProcessor but it uses an older version of Swift and I can't even compile it. Trying SongProcessor's way to use AKOfflineRenderNode didn't help either. It always turned out silent.
I created a demo project just to test this. Because I don't own the audio file I used to test with, I couldn't upload it to my GitHub. Please add an audio file named "Test" into the project before compiling onto an iOS 10.3.1 simulator. (And if your file isn't in m4a, remember to change the file type in code where I initialize AKPlayer)
If you don't want to download and run the sample, the essential part is here:
#IBAction func export() {
// url, player, offlineRenderer and others are predefined and connected as player >> aPitchShifter >> offlineRenderer
// AudioKit.output is already offlineRenderer
offlineRenderer.internalRenderEnabled = false
try! AudioKit.start()
// I also tried using AKAudioPlayer instead of AKPlayer
// Also tried getting time in these ways:
// AVAudioTime.secondsToAudioTime(hostTime: 0, time: 0)
// player.audioTime(at: 0)
// And for hostTime I've tried 0 as well as mach_absolute_time()
// None worked
let time = AVAudioTime(sampleTime: 0, atRate: offlineRenderer.avAudioNode.inputFormat(forBus: 0).sampleRate)
player.play(at: time)
try! offlineRenderer.renderToURL(url, duration: player.duration)
player.stop()
player.disconnectOutput()
offlineRenderer.internalRenderEnabled = true
try? AudioKit.stop()
}

Suddenly cannot read files anymore

I'm developing a tiny game and at first it works super perfect. It loads sks files and sound files pretty well. But after a few minutes of testing, I start to get "Error loading sound resource" and particle effects all disappear.
I thought it might be the memory problem, however, the game only takes about 120M, and I haven't received any memory warnings yet.
So how can it manages to read files in the beginning and fails to do so a few minutes later? Is there something tricky in SpriteKit that I don't know? Such as after loading one hundred files successfully, the game cannot read any files anymore until the user shakes the iPhone twice and turns it upside down... If it's true, I will possible inform the players to do so in the game's tutorial...
So can anyone help me handle this...? Lots of thanks...
// The way I load background music:
let musicPath=Bundle.main.path(forResource: "Main", ofType: "mp3")
let url = URL(fileURLWithPath: musicPath!)
do{
try main=AVAudioPlayer(contentsOf: url)
}catch{
return
}
// The way I load sound files:
let a = SKAction.playSoundFileNamed("Fire",waitForCompletion:false)
audioNode.run(a)
// The way I load SKS files:
let emitterPath = Bundle.main.path(forResource: "Fire", ofType: "sks")!
let emitter = NSKeyedUnarchiver.unarchiveObject(withFile: emitterPath) as! SKEmitterNode
self.addChild(emitter)
It's hard to say without seeing your code but if you're opening a lot of file readers you could run out of memory or make the files inaccessible until the other nodes reading from the files are gone.
Do you happen to know if you're creating new audio nodes every time they are rendered?
Apples documentation suggests that you create a single SKAction to be reused. So if you need to reuse your Fire action, I would create a singleton of it and run that single object over and over instead of allocating the new SKAction again and again.
Source

Sound Effect Won't Play (XCode/Swift)

So I am following through an old tutorial and I think with the changes in xcode and swift this code is now no longer usable, but i am not sure. Would love some help.
Declaring constant for the audio effect
let cannonSound = SKAction.playSoundFileNamed("cannon.wav", waitForCompletion: false)
Calling the audio effect within my funtion
let hotdogSequence = SKAction.sequence([cannonSound, moveHotdog, deleteHotdog])
hotdog.run(hotdogSequence)
For more info I am using SpriteKit in Xcode and this code is contained within GameScene.swift file.
Update:
The error I receive is
2017-03-30 00:52:43.631 Ballpark Weiner[95999:1983181] SKAction: Error loading sound resource: "cannon.wav"
The game doesn't crash just no sound plays
This message usually means that the file cannot be found in your project or might be corrupt.
You should check that the file is actually copied into your project and is spelled correctly. Its case sensitive so if the actual file is called "Cannon.wav" it will not work.
Hope this helps

iOS: How to play audio without fps drops?

I am in the process of developing a game for iOS 9+ using Sprite Kit and preferably using Swift libraries.
Currently I'm using a Singleton where I preload my audio files, each connected to a separate instance of AVAudioPlayer.
Here's a short code-snipped to get the idea:
import SpriteKit
import AudioToolbox
import AVFoundation
class AudioEngine {
static let sharedInstance = AudioEngine()
internal var sfxPing: AVAudioPlayer
private init() {
self.sfxPing = AVAudioPlayer()
if let path = NSBundle.mainBundle().pathForResource("ping", ofType: "m4a") {
do {
let url = NSURL(fileURLWithPath:path)
sfxPing = try AVAudioPlayer(contentsOfURL: url)
sfxPing.prepareToPlay()
} catch {
print("ERROR: Can't load ping.m4a audio file.")
}
}
}
}
This Singleton is initialised during app start-up. In the game-loop I then just call the following line to play a specific audio file:
AudioEngine.sharedInstance.sfxPing.play()
This basically works, but I always get glitches when a file is played and the frame rate drops from 60.0 to 56.0 on my iPad Air.
Someone any idea how to fix this performance issue with AVAudioPlayer ?
I also watched out for 3rd party libraries, namely:
AudioKit [Looks very heavy-weighted]
ObjectAL [Last Update 2013 ...]
AVAudioEngine [Based on AVAudioPlayer, same problems ?]
Requirements:
Play a lot of very short samples (like shots, hits, etc..)
Play some motor effects (thus pitching would be nice)
Play some background / ambient sound in a loop
NO nasty glitches / frame rate drops !
Could you recommend any of the above mentioned libraries for my requirements or point out the problems using the above code ?
UPDATE:
Playing short sounds with:
self.runAction(SKAction.playSoundFileNamed("sfx.caf", waitForCompletion: false))
does indeed improve the frame rate. I exported the audio files with Audiacity to the .caf format (Apple's Core Audio Format). But in the tutorial, they export with "Signed 32-bit PCM" encoding which led to disturbed audio playback in my case. Using any of the other encoding options (32-bit float, U-Law, A-Law, etc..) worked fine for me.
Why using caf format? Because it's uncompressed and thus loaded faster into memory with less CPU overhead compared to compressed formats like m4a. For short sound effects played a lot in short intervals, this makes sense and disk usage is not affected much for short audio files consuming few kilobytes. For bigger audio files, like ambient and background music, using compressed formats (mp3, m4a) is obviously the better choice.
According to your question, if you develop a game for iOS 9+, you can use the new iOS 9 library SKAudioNode (official Apple doc):
var backgroundMusic: SKAudioNode!
For example you can add this to didMoveToView():
if let musicURL = NSBundle.mainBundle().URLForResource("music", withExtension: "m4a") {
backgroundMusic = SKAudioNode(URL: musicURL)
addChild(backgroundMusic)
}
You can also use to play a simple effect:
let beep = SKAudioNode(fileNamed: "beep.wav")
beep.autoplayLooped = false
self.addChild(beep)
Finally, if you want to change the volume:
beep.runAction(SKAction.changeVolumeTo(0.4, duration: 0))
Update:
I see you have update your question speaking about AVAudioPlayer and SKAction. I've tested both of them for my iOS8+ compatible games.
About AVAudioPlayer, I personally use a custom library maked by me based from the old SKTAudio.
I see your code, about AVAudioPlayer init, and my code is different because I use:
#available(iOS 7.0, *)
public init(contentsOfURL url: NSURL, fileTypeHint utiString: String?)
I don't know if fileTypeHint make the difference, so try and fill me about your test.
Advantages about your code:
With a shared instance audio manager based to AVAudioPlayer you can control volume, use your manager wherever you want, ensure compatibility with iOS8
Disadvantages about your code:
Everytime you play a sound and you want to play another sound, the previous is broken, especially if you have launch a background music.
How to solve? According with this SO post to work well without issues seems AVFoundation is limited to 4 AVAudioPlayer properties instantiables, so you can do this:
1) backgroundMusicPlayer: AVAudioPlayer!
2) soundEffectPlayer1: AVAudioPlayer!
3) soundEffectPlayer2: AVAudioPlayer!
4) soundEffectPlayer3: AVAudioPlayer!
You could build a method that switch through the 3 soundEffect to see if is occupied:
if player.playing
and use the next free player. With this workaround you have always your sound played correctly, even your background music.

Resources