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")            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;            }        }    };    }

更多相关文章

  1. Android之布局参数
  2. Android UI 中 gravity 与 layout_gravity 参数
  3. AndroidManifest中activity属性参数设置大全
  4. RelativeLayout参数意义
  5. android横竖屏切换参数
  6. Android基础之相关参数区别
  7. android 参数含义
  8. [android]android自动化测试七之动态AVD硬件参数
  9. 【Android】常见布局xml参数详解

随机推荐

  1. [置顶] 我的Android进阶之旅------>andro
  2. Android(安卓)webkit 事件传递流程
  3. Android(安卓)Studio遇到Failed to resol
  4. Rockie's Android(安卓)Porting Guide(4)—
  5. Android(安卓)assets文件夹之位置放置和
  6. Android——多语言适配
  7. Ubuntu 10.10 编译Android(安卓)2.2
  8. Android(安卓)SnackBar
  9. Android使用腾讯X5内核替换原生webview
  10. android项目中每个文件的作用