Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android FM模塊學習之三 FM手動調頻

Android FM模塊學習之三 FM手動調頻

編輯:關於Android編程

前一章我們學習了FM的自動調頻,接下來我們就看看FM手動調頻是如何進行的。如果不清楚FM自動調頻的過程,請打開超鏈接查看FM搜索頻率流程。

首先來看一下流程圖:

\

2.滑動刻度盤HorizontalNumberPicker控件在監聽事件裡使用方法valueToFrequency(newVal)

1.長按左右箭頭居中的頻率字符串,彈出FrequencyPickerDialog調頻對話框確定調用tuneRadio(frequency)調頻。<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGltZyBzcmM9"/uploadfile/Collfiles/20141126/201411260842539.jpg" alt="\">

獲取到頻率

protected int valueToFrequency(int value) {
       mFrequency = mPrefs.getLowerLimit() + value *
                             mPrefs.getFrequencyStepSize();
       return mFrequency;
   }
發送一個handler 回調一個tuneRadio(frequency)調頻。

Runnable mRadioChangeFrequency = new Runnable(){
       public void run() {
           mUpdatePickerValue = false;
           tuneRadio(mFrequency);
       }
   };

3.手動點擊按鈕左右箭頭, 通過監聽調用:

\


int frequency =FmSharedPreferences.getNextTuneFrequency();

int frequency =FmSharedPreferences.getPrevTuneFrequency();

tuneRadio(frequency);進行調頻

getNextTuneFrequency()方法通過判斷頻率最大限制范圍,後加200(刻度)

getPrevTuneFrequency()方法通過判斷頻率最小限制范圍,後減200(刻度)


調頻方法分析

private void tuneRadio(int frequency)


private void tuneRadio(int frequency){
      /* Issue the tune command only if tuneCommand is already not active */
      if((mService != null) && (mCommandActive != CMD_TUNE) && isFmOn()) {
         boolean bStatus = false;
         try {
             bStatus = mService.tune(frequency);
             if (bStatus) {
                 postTimeoutHandler(CMD_TUNE);
             }else {
               if (isFmOn()) {
                  mCommandFailed = CMD_TUNE;
                  Log.e(LOGTAG, "mService.tune failed");
                  showDialog(DIALOG_CMD_FAILED);
               }
             }mTunedStation.setName("");
             mTunedStation.setPI(0);
             mTunedStation.setPty(0);
             updateStationInfoToUI();
         }catch (RemoteException e) {
            e.printStackTrace();
         }
      }else {
         Log.e(LOGTAG, "Delayed Tune handler stopped");
      }
   }


通過回調引用類調用FMRadioService類的tune()方法進行調頻

bStatus = mService.tune(frequency);

發送一個廣播連接是否超時

postTimeoutHandler(CMD_TUNE);


設置調頻名字,更新FMRadioUI界面信息

mTunedStation.setName("");

mTunedStation.setPI(0);

mTunedStation.setPty(0);

updateStationInfoToUI()

(通過IFMRadioSrevice.aidl通信機制onbind返回的類的引用調用FMRadioService中的調頻方法)

FMRadioService中的tune方法

public boolean tune(int frequency)

public boolean tune(int frequency) {
      boolean bCommandSent=false;
      double doubleFrequency = frequency/1000.00;

      Log.d(LOGTAG, "tuneRadio:  " + doubleFrequency);
      if (mReceiver != null)
      {
         mReceiver.setStation(frequency);
         bCommandSent = true;
      }
      return bCommandSent;
   }

調用FMReceiver類的setStation方法調頻

public boolean setStation (intfrequencyKHz)


public boolean setStation (int frequencyKHz) {
      int ret;

      mControl.setFreq(frequencyKHz);
      ret = mControl.setStation(sFd);
      if(ret < 0 )
      {
         return false;
      }
      else
      {
         return true;
      }
   }

調用FMRxControls類(FM讀取控制台信息)設置頻率

mControl.setFreq(frequencyKHz);


設置優化調頻核心指定的頻率

ret = mControl.setStation(sFd);

  public int setStation(int fd) {
      Log.d(TAG, "** Tune Using: "+fd);
      int ret = FmReceiverJNI.setFreqNative(fd, mFreq);
      Log.d(TAG, "** Returned: "+ret);
      return ret;
   }

最後調用FmReceiverJNI類

setFreqNative(fd, mFreq); 本地方法 JNI到 cpp文件

/*native interface */
static jint android_hardware_fmradio_FmReceiverJNI_setFreqNative
    (JNIEnv * env, jobject thiz, jint fd, jint freq)
{
    int err;
    double tune;
    struct v4l2_frequency freq_struct;
    freq_struct.type = V4L2_TUNER_RADIO;
    freq_struct.frequency = (freq*TUNE_MULT/1000);
    err = ioctl(fd, VIDIOC_S_FREQUENCY, &freq_struct);
    if(err < 0){
            return FM_JNI_FAILURE;
    }
    return FM_JNI_SUCCESS;
}




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved