流式音频预载入示例-Streaming audio in iPhone Unity (using obj-c) source code
这个小教程是针对iphone进行开发,使用语言是obj-c,有项目源文件供下载!
Isn’t it annoying that you have to pre-load your entire music track into memory before you can play it in Unity iPhone? When you have music files at 3meg or larger, it’s a huge amount of memory to use up when you are so limited.
Since Unity iPhone doesn’t yet have streaming audio capability, I put something together to take care of it. Right now, it’s a pretty basic system .. but it plays, stops and pauses.
For some reason, some people have reported a pulsing problem when the cpu is being heavily loaded – the game will stall a little. I don’t know that causes this, so if you solve it please do let me know so I can post the solution for everyone. Thanks!
If you use this code and do anything cool with it, all I ask is that you share it with the Unity community.
DOWNLOAD SOURCE HERE!
To get going, you should just need a few lines in your AppController.mm file:
1. Right at the top of your AppController.mm, add this line:
#import “MusicController.h”
2. Scroll down or use the search to find the function:
And within that function, before anything else happens there add the line:
[MusicController dealloc];
4. Add a reference to AVFoundation.framework … in xcode, go to the Project menu. Select ‘Add to Project’. Browse through the folders:
SYSTEM -> Developer -> Platforms -> iPhoneOS.platform -> Developer -> SDKs -> iPhoneOS3.0.sdk -> System -> Library -> Frameworks
Finally, once you made your way through all that, select AVFoundation.framework.
5. Add calls to your game code in Unity:
To play a track (NOTE: Right now this is set up to ONLY use mp3s. If you want to change this, search for mp3 in the MusicController.m file and change it to your chosen file format.)
PlayerPrefs.SetString(“
_
music
_
filename”, );
theFilename is your audio file name (with no extension) and this one pref will start the music playing.
IMPORTANT: Don’t include the file extension in your filename here, just the filename alone. The obj-c will add the .mp3 for you. Also, do not include a path. Just the filename is enough, as it all gets flattened down when you build.
I put all my audio in a folder called ‘MP3′ in the root of my xcode project, so feel free to place them in a subfolder, but don’t forget to add the music files to your project by going to the Project menu / Add to Project and select your folder of audio files.
To stop the track:
PlayerPrefs.SetInt(“
_
stop
_
music”,1);
To pause the track:
PlayerPrefs.SetInt(“
_
pause
_
music”,1);
To play after a pause:
PlayerPrefs.SetInt(“
_
play
_
music”,1);
One more thing … search MusicController.m for ‘AVAudioSessionCategoryPlayback’ to change the way the audio player works. There are a few different methods. The following list is taken from the Apple developer site. It’s up to you to pick the right one to suit your application
AVAudioSessionCategoryAmbient
For an application in which sound playback is nonprimary—your application can be used successfully with the sound turned off. This category is also appropriate for “play along” style applications, such as a virtual piano that a user plays over iPod audio. When you use this category, other audio, such as from the iPod application, mixes with your audio. Your audio is silenced by screen locking and by the Ring/Silent switch.
Available in iPhone OS 3.0 and later.
Declared in
AVAudioSession.h
.
AVAudioSessionCategorySoloAmbient
The default category; used unless you set a category with the
setCategory:error:
method. This category silences audio from other applications, such as the iPod. Your audio is silenced by screen locking and by the Ring/Silent switch.
Available in iPhone OS 3.0 and later.
Declared in
AVAudioSession.h
.
AVAudioSessionCategoryPlayback
For playing recorded music or other sounds that are central to the successful use of your application. This category silences audio from other applications, such as the iPod. You can, however, modify this category to allow mixing by using the
kAudioSessionProperty
_
OverrideCategoryMixWithOthers
property. Your audio continues with the Ring/Silent switch set to silent and with the screen locked.
Available in iPhone OS 3.0 and later.
Declared in
AVAudioSession.h
.
AVAudioSessionCategoryRecord
For recording audio; this category silences playback audio. Recording continues with the screen locked.
Available in iPhone OS 3.0 and later.
Declared in
AVAudioSession.h
.
AVAudioSessionCategoryPlayAndRecord
For recording and playback of audio—simultaneous or not—such as for a VOIP (voice over IP) application. This category silences audio from other applications, such as the iPod. You can, however, modify this category to allow mixing by using the
kAudioSessionProperty
_
OverrideCategoryMixWithOthers
property. Your audio continues with the Ring/Silent switch set to silent and with the screen locked.
Available in iPhone OS 3.0 and later.
Declared in
AVAudioSession.h
.
AVAudioSessionCategoryAudioProcessing
For using an audio hardware codec or signal processor while not playing or recording audio. Use this category, for example, when performing offline audio format conversion.