织梦CMS - 轻松建站从此开始!

罗索实验室

iphone/ipad使用OpenAl实例代码

落鹤生 发布于 2011-07-26 14:34 点击:次 
我是写MAC播放音频流,写出后,最好的情况是播放时有明显卡,修改代码数次还是没效果;于是乎,我就使者在Ipad上用这个播放,很流畅,郁闷ing.......
TAG:

这两天搞一个网络传输音频流播放,搞的晕头转向的,我在工程中就用的是XCode自带的OpenAl.frame

我是写MAC播放音频流,写出后,最好的情况是播放时有明显卡,修改代码数次还是没效果;于是乎,我就使者在Ipad上用这个播放,很流畅,郁闷ing.......

 
由此可见,是平台的问题,一个是32位的,一个是64位的,暂时没有想到解决办法...
现把代码公布如下,代码就不解释了,自己网上找下吧,这个库很简单,需要的朋友自取:
使用例子
Sound *mySound; //这个做成全局的

 
//初始下
mySound = [[Sound alloc] init];
[mySound initOpenAL];

 
//播放流时用这个
[mySound openAudioFromQueue: data dataSize: dataSize];

 
//.h

#import <Foundation/Foundation.h>

#import <OpenAL/al.h>

#import <OpenAL/alc.h>

@interface Sound : NSObject {

ALCcontext *mContext;

    ALCdevice *mDevice;

 

ALuint outSourceID;

}


 

@property (nonatomic) ALCcontext *mContext;

@property (nonatomic) ALCdevice *mDevice;


 

-(void)initOpenAL;


 

-(void)playSound;

-(void)stopSound;

-(void)cleanUpOpenALID;

-(void)cleanUpOpenAL;


 

@end


 

//.m

 

#import "Sound.h"

@implementation Sound


 

@synthesize mContext;

@synthesize mDevice;


 

-(void)initOpenAL

{

    mDevice=alcOpenDevice(NULL);

    if (mDevice) {

        mContext=alcCreateContext(mDevice, NULL);

        alcMakeContextCurrent(mContext);

    }

 

alGenSources(1, &outSourceID);

alSpeedOfSound(1.0);

alDopplerVelocity(1.0);

alDopplerFactor(1.0);

alSourcef(outSourceID, AL_PITCH, 1.0f);

alSourcef(outSourceID, AL_GAIN, 1.0f);

alSourcei(outSourceID, AL_LOOPING, AL_FALSE);

alSourcef(outSourceID, AL_SOURCE_TYPE, AL_STREAMING);

 

[NSTimer scheduledTimerWithTimeInterval: 1/1000.0 

target:self  

  selector:@selector(updataQueueBuffer) 

  userInfo: nil

repeats:YES];

}


 

- (void) openAudioFromQueue:(unsigned char*)data dataSize:(UInt32)dataSize

{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

 

NSCondition* ticketCondition= [[NSCondition alloc] init];

[ticketCondition lock];

 

ALuint bufferID = 0;

alGenBuffers(1, &bufferID);

 

NSData * tmpData = [NSData dataWithBytes:data length:dataSize];

alBufferData(bufferID, AL_FORMAT_STEREO16, (char*)[tmpData bytes], (ALsizei)[tmpData length], 44100);

alSourceQueueBuffers(outSourceID, 1, &bufferID);

 

[self updataQueueBuffer];

 

ALint stateVaue;

alGetSourcei(outSourceID, AL_SOURCE_STATE, &stateVaue);

 

[ticketCondition unlock];

[ticketCondition release];

ticketCondition = nil;

 

[pool release];

pool = nil;

}


 

- (BOOL) updataQueueBuffer

{

ALint stateVaue;

int processed, queued;

 

alGetSourcei(outSourceID, AL_SOURCE_STATE, &stateVaue);

 

if (stateVaue == AL_STOPPED ||

stateVaue == AL_PAUSED || 

stateVaue == AL_INITIAL) 

{

[self playSound];

return NO;

}

 

    alGetSourcei(outSourceID, AL_BUFFERS_PROCESSED, &processed);

alGetSourcei(outSourceID, AL_BUFFERS_QUEUED, &queued);

 

NSLog(@"Processed = %d\n", processed);

NSLog(@"Queued = %d\n", queued);

 

    while(processed--)

    {

        ALuint buff;

        alSourceUnqueueBuffers(outSourceID, 1, &buff);

alDeleteBuffers(1, &buff);

}

 

return YES;

}

 

(greenman723)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www1.rosoo.net/a/201107/14755.html]
本文出处:blog.sina.com.cn/greenman723 作者:greenman723
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容