本文实例讲述了android实现音量调节的方法。分享给大家供大家参考。具体如下:
main.xml布局文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<?xml version= "1.0" encoding= "utf-8" ?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" android:layout_width= "fill_parent" android:layout_height= "fill_parent" > <button android:id= "@+id/btnplay" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "播放音乐" /> <linearlayout android:orientation= "horizontal" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center_horizontal" > <togglebutton android:id= "@+id/tbmute" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:texton= "静音" android:textoff= "正常" android:checked= "true" android:layout_gravity= "center_vertical" /> <button android:id= "@+id/btnupper" android:text= "增大音量" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> <button android:id= "@+id/btnlower" android:text= "减小音量" android:layout_width= "wrap_content" android:layout_height= "wrap_content" /> </linearlayout> </linearlayout> |
audioactivity类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package com.ljq.activity; import android.app.activity; import android.app.service; import android.media.audiomanager; import android.media.mediaplayer; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.compoundbutton; import android.widget.togglebutton; import android.widget.compoundbutton.oncheckedchangelistener; public class audioactivity extends activity { private button btnplay= null , btnupper= null , btnlower= null ; private togglebutton tbmute= null ; private mediaplayer mediaplayer= null ; //声频 private audiomanager audiomanager= null ; //音频 @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); audiomanager=(audiomanager)getsystemservice(service.audio_service); btnplay=(button)findviewbyid(r.id.btnplay); btnupper=(button)findviewbyid(r.id.btnupper); btnlower=(button)findviewbyid(r.id.btnlower); btnplay.setonclicklistener(listener); btnupper.setonclicklistener(listener); btnlower.setonclicklistener(listener); tbmute=(togglebutton)findviewbyid(r.id.tbmute); tbmute.setoncheckedchangelistener( new oncheckedchangelistener(){ public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) { audiomanager.setstreammute(audiomanager.stream_music, !ischecked); //设置是否静音 } }); } view.onclicklistener listener= new view.onclicklistener(){ public void onclick(view v) { @suppresswarnings ( "unused" ) button btn=(button)v; switch (v.getid()) { case r.id.btnplay: //播放音乐 mediaplayer=mediaplayer.create(audioactivity. this , r.raw.music); mediaplayer.setlooping( true ); //设置循环播放 mediaplayer.start(); //播放声音 break ; case r.id.btnupper: //增多音量 //adjuststreamvolume: 调整指定声音类型的音量 audiomanager.adjuststreamvolume(audiomanager.stream_music, audiomanager.adjust_raise, audiomanager.flag_show_ui); //调高声音 break ; case r.id.btnlower: //减少音量 //第一个参数:声音类型 //第二个参数:调整音量的方向 //第三个参数:可选的标志位 audiomanager.adjuststreamvolume(audiomanager.stream_music, audiomanager.adjust_lower, audiomanager.flag_show_ui); //调低声音 break ; } } }; } |
运行结果:
希望本文所述对大家的android程序设计有所帮助。