最近在实际工作中遇到了一种情况,写一个音量条,音量条显示出来之前要判断系统的音量大小,然后给音量条设置显示的位置。解决办法有两种,
第一种: m_pHostThread>MsgAsyncCall(MSG_METHOD(this,speakeMuteBtnHandler::onVolumeBarChanged));在onVolumeBarChanged函数中,把你当前的音量设置到系统中,实现onVolumeBarChanged函数。
1 BOOL speakeMuteBtnHandler::onVolumeBarChanged(msgObject&) 2 { 3 xSliderHandler* sliderHandler = m_pHostView->viewAt<xView>("volumeBar").findHandler<xSliderHandler>(); 4 chASSERT(sliderHandler != NULL); 5 int volume = devGetTalkVolume(); 6 sliderHandler->setValue(toString(volume)); 7 return TRUE; 8 }
在这个函数中,先将布局取到,m_pHostView指的是当前整个的布局,通过m_pHostView拿到 id为volumebar的音量条布局,在这个里面取出SliderHandler,判断当前音量条的位置,设置音量。
第二种:
把整个的次级菜单,封装进一个sliderHandler的类,这类可以继承xLinearLayout或者其他布局中需要的类,在类内,将这个布局文件加载出来,加载出来以后,取出对象,设置音量。
这里写一下音量条的联动:
1 void speakeMuteBtnHandler::onViewConnected() 2 { 3 m_pSpeakeMuteBtn = &m_pHostView->viewAt<xView>("speakeMuteBtn"); 4 slotSpeakeMuteBtn(); 5 msgJoinMessageGroup(szVolumeGroupName, MSG_METHOD(this, speakeMuteBtnHandler::onSpeakeMuteChanged)); 6 SLOT_CONNECT(m_pSpeakeMuteBtn, signalClicked, this, speakeMuteBtnHandler::slotSpeakeMuteBtnClicked); 7 xSliderHandler* sliderHandler = m_pHostView->viewAt<xView>("volumeBar").findHandler<xSliderHandler>(); 8 chASSERT(sliderHandler != NULL); 9 SLOT_CONNECT(sliderHandler, signalValueChanged, this, speakeMuteBtnHandler::slotVolumeBarChanged); 10 m_pHostThread->MsgAsyncCall(MSG_METHOD(this, speakeMuteBtnHandler::onVolumeBarChanged)); 11 };
onViewConnected这个函数只加载一次,slotSpeakeMUteBtn是要初始化一次,这个函数的实现是:
1 void speakeMuteBtnHandler::slotSpeakeMuteBtn() 2 { 3 xImageView& icon = m_pSpeakeMuteBtn->viewAt<xImageView>("icon"); 4 if (devGetOutputSilence()) 5 { 6 icon.setPixmap("talk/softkey/silence.png"); 7 } 8 else 9 { 10 icon.setPixmap("talk/softkey/volume-mute-btn-normal.png"); 11 } 12 };
判断当前是否静音,设置图标状态。
1 BOOL speakeMuteBtnHandler::onSpeakeMuteChanged(msgObject& msgObj) 2 { 3 xImageView& icon = m_pSpeakeMuteBtn->viewAt<xImageView>("icon"); 4 switch (msgObj.message) 5 { 6 case VOLUME_OUTPUT_MUTE_CHANGED: 7 slotSpeakeMuteBtn(); 8 break; 9 default: 10 return FALSE; 11 } 12 return TRUE; 13 };
监听组播消息,这里的组播是线程通信的一种,其他地方有设置静音,这里进行监听,进行相应的UI修改。
1 void speakeMuteBtnHandler::slotSpeakeMuteBtnClicked(chSignal* /*sender*/, WPARAM /*wParam*/, LPARAM /*lParam*/, int /*nDataBytes*/, LPVOID /*pData*/) 2 { 3 devSetOutputSilence(!devGetOutputSilence()); 4 };
调用静音接口函数。
1 void speakeMuteBtnHandler::slotVolumeBarChanged(chSignal* /*sender*/, WPARAM wParam, LPARAM /*lParam*/, int /*nDataBytes*/, LPVOID /*pData*/) 2 { 3 xSliderHandler* sliderHandler = m_pHostView->viewAt<xView>("volumeBar").findHandler<xSliderHandler>(); 4 int volume = 0; 5 toValue(sliderHandler->value, volume); 6 devSetTalkVolume(volume); 7 };
根据滚动条的变化来设置音量。