由于目前的Android系统如果系统的SYSTEM(STREAM_SYSTEM)中的音量设置或者调节为0,相关的STREAM_*的音量也会被Mute住,比如说,我现在的系统将对应的STREAM_SYSTEM和STREAM_RING/STREAM_NOTIFICATION在Mute功能方面绑定的,如果我将STREAM_SYSTEM的volume调节为0,那么Android系统会将STREAM_SYSTEM这个音量MUTE,记住MUTE住和volume为0是两回事,对应的volume=0,依然能够听到声音,只是非常的小而已,但是如果是MUTE了,无论volume是否为0,均在听不到声音,只是现在Android系统在当使用者将STREAM_SYSTEM调节为0时,会开启Mute功能,将STREAM_SYSTEM种类的彻底MUTE住,另外RING/NOTIFICATION是两关相关的,所以他们也会被MUTE住,具体参看下面的程序:

<0> : 修改的文件名大致在./framework/base/media/java/android/media/AudioService.java,路径可能有不正确,但是修改的文件是AudioService.java

<1> :现将各自的关系分离开:



private final int[] STREAM_VOLUME_ALIAS = new int[] {
AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL
AudioSystem.STREAM_SYSTEM, // STREAM_SYSTEM
//--- AudioSystem.STREAM_RING, // STREAM_SYSTEM
AudioSystem.STREAM_RING, // STREAM_RING
AudioSystem.STREAM_MUSIC, // STREAM_MUSIC
AudioSystem.STREAM_ALARM, // STREAM_ALARM
AudioSystem.STREAM_NOTIFICATION, // STREAM_NOTIFICATION
//--- AudioSystem.STREAM_RING, // STREAM_NOTIFICATION
AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO
AudioSystem.STREAM_SYSTEM_ENFORCED, // STREAM_SYSTEM_ENFORCED
//--- AudioSystem.STREAM_RING, // STREAM_SYSTEM_ENFORCED
AudioSystem.STREAM_DTMF, // STREAM_DTMF
//--- AudioSystem.STREAM_RING, // STREAM_DTMF
AudioSystem.STREAM_TTS, // STREAM_TTS
//--- AudioSystem.STREAM_MUSIC // STREAM_TTS
};
private final int[] STREAM_VOLUME_ALIAS_NON_VOICE = new int[] {
AudioSystem.STREAM_VOICE_CALL, // STREAM_VOICE_CALL
AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM
AudioSystem.STREAM_RING, // STREAM_RING
AudioSystem.STREAM_MUSIC, // STREAM_MUSIC
AudioSystem.STREAM_ALARM, // STREAM_ALARM
AudioSystem.STREAM_NOTIFICATION, // STREAM_NOTIFICATION
//AudioSystem.STREAM_RING, // STREAM_NOTIFICATION
AudioSystem.STREAM_BLUETOOTH_SCO, // STREAM_BLUETOOTH_SCO
AudioSystem.STREAM_MUSIC, // STREAM_SYSTEM_ENFORCED
AudioSystem.STREAM_MUSIC, // STREAM_DTMF
AudioSystem.STREAM_MUSIC // STREAM_TTS
};


<2> : 修改private void readPersistedSettings()里面的"或"关系,将RING/NOTIFICATION将其不要跟随STREAM_SYSTEM一起MUTE,只是MUTE STREAM_SYSTEM一个就好了,总共在这个方法中修改三处:

下面有两处:



// make sure settings for ringer mode are consistent with device type: non voice capable
// devices (tablets) include media stream in silent mode whereas phones don't.
mRingerModeAffectedStreams = Settings.System.getIntForUser(cr,
Settings.System.MODE_RINGER_STREAMS_AFFECTED,
(/*(1 << AudioSystem.STREAM_RING)/*|(1 << AudioSystem.STREAM_NOTIFICATION)|*/
(1 << AudioSystem.STREAM_SYSTEM)|(1 << AudioSystem.STREAM_SYSTEM_ENFORCED)),
UserHandle.USER_CURRENT);

// ringtone, notification and system streams are always affected by ringer mode
mRingerModeAffectedStreams |= /*(1 << AudioSystem.STREAM_RING)|
(1 << AudioSystem.STREAM_NOTIFICATION)|*/
(1 << AudioSystem.STREAM_SYSTEM);


第三处:



// only effort system itself
mMuteAffectedStreams = System.getIntForUser(cr,
System.MUTE_STREAMS_AFFECTED,
(/*(1 << AudioSystem.STREAM_MUSIC)|
(1 << AudioSystem.STREAM_RING)|*/
(1 << AudioSystem.STREAM_SYSTEM)),
UserHandle.USER_CURRENT);


