OGG Vorbis in iPhone sdk

为君一笑 提交于 2019-11-28 07:51:51
DanSkeel

Better late than never ;)
I have found the answer here Use cocos2d for playing ogg file in my project?.

PASoundMgr is a different sound engine that had support for ogg file playback. However, it hasn't been updated since iOS 2.x and there are numerous issues that have cropped up since then that it doesn't handle.

Why do you need to play ogg files? If you convert them to aac you will be able to play them back using hardware decoding which is much more efficient from a cpu usage point of view.

They mentioned PASoundMgr. It worked for me. I just copied from cocos2d framework all files->libraries that SoundEngineTest was based on. And got rid of unnecessary code.

Here is my demoProject that shows how to play ogg on ios.

Be careful with iOS 5.* simulators, they have some problems with sound library. My demo works on 4.3 simulator and on Device.


Here are steps that I made to create demo:

  1. First you will need cocos2d-iphone framework. I've already had it, but you can find it here cocos-2d_download.

  2. As you can notice SoundEngineTest depends on libvorbis.a. It's a library that made of files from external/Tremor group. Also it depends on OpenAl, AudioToolbox frameworks.

  3. I copied all files from tremor group to my project. Crearted "vorbis" Cocoa Touch Static Library, without ARC. And added all source files and header to the "vorbis" target in Build Phases tab.

  4. In the Build Phases of OggPlayDemo Added libraries (libvorbis, OpenAl, AudioToolbox) to the Link Binary with Libraries box.

  5. Added PA classes to project. And checked OggPlayDemo as a target. To avoid problems with ARC, I disabled ARC compilation for this 3 PA files. (see disable ARC for single file)

  6. Removed all cocos2d references. There were some code related to correcting position of listener depending on orientation... I commented it. I don't need this feature for just playing audio.

  7. Copied audio file.

  8. And finally added this code to ViewController:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        //init
        [PASoundMgr sharedSoundManager];
        [[[PASoundMgr sharedSoundManager] listener] setPosition:CGPointMake(0, 0)];
        self.audioSource = [[PASoundMgr sharedSoundManager] addSound:@"trance-loop" withExtension:@"ogg" position:CGPointMake(0, 0) looped:YES];
    
        // lower music track volume and play it
        [self.audioSource setGain:0.5f];
    }
    - (IBAction)play:(id)sender {
        [self.audioSource playAtListenerPosition];   
    }
    

Cricket Audio will play ogg files, among others, and works on iOS/Android/WP8.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!