流式音频预载入示例-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.
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:
- (void) applicationDidFinishLaunching:(UIApplication*)application
In that function, after the line:
[self startUnity:application];
Add this line:
[[MusicController alloc] init];
3. Scroll down or find the function:
- (void) dealloc
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 ![]()
AVAudioSessionCategoryAmbientAvailable in iPhone OS 3.0 and later.
Declared in AVAudioSession.h.
AVAudioSessionCategorySoloAmbientsetCategory: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.
AVAudioSessionCategoryPlaybackkAudioSessionProperty_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.
AVAudioSessionCategoryRecordAvailable in iPhone OS 3.0 and later.
Declared in AVAudioSession.h.
AVAudioSessionCategoryPlayAndRecordkAudioSessionProperty_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.
AVAudioSessionCategoryAudioProcessingAvailable in iPhone OS 3.1 and later.
Declared in AVAudioSession.h.
Annnd I think that’s everything.
Good luck!
转自:http://psychicparrotgames.com/?p=33

