Android FM模块学习之三 FM手动调频

前一章我们学习了FM的自动调频,接下来我们就看看FM手动调频是如何进行的。如果不清楚FM自动调频的过程,请打开超链接查看FM搜索频率流程。

首先来看一下流程图:

8cc705670f78a9e626eacb0fe55259c7716.jpg

2.滑动刻度盘HorizontalNumberPicker控件在监听事件里使用方法valueToFrequency(newVal)

1.长按左右箭头居中的频率字符串,弹出FrequencyPickerDialog调频对话框确定调用tuneRadio(frequency)调频。

b4923b6387dfca6d7bb05c93c22d858b813.jpg

获取到频率

    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.手动点击按钮左右箭头, 通过监听调用:

1ed57cce6e7e405b9ec4b939caecfe26545.jpg
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;
    }

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flybirding10011

谢谢支持啊999

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值