Source for iphone streaming audio player

Since Unity iPhone still doesn’t have streaming audio capability, we are forced to pre-load audio into memory before we can play it. That’s bad. Especially considering how little memory we have to play with. Audio is always huge, which means 3-5meg of ram could be wasted just on a single song!

Also, it’s frustrating that you can’t play music as your game is loading. Not playing music through the loading process drops your production value a lot. Trying to play music in Unity, during a scene load, ends up with horrible cutting off.

That’s why I hacked… ahem… carefully crafted together a streaming audio system in xcode for Unity iPhone.

Hopefully I’m not missing anything in my little ‘how to get this going’ post! I’m pretty new to obj-c, though I’ve tested this a LOT and it appears to be all good - I take no responsibility if your project explodes, though, m’kay?

http://psychicparrotgames.com/blog/?p=33

Enjoy!

Please remember that if you expand this lib, share it back with the community … it’s the right thing to do :slight_smile:

Cheers!

Known issues:

Slight pulsing every half second or so when you load the cpu. Buffering perhaps? - anybody got any ideas on this?
No volume control. As an obj-c noob, I started adding it but I messed it up!

I think I might love you… will try this out in Doki Doki Bloki. If it works I’ll drop it in the 1.1 update!

Will get back to you :slight_smile:

Thank you, very kind of you to share this with the community.

Two questions.

1: Where do I put the MP3 files?

2: How do I select from multiple tracks?

Cheers!

Thanks,take a look,

Hey Moondog90210

You should be able to either dump them straight into your xcode project folder OR put a subfolder in the xcode project and dump them in there.

I have mine setup like this (the attachment image):

231283--8286--$folders_163.jpg

Multiple tracks …

Well, just before you call play you can set the filename to a new track, call stop and then when you call play again it will play the new track :slight_smile:

Hope this works out for you!

OMG thanks so much!

Wonderful stuff. I’ll get implementing it!

Just did a quick test and got a couple of issues.

I don’t think I’m calling the tracks the right way as it always plays the first MP3 I added. Is this right?

PlayerPrefs.SetString("_music_filename", "MP3/03.mp3");

I’m also getting a frame rate glitch around ever half second. I’m assuming this is because I’m not calling the music the right way with the above syntax.

Sorry to keep posting, I know you’re crunching right now! This code is a life saver though if I can get it working. Getting memory issues because of music.

Hmmm … try calling stop, then set filename, then play.

What worries me is the framerate glitch you’re mentioning … sounds like you are seeing a glitch every time the code checks for updates in those prefs. I hope it’s not something to do with the update loop and the way your unity game gets updated or something odd like that.

Since I don’t see the glitch myself, I’m not sure what it could be … I’ll try making a test bed here and see if I can repro it.

Hold off on this for a little bit while I go through it. I stripped this code out of my main code and it looks like I made a couple of mistakes :frowning:

I’ll post it up in a little bit … sorry about that!

will it play loops?

No, no loops … just single tracks right now.

It’s a very simple player.

On the plus side, I found a couple of bugs in this and I’ll get another version up in the next hour or so :slight_smile:

ok, but i guess it will be easy enough to loop it manually from unity anyway.
will it work with iphone basic?

Yeah, it’ll work with Basic :slight_smile:

you really really solved a giant problem for me with this.
i can’t stop saying thank you.

AWESOME! Well, I just found a couple of major bugs with it so don’t add it to your project just yet.

I’m trying to fix them right now and I should have a new version up this afternoon :slight_smile:

I just did a complete re-factor of my music controller to add in the option to use the streams. Once Psychicparrot has ironed out the bugs and I’ve had a chance to test my code works I’ll post it here.

Basically it’ll handle streamed and non-streamed (for in editor testing) music and should in theory handle looping music on the streams so long as you give it the length of the track in seconds. Completely untested as of now but the looper should also handle you pausing and un-pausing a track.

Once it’s all working I’ll add it here as a generic controller if anyone needs it.

Good luck with the bug tracking. I think a lot of people here are going to find this streamer very very useful!!

Sorry about this! I thought it was working OK and once you mentioned it, I looked at it a little more and realized just how messed up it is!

The bug is in the prefs code somewhere, in that it isn’t resetting the prefs once it checks them … so the file-name is getting checked over and over again and it’s causing the frame delay.

In the original version, it wasn’t resetting the track either when you set the filename.

I really messed it up lol - I will find a way to fix this and get it back up here soon.

Cheers!