美文网首页
第十二节 音量控制、左右声道切换

第十二节 音量控制、左右声道切换

作者: 最美下雨天 | 来源:发表于2018-12-06 14:51 被阅读10次
image.png

注意这儿的代码:

const SLInterfaceID ids[2] = {SL_IID_BUFFERQUEUE,SL_IID_VOLUME};
    const SLboolean req[2] = {SL_BOOLEAN_TRUE,SL_BOOLEAN_TRUE};

    (*engineEngine)->CreateAudioPlayer(engineEngine, &pcmPlayerObject, &slDataSource, &audioSnk, 2, ids, req);
    //初始化播放器
    (*pcmPlayerObject)->Realize(pcmPlayerObject, SL_BOOLEAN_FALSE);

//    得到接口后调用  获取Player接口
    (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_PLAY, &pcmPlayerPlay);

    (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_VOLUME, &pcmVolumePlay);
void HAudio::setVolume(int percent) {
    volumePercent = percent;
    if(pcmVolumePlay != NULL)
    {
        if(percent > 30)
        {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -20);
        }
        else if(percent > 25)
        {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -22);
        }
        else if(percent > 20)
        {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -25);
        }
        else if(percent > 15)
        {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -28);
        }
        else if(percent > 10)
        {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -30);
        }
        else if(percent > 5)
        {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -34);
        }
        else if(percent > 3)
        {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -37);
        }
        else if(percent > 0)
        {
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -40);
        }
        else{
            (*pcmVolumePlay)->SetVolumeLevel(pcmVolumePlay, (100 - percent) * -100);
        }
    }
}
image.png
 const SLInterfaceID ids[3] = {SL_IID_BUFFERQUEUE,SL_IID_VOLUME,SL_IID_MUTESOLO};
    const SLboolean req[3] = {SL_BOOLEAN_TRUE,SL_BOOLEAN_TRUE,SL_BOOLEAN_TRUE};

    (*engineEngine)->CreateAudioPlayer(engineEngine, &pcmPlayerObject, &slDataSource, &audioSnk, 3, ids, req);
    //初始化播放器
    (*pcmPlayerObject)->Realize(pcmPlayerObject, SL_BOOLEAN_FALSE);

//    得到接口后调用  获取Player接口
    (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_PLAY, &pcmPlayerPlay);

    (*pcmPlayerObject)->GetInterface(pcmPlayerObject, SL_IID_VOLUME, &pcmVolumePlay);

    (*pcmPlayerObject)->GetInterface(
            pcmPlayerObject,
            SL_IID_MUTESOLO,
            &pcmPlayPlayerMuteSolo);
void HAudio::setStereoVolume() {
    (*pcmPlayPlayerMuteSolo)->SetChannelMute(
            pcmPlayPlayerMuteSolo,
            1,  //0右声道1左声道
            false //声道是否开启
    );
    (*pcmPlayPlayerMuteSolo)->SetChannelMute(
            pcmPlayPlayerMuteSolo,
            0,  //0右声道1左声道
            false //声道是否开启
    );
}

void HAudio::setRightVolume() {
    (*pcmPlayPlayerMuteSolo)->SetChannelMute(
            pcmPlayPlayerMuteSolo,
            1,  //0右声道1左声道
            false //声道是否开启
    );
    (*pcmPlayPlayerMuteSolo)->SetChannelMute(
            pcmPlayPlayerMuteSolo,
            0,  //0右声道1左声道
            true //声道是否开启
    );
}

void HAudio::setLeftVolume() {
    (*pcmPlayPlayerMuteSolo)->SetChannelMute(
            pcmPlayPlayerMuteSolo,
            0,  //0右声道1左声道
            false //声道是否开启
    );
    (*pcmPlayPlayerMuteSolo)->SetChannelMute(
            pcmPlayPlayerMuteSolo,
            1,  //0右声道1左声道
            true //声道是否开启
    );
}

相关文章

网友评论

      本文标题:第十二节 音量控制、左右声道切换

      本文链接:https://www.haomeiwen.com/subject/xcbttqtx.html