zoukankan      html  css  js  c++  java
  • OpenAL音频播放

    //
    //  OpenALPlayer.m
    //  live
    //
    //  Created by lujunjie on 2016/11/5.
    //  Copyright © 2016年 lujunjie. All rights reserved.
    //
    
    #import "OpenALPlayer.h"
    #import <OpenAL/al.h>
    #import <OpenAL/alc.h>
    @interface OpenALPlayer()
    {
        ALCdevice  *mDevice;
        ALCcontext *mContext;
        ALuint     outSourceID;
    }
    @end
    @implementation OpenALPlayer
    - (instancetype)init
    {
        if (self=[super init]) {
            
            mDevice=alcOpenDevice(NULL);
            if (mDevice) {
                mContext=alcCreateContext(mDevice, NULL);
                alcMakeContextCurrent(mContext);
                alGenSources(1, &outSourceID);// 创建多个source
                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);
            }
            
        }
        return self;
    }
    - (void)play:(void*)pcmData length:(unsigned int)length
    {
        NSCondition* ticketCondition= [[NSCondition alloc] init];
        [ticketCondition lock];
        ALuint bufferID = 0;
        alGenBuffers(1, &bufferID); // 创建buffer并获取bufferID
        alBufferData(bufferID, AL_FORMAT_MONO16, pcmData, length, 8000);//把数据添加到buffer
        alSourceQueueBuffers(outSourceID, 1, &bufferID);// 入队bufferID
        ALint stateVaue;
        alGetSourcei(outSourceID, AL_SOURCE_STATE, &stateVaue);//获取状态
        
        if (stateVaue != AL_PLAYING)
        {
            alSourcePlay(outSourceID);
        }
        
        [ticketCondition unlock];
        ticketCondition = nil;
    }
    @end
  • 相关阅读:
    [2020多校联考]甲虫
    .eww
    MinGW安装c-c++
    .竖梁上的两个孔最小距离可以是多少呢?PS15D
    .dwg(sw)-exb
    开始学emacs-1
    看jpg和png图片
    .系列化参数关系
    2015计划
    大蚂蚁在64位系统下,右键没有快发的解决方案
  • 原文地址:https://www.cnblogs.com/-ljj/p/6033748.html
Copyright © 2011-2022 走看看