其实我还修改了另外一个方法public void setStreamMute(int streamType, boolean state, IBinder cb)里面的内容:



/** @see AudioManager#setStreamMute(int, boolean) */
public void setStreamMute(int streamType, boolean state, IBinder cb) {

if(streamType==AudioManager.STREAM_RING || streamType==AudioManager.STREAM_NOTIFICATION){

return ;

}

if (isStreamAffectedByMute(streamType)) {
mStreamStates[streamType].mute(cb, state);
}
}


我增加了三行,其实我个人觉得,我前面分离的它们所有者的关系,这一步应该不需要了,但是我为了以防万一,我还是在它准备调用mute功能里面再次做了一次检查,其他它下面的isStreamAffectedByMute(streamType)条件就可以判断了.当然反过来想一想,如果我没有修改前面三处,直接在这里增加的三行同样可以摆平MUTE功能不会施加在RING和NOTIFICATION上.当然我的Android系统被改成RING/NOTIFICATION的volume大小始终保持一致了,所以同样还是要修改前面3点,保证所有的STREAM_**不会被其他的干扰.

修改完以后,build一下,生成framework.jar,放到系统里面去,发现还是不行,没有达到目的,但是RING/NOTIFICATION两者的关系是分开了,但是当STREAM_SYSTEM调节为0时,还是会MUTE住RING/NOTIFICATION两个音量,经过查找,发现还需要修改一个文件,不过这个文件时SettingsProvider.apk工程中的.修改如下:

路径大概在./framework/base/packages/SettingsProvider/DatabaseHelper.java下,修改DatabaseHelper.java文件,修改的内容如下:



/**
* Loads the default volume levels. It is actually inserting the index of
* the volume array for each of the volume controls.
*
* @param db the database to insert the volume levels into
*/
private void loadVolumeLevels(SQLiteDatabase db) {
SQLiteStatement stmt = null;
try {
stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+ " VALUES(?,?);");

loadSetting(stmt, Settings.System.VOLUME_MUSIC,
AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_MUSIC]);
loadSetting(stmt, Settings.System.VOLUME_RING,
AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_RING]);
loadSetting(stmt, Settings.System.VOLUME_SYSTEM,
AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_SYSTEM]);
loadSetting(
stmt,
Settings.System.VOLUME_VOICE,
AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_VOICE_CALL]);
loadSetting(stmt, Settings.System.VOLUME_ALARM,
AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_ALARM]);
loadSetting(
stmt,
Settings.System.VOLUME_NOTIFICATION,
AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_NOTIFICATION]);
loadSetting(
stmt,
Settings.System.VOLUME_BLUETOOTH_SCO,
AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_BLUETOOTH_SCO]);

// By default:
// - ringtones, notification, system and music streams are affected by ringer mode
// on non voice capable devices (tablets)
// - ringtones, notification and system streams are affected by ringer mode
// on voice capable devices (phones)
int ringerModeAffectedStreams = /*(1 << AudioManager.STREAM_RING) |
(1 << AudioManager.STREAM_NOTIFICATION) |*/
(1 << AudioManager.STREAM_SYSTEM) |
(1 << AudioManager.STREAM_SYSTEM_ENFORCED);
if (!mContext.getResources().getBoolean(
com.android.internal.R.bool.config_voice_capable)) {
ringerModeAffectedStreams |= (1 << AudioManager.STREAM_MUSIC);
}
loadSetting(stmt, Settings.System.MODE_RINGER_STREAMS_AFFECTED,
ringerModeAffectedStreams);

loadSetting(stmt, Settings.System.MUTE_STREAMS_AFFECTED,
((1 << AudioManager.STREAM_MUSIC) |
/*(1 << AudioManager.STREAM_RING) |
(1 << AudioManager.STREAM_NOTIFICATION) |*/
(1 << AudioManager.STREAM_SYSTEM)));
} finally {
if (stmt != null) stmt.close();
}

loadVibrateWhenRingingSetting(db);
}


注意上面的几个<<运算,将其中RING/NOTIFICATION去掉就可以了.

让后再build一次,将会生成SettingsProvider.apk,将其放到系统里面,重启再试一下,结果发现还是不行,晕吧.

