最近家里的電視不知道什么鬼,經常性的音量自動跳到0,不是減小到0,是直接跳到0,而且跳的時候不顯示音量面板,就是突然沒聲了,然后發現音量成了0。網上各種搜,找不到問題。于是只好換個思路,跳0的時候,再自動給它調回去。
1.獲取音量改變資訊:接收系統的音量改變事件廣播android.media.VOLUME_CHANGED_ACTION,然后從廣播intent中獲取oldVolume和newVolume。這個容易
2.設定音量:如果newVolume==0且差值為1則把音量設定為oldVolume。
方案一:利用自動化軟體Macrodroid,接收到廣播后執行shell命令來調節音量:"media volume --stream3 --set 10",結果可能
是電視版的media命令是閹割過的,尼瑪沒有volume子命令。
方案二:自己寫個APP來做。內容如下:
這是廣播接收器代碼:
public class VolumeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//Toast.makeText(context,"音量變化",Toast.LENGTH_SHORT).show();
int oldVol=intent.getIntExtra("android.media.EXTRA_PREV_VOLUME_STREAM_VALUE",-1);
int newVol=intent.getIntExtra("android.media.EXTRA_VOLUME_STREAM_VALUE",-1);
int streamType=intent.getIntExtra("android.media.EXTRA_VOLUME_STREAM_TYPE",-1);
Log.d("StreamType:"+streamType, "調節后音量:"+newVol+(char)10+"調節前音量:"+oldVol);
if(Math.abs(oldVol-newVol)>1 && newVol==0){
//if(newVol==0){
AudioManager audioManager=(AudioManager)context.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setStreamVolume(streamType,oldVol,AudioManager.FLAG_PLAY_SOUND);
Toast.makeText(context,"檢測到例外的音量歸零,已自動恢復", Toast.LENGTH_LONG).show();
}
}
}這是清單檔案:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.yangliu.volmanager">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
<uses-feature android:name="android.software.touchscreen"
android:required="false"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@android:style/Theme.NoDisplay"
tools:ignore="GoogleAppIndexingWarning">
<receiver
android:name=".BootReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<!-- <category android:name="android.intent.category.LAUNCHER" /> -->
</intent-filter>
</activity>
<receiver
android:name=".VolumeReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.media.VOLUME_CHANGED_ACTION" />
</intent-filter>
</receiver>
</application>
</manifest>
主要代碼就這些了。
結果:日志記錄顯示音量的值是改變回去了的,但實際上聲音還是沒有,音量面板上的值也是變回去了的,但是只要再按一下遙控的音量+,進度條就變成了1,也就是說原來是0。奇怪的很吶,求高手指點。
方案一的問題有沒有解決方案?方案二是什么問題?怎么解決?


uj5u.com熱心網友回復:
有沒有大神來解答一下呀
uj5u.com熱心網友回復:
補充一下,電視是android 4.4的。uj5u.com熱心網友回復:
mAudioManager=(AudioManager) getSystemService(AUDIO_SERVICE);設定聲音如下
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, mCurrentVolume,0);
靜音如下:
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
當前音量如下:
volume=mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
以上是多個安卓4.4的方案中播放器的音量控制代碼;
uj5u.com熱心網友回復:
跟我的代碼沒區別
uj5u.com熱心網友回復:
我建議換個電視轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/61253.html
標籤:Android
