Android 系统(253)---O版本,用耳机按键控制FM的修改方法

O版本,用耳机按键控制FM的修改方法

 

 收音机播放时,按下耳机hook键,收音机暂停;若再次按hook键,收音机又会播放,之前的FAQ13878 由于架构改变,在O版本上会无效,现提供新的修改方法。

 

  一、 修改FmService.java (/vendor/mediatek/proprietary/packages/apps/FMRadio/src/com/android/fmradio/FmService.java)

1.在文件头部添加:

import android.content.ComponentName;
import android.media.RemoteControlClient;
import android.media.RemoteControlClient.MetadataEditor;

 

2.在FmService类的开始处添加如下:

private static final String TAG = "FmService";
//MTK add start
public static final String CMDTOGGLEPAUSE = "togglepause";
public static final String CMD= "command";
public static final String CMDNEXT = "cmdnext";
private RemoteControlClient mRemoteControlClient;
private ComponentName mFMMediaButtonIntentReceiver = null;
//MTK add end

 

3. 增加定义:

private AudioTrack mAudioTrack = null;
private AudioTrack mButtonAudioTrack = null; // MTK add

 

4.  powerUp()里增加如下:

mPowerStatus = DURING_POWER_UP;
// MTK add start
mButtonAudioTrack.play();
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING);
//MTK add end

 

5. powerDown() 里增加如下:

// activity used for update powerdown menu
mPowerStatus = POWER_DOWN;
// MTK add start
mButtonAudioTrack.stop();
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PAUSED);
//MTK add end

 

6.  onCreate()里增加如下:

if (!FmUtils.isFmSuspendSupport()) {
mWakeLock.setReferenceCounted(false);
}
sRecordingSdcard = FmUtils.getDefaultStoragePath();
//MTK add start
Log.d(TAG, "registerMediaButtonEventReceiver.mFMMediaButtonIntentReceiver" );
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mFMMediaButtonIntentReceiver = new ComponentName(getPackageName(),
FMMediaButtonIntentReceiver.class.getName());
am.registerMediaButtonEventReceiver(mFMMediaButtonIntentReceiver);

Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
i.setComponent(mFMMediaButtonIntentReceiver);
PendingIntent pi = PendingIntent.getBroadcast(this /*context*/,
0 /*requestCode, ignored*/, i /*intent*/, 0 /*flags*/);
mRemoteControlClient = new RemoteControlClient(pi);
mAudioManager.registerRemoteControlClient(mRemoteControlClient);

int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
| RemoteControlClient.FLAG_KEY_MEDIA_NEXT
| RemoteControlClient.FLAG_KEY_MEDIA_PLAY
| RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_STOP;
mRemoteControlClient.setTransportControlFlags(flags);

mButtonAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
SAMPLE_RATE, CHANNEL_CONFIG, AUDIO_FORMAT, RECORD_BUF_SIZE, AudioTrack.MODE_STREAM);
Log.d(TAG, "onCreate, mButtonAudioTrack = " + mButtonAudioTrack);

//MTK add end

 

7. onDestroy() 里增加如下:

Log.d(TAG, "onDestroy");
if (mIsParametersSet) {
mIsParametersSet = false;
Log.w(TAG, "AudioFmPreStop=0");
mAudioManager.setParameters("AudioFmPreStop=0");
}
setMute(true);
//MTK add start
Log.d(TAG, "unregisterMediaButtonEventReceiver.mFMMediaButtonIntentReceiver" );
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.unregisterMediaButtonEventReceiver(mFMMediaButtonIntentReceiver);
mAudioManager.unregisterRemoteControlClient(mRemoteControlClient);

try {
// mButtonAudioTrack.stop();
mButtonAudioTrack.release();
mButtonAudioTrack = null;
} catch (IllegalStateException e) {
Log.d(TAG, "stop mButtonAudioTrack, IllegalStateException");
} catch (NullPointerException e) {
Log.d(TAG, "stop mButtonAudioTrack, NullPointerException");
}
// MTK add end

 

8. onStartCommand() 增加如下:

int ret = super.onStartCommand(intent, flags, startId);
Log.d(TAG, ">>> FmRadioService.onStartCommand intent: " + intent + " startId: " + startId);
//MTK add start
String cmd = intent.getStringExtra("command");
Log.d(TAG, ">>> FmRadioService.onStartCommand command: " + cmd);
if( CMDTOGGLEPAUSE.equals(cmd) ){
Log.d(TAG, "onStartCommand:CMDTOGGLEPAUSE" );
if(mPowerStatus != POWER_UP){
powerUpAsync(FmUtils.computeFrequency(getFrequency()));}
else{
forceToHeadsetMode();
powerDownAsync();}
}

//MTK add end 

 

二, 在

/vendor/mediatek/proprietary/packages/apps/FMRadio/src/com/android/fmradio/ 下增加一只文件,文件名: FMMediaButtonIntentReceiver.java

内容如下:

 package com.android.fmradio;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.util.Log;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import java.util.List;


public class FMMediaButtonIntentReceiver extends BroadcastReceiver {
 public static final String TAG = "FmRx/Receiver";
 @Override
 public void onReceive(Context context, Intent intent) {
 Log.d(TAG, " mFMMediaButtonIntentReceiver onReceive");
 String Iaction = intent.getAction();
 if (Intent.ACTION_MEDIA_BUTTON.equals(Iaction)) {
 Log.d(TAG, " mFMMediaButtonIntentReceiver onReceive ACTION_MEDIA_BUTTON");
 KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
 if (event == null) {
 return; }
 int keycode = event.getKeyCode();
 int action = event.getAction();
 long eventtime = event.getEventTime();
 Log.d(TAG, "onReceive keycode="+keycode+",action="+action);

 String command = null;
 switch (keycode) {
 case KeyEvent.KEYCODE_MEDIA_STOP:
 break;
 case KeyEvent.KEYCODE_HEADSETHOOK:
 case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
 command = FmService.CMDTOGGLEPAUSE;
 break;
 case KeyEvent.KEYCODE_MEDIA_NEXT:
 break;
 case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
 break;
 case KeyEvent.KEYCODE_MEDIA_PAUSE:
 break;
 case KeyEvent.KEYCODE_MEDIA_PLAY:
 break;
 /// M: AVRCP and Android Music AP supports the FF/REWIND @{
 case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
 break;
 case KeyEvent.KEYCODE_MEDIA_REWIND:
 break;
 default:
 break;
 }
 if (command != null) {
 if ((action == KeyEvent.ACTION_DOWN) && (event.getRepeatCount() == 0)) {
 sendToStartService(context, command);}
 if (isOrderedBroadcast()) {
 abortBroadcast();}
 }} }

 public void sendToStartService(Context context, String command) {
 Intent i = new Intent(context, FmService.class);
 i.putExtra(FmService.CMD, command);
 context.startService(i); }
}


三. 修改 /vendor/mediatek/proprietary/packages/apps/FMRadio/AndroidMainfest.xml  ,添加如下:

<receiver android:name="com.android.fmradio.FMMediaButtonIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>

 

结束。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值