纳金网

标题: 流式音频预载入示例 [打印本页]

作者: 会飞的鱼    时间: 2011-11-25 15:32
标题: 流式音频预载入示例


           流式音频预载入示例-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:
         

           - (void) applicationDidFinishLaunchingUIApplication*)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
         

           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.
           

           Available in iPhone OS 3.1 and later.
         

           Declared in
           

           AVAudioSession.h
           

           .
         

           Annnd I think that’s everything.
         

           Good luck!
         

           转自:http://psychicparrotgames.com/?p=33
         

作者: 奇    时间: 2012-1-30 23:24
新雪时舞蹈, 年夜思悄然。

作者: 奇    时间: 2012-2-26 23:18
跑着去顶朋友滴铁

作者: 奇    时间: 2012-4-2 23:23
跑着去顶朋友滴铁

作者: C.R.CAN    时间: 2012-5-29 23:27
有意思!学习了!

作者: C.R.CAN    时间: 2012-7-23 23:19
百度的叫度娘,网易的叫易娘,新浪内部还在为是叫新娘还是浪娘而争论不休!……不管你们是企鹅的额娘,豆瓣的伴娘,还是华为的伪娘,都要记得,淘宝才是你们的亲娘啊!亲!!

作者: 驰骋的风    时间: 2012-7-24 09:31
http://www.edy.es/unity/Offroader.html/
作者: 驰骋的风    时间: 2012-7-24 09:32
高手速来赐教!kismet里面怎么加一个延迟DELAY??



unity特效大家PP_武功盖世呀








【严重分享】Unity3D_3.0破解版[安装包][下载]






高手请赐教!用模型做的特效动画材质丢失








antares_universe(vizio)可视化编辑

作者: 菜刀吻电线    时间: 2013-1-28 23:25
不会吧,太恐怖了

作者: C.R.CAN    时间: 2013-2-24 23:22
都闪开,介个帖子,偶来顶





欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5