Ypbpr和HDMI是两种视频输出接口,笔者想做一个Service实现两者之间的切换
首先得加上AudioManager与DisplayManager:
Context mContext;
AudioManager mAudioManager = null;
DisplayManager mDisplayManager = null;
public SpdifManager(Context context){
mContext = context;
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
mDisplayManager = (DisplayManager) context.getSystemService("display");
}
其次两者之间的切换:
public void SetHdmiMode()
{
mDisplayManager.setDisplayOutputType(0,
DisplayManager.DISPLAY_OUTPUT_TYPE_HDMI, DisplayManager.DISPLAY_TVFORMAT_720P_60HZ);
mDisplayManager.setDisplayMode(DisplayManager.DISPLAY_MODE_SINGLE);//HDMI显示模式
mAudioManager.switchAudioOutMode(AudioManager.AUDIO_OUT_CHANNEL_HDMI);//HDMI声音模式
Settings.System.putString(mContext.getContentResolver(),
"audio_output_channel",AudioManager.auidoOutputChannelCode2Name(AudioManager.AUDIO_OUT_CHANNEL_HDMI));
Settings.System.putString(mContext.getContentResolver(),
"display_output_format", DispList.DISP_FORMAT_NAME_HDMI_720P_60HZ);
}
public void SetYfpbprMode()
{
mDisplayManager.setDisplayOutputType(0,
DisplayManager.DISPLAY_OUTPUT_TYPE_TV, DisplayManager.DISPLAY_TVFORMAT_720P_60HZ);
mDisplayManager.setDisplayMode(DisplayManager.DISPLAY_MODE_SINGLE_VAR);//YPBPR显示模式
mAudioManager.switchAudioOutMode(AudioManager.AUDIO_OUT_CHANNEL_CODEC);//YPBPR声音模式
Settings.System.putString(mContext.getContentResolver(),
"audio_output_channel",AudioManager.auidoOutputChannelCode2Name(AudioManager.AUDIO_OUT_CHANNEL_CODEC));
Settings.System.putString(mContext.getContentResolver(),
"display_output_format", DispList.DISP_FORMAT_NAME_YPBPR_720P_60HZ);
}
最后需要加入system权限和相应的许可:
android:sharedUserId="android.uid.system"
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.MODIFY_SOUND_SETTINGS" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.SETTINGS" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
如果程序需要写外置TF卡可以把写外置TF卡的代码单独写入一个activity中并与此service通信即可