通过终端进入,将系统的/data/*下所有的数据删除,然后再重启一下系统,就OK了.

下面给出测试DEMO程序:



package com.example.androidvolumedemo;

import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity implements OnSeekBarChangeListener {

private AudioManager mAudioManager;

private SeekBar mSpkSeekBar;
private SeekBar mHandSeekBar;
private SeekBar mHeadSeekBar;
private SeekBar mRingSeekBar;
private SeekBar mNotifSeekBar;

private Button mUpdateBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

mSpkSeekBar = (SeekBar) findViewById(R.id.tf_gro_speaker_bar);
mSpkSeekBar.setOnSeekBarChangeListener(this);
mSpkSeekBar.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM));

mHandSeekBar = (SeekBar) findViewById(R.id.tf_gro_handset_bar);
mHandSeekBar.setOnSeekBarChangeListener(this);
mHandSeekBar.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL));

mHeadSeekBar = (SeekBar) findViewById(R.id.tf_gro_headset_bar);
mHeadSeekBar.setOnSeekBarChangeListener(this);
mHeadSeekBar.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL));

mRingSeekBar = (SeekBar) findViewById(R.id.tf_gro_ring_bar);
mRingSeekBar.setOnSeekBarChangeListener(this);
mRingSeekBar.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING));

mNotifSeekBar = (SeekBar) findViewById(R.id.tf_gro_notif_bar);
mNotifSeekBar.setOnSeekBarChangeListener(this);
mNotifSeekBar.setMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION));


mUpdateBtn=(Button)findViewById(R.id.updatebtn);
mUpdateBtn.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
updateUI();
}

});

updateUI();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:

return true;
}
return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:

return true;
case KeyEvent.KEYCODE_VOLUME_UP:

return true;
}
return super.onKeyUp(keyCode, event);
}

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
int id = seekBar.getId();

switch (id) {
case R.id.tf_gro_speaker_bar:
mAudioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, progress, AudioManager.FLAG_PLAY_SOUND);

break;
case R.id.tf_gro_handset_bar:
mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, progress, AudioManager.FLAG_PLAY_SOUND);
break;
case R.id.tf_gro_headset_bar:
mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, progress, AudioManager.FLAG_PLAY_SOUND);
break;
case R.id.tf_gro_ring_bar:
mAudioManager.setStreamVolume(AudioManager.STREAM_RING, progress, AudioManager.FLAG_PLAY_SOUND);
break;
case R.id.tf_gro_notif_bar:
mAudioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION, progress, AudioManager.FLAG_PLAY_SOUND);
break;

}

// updateUI();

}

private void updateUI(){

this.mSpkSeekBar.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM));
this.mHandSeekBar.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL));
this.mHeadSeekBar.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL));
this.mRingSeekBar.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_RING));
this.mNotifSeekBar.setProgress(mAudioManager.getStreamVolume(AudioManager.STREAM_NOTIFICATION));

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

}


xml文件内容:



<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="195dp"
android:orientation="vertical" >

<!-- system -->
<TextView
android:id="@+id/tev1"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:paddingLeft="10dip"
android:text="speaker"/>

<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imagev4"
android:paddingRight="10dip"
android:paddingLeft="10dip"
android:src="@drawable/ic_action_volume_on"
/>
<SeekBar
android:id="@+id/tf_gro_speaker_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

</TableRow>

</TableLayout>

<!-- voice -->
<TextView
android:id="@+id/tev2"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:paddingLeft="10dip"
android:text="handset"/>

<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imagev1"
android:paddingRight="10dip"
android:paddingLeft="10dip"
android:src="@drawable/ic_action_ring_volume"
/>
<SeekBar
android:id="@+id/tf_gro_handset_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

</TableRow>

</TableLayout>

<TextView
android:id="@+id/tev3"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:paddingLeft="10dip"
android:text="headset"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imagev3"
android:paddingRight="10dip"
android:paddingLeft="10dip"
android:src="@drawable/ic_action_headset"
/>
<SeekBar
android:id="@+id/tf_gro_headset_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

</TableRow>

</TableLayout>

<TextView
android:id="@+id/tev5"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:paddingLeft="10dip"
android:text="ring"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imagev2"
android:paddingRight="10dip"
android:paddingLeft="10dip"
android:src="@drawable/ic_action_volume_on"
/>
<SeekBar
android:id="@+id/tf_gro_ring_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

</TableRow>

</TableLayout>

<TextView
android:id="@+id/tev2"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:paddingLeft="10dip"
android:text="notifications"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imagev2"
android:paddingRight="10dip"
android:paddingLeft="10dip"
android:src="@drawable/ic_action_warning"
/>

<Button
android:id="@+id/updatebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="update UI" />



<SeekBar
android:id="@+id/tf_gro_notif_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

</TableRow>

</TableLayout>

</LinearLayout>

</ScrollView>


 

OK,就可以了.

 

如果要增加相关关系,即如果STREAM_SYSTEM的volume调节为0时,MUTE住了STREAM_SYSTEM,还要增加对STREAM_MUSIC的MUTE作用,可以依照RING修改的做法进行依葫芦画瓢,进行逆向操作,修改的文件都是一样的.