When the user adjusts the volume, the system's media volume interface is displayed, which is out of line with the design style of the project.
Therefore, it is necessary to adjust the volume and display the custom media volume interface.
1) Adjust the Music volume
Private audiomanager = NULL; // audio
Audiomanager = (audiomanager) getsystemservice (service. audio_service );
Increase or decrease the volume:
Audiomanager. adjuststreamvolume (audiomanager. stream_music,
Audiomanager. adjust_raise, audiomanager. flag_play_sound
| Audiomanager. flag_show_ui );
Audiomanager. adjuststreamvolume (audiomanager. stream_music,
Audiomanager. adjust_lower, audiomanager. flag_play_sound
| Audiomanager. flag_show_ui );
Audiomanager. flag_show_ui displays the media volume page. Override the onkeydown method:
@ Override
Public Boolean onkeydown (INT keycode, keyevent event ){
Switch (keycode ){
Case keyevent. keycode_volume_up:
Audiomanager. adjuststreamvolume (audiomanager. stream_music,
Audiomanager. adjust_raise, audiomanager. flag_play_sound );
Return true;
Case keyevent. keycode_volume_down:
Audiomanager. adjuststreamvolume (audiomanager. stream_music,
Audiomanager. adjust_lower, audiomanager. flag_play_sound );
Return true;
Default:
Break;
}
Return super. onkeydown (keycode, event );
}
You can also display the custom media volume page.
2) custom media volume page
You need to obtain the current volume. The method is as follows:
Private void getv (){
// Call volume
Int max = audiomanager
. Getstreammaxvolume (audiomanager. stream_voice_call );
Int current = audiomanager
. Getstreamvolume (audiomanager. stream_voice_call );
Log. D ("vioce_call", "Max:" + MAX + "Current:" + current );
// System volume
Max = audiomanager. getstreammaxvolume (audiomanager. stream_system );
Current = audiomanager. getstreamvolume (audiomanager. stream_system );
Log. D ("system", "Max:" + MAX + "Current:" + current );
// Ringtone volume
Max = audiomanager. getstreammaxvolume (audiomanager. stream_ring );
Current = audiomanager. getstreamvolume (audiomanager. stream_ring );
Log. D ("ring", "Max:" + MAX + "Current:" + current );
// Music volume
Max = audiomanager. getstreammaxvolume (audiomanager. stream_music );
Current = audiomanager. getstreamvolume (audiomanager. stream_music );
Log. D ("Music", "Max:" + MAX + "Current:" + current );
// Sound volume prompt
Max = audiomanager. getstreammaxvolume (audiomanager. stream_alarm );
Current = audiomanager. getstreamvolume (audiomanager. stream_alarm );
Log. D ("alarm", "Max:" + MAX + "Current:" + current );
}
To get the current volume and the maximum volume, you can draw a media volume interface that is consistent with your own application style.