Question

I want to use the playSoundFileNamed: Method to play a background soundfile as loop, which I copied into the project as usual, but the problem is, that the program stops at the sound code line...

SKAction* playSound = [SKAction playSoundFileNamed:@"backgroundMusicLoop.m4a" waitForCompletion:NO];
[self runAction: [SKAction repeatActionForever:playSound]];

It doesn't happen anything (and I tried it not in the simulator, but on a real device) ! I tried to play a wav file and there was no error, but wavs have a lot bigger filesize...

Hope someone knows about a solution :)

Was it helpful?

Solution

UPDATED

Change your waitForCompletion from NO to YES.

Having it set to NO, means that the action will always repeat itself without waiting for the sound to play to end. In effect it gets stuck at the beginning and has no chance to play through.

(Brain fart on my part for not seeing that the first time around!)

ORIGINAL

The m4a sound format seems to have issues with the playSoundFileNamed:waitForCompletion:. Try another format such as mp3 or caf.

If you absolutely need to use m4a then use AVAudioSession to play your sounds, music.

In case you don't know how to convert formats using iTunes, here are Apple's instructions for it:

Saving a copy of a song in a new file format When converting from a compressed to uncompressed file format (for example, from MP3 to AIFF) you shouldn't notice any reduction in sound quality. However, when converting between compressed formats (for example MP3 and AAC), you may notice a reduction in the sound quality. For the best results, if you want your music encoded in a different file format, you should import the music again from the original source using the new encoding format.

To convert a song's file format Open iTunes Preferences. Windows: Choose Edit > Preferences. Mac: Choose iTunes > Preferences. Click the General button, then click the Importing Settings… button in the lower section of the window. From the Import Using pop-up menu, choose the encoding format that you want to convert the song to, then click OK to save the settings. Select one or more songs in your library, then from the File > Create New Version menu, choose one of the following (the menu item changes to show what's selected in your Importing preferences): Create MP3 version Create AAC version Create AIFF version Create WAV version Create Apple Lossless version If you haven't imported some songs into iTunes yet, you can import and convert them at the same time. This will create a converted copy of the file in your iTunes Library based on your iTunes preferences. To convert all the songs in a folder or on a disk, hold down the Option key (Mac) or Shift key (Windows) and choose File > Create New Version

Convert [import preference setting]. The Import preference setting will match what you chose in step 3. iTunes will prompt you for the location of the folder or disk you want to import and convert. All the songs in the folder or on the disk will be converted. Note: Older purchased songs are encoded using a Protected AAC format that prevents them from being converted. If you need to convert these to another format, follow the instructions in this article to upgrade them.

The song in its original format and the newly converted song appear in your library.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top