没有合适的资源?快使用搜索试试~ 我知道了~
首页Android实现音量调节的方法
资源详情
资源评论
资源推荐

Android实现音量调节的方法实现音量调节的方法
主要介绍了Android实现音量调节的方法,涉及Android页面布局及多媒体播放的设置技巧,具有一定参考借鉴价值,
需要的朋友可以参考下
本文实例讲述了Android实现音量调节的方法。分享给大家供大家参考。具体如下:
main.xml布局文件:
<?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类:
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")














安全验证
文档复制为VIP权益,开通VIP直接复制

评论0