效果
代码
package com.javen.devicemange.CrazyOne.content; import android.app.Service; import android.content.Context; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.CompoundButton; import android.widget.ToggleButton; import com.javen.devicemange.R; /** * Created by Administrator on 2017/2/20 0020. * 使用AudioManager控制手机音频 */ public class AudioManagerTest extends AppCompatActivity implements View.OnClickListener { private Button play; private Button up; private Button down; Context context; AudioManager audioManager; private ToggleButton mute; MediaPlayer mediaPlayer; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.audiomanagertest); initView(); this.context = this; audioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE); mute.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { //将手机指定类型的声音调整为静音 audioManager.setStreamMute(AudioManager.STREAM_MUSIC, isChecked); } }); } private void initView() { play = (Button) findViewById(R.id.play); up = (Button) findViewById(R.id.up); down = (Button) findViewById(R.id.down); play.setOnClickListener(this); up.setOnClickListener(this); down.setOnClickListener(this); mute = (ToggleButton) findViewById(R.id.mute); mute.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.play: mediaPlayer = MediaPlayer.create(this, R.raw.bomb); mediaPlayer.setLooping(true); mediaPlayer.start(); break; case R.id.up: //AudioManager.STREAM_MUSIC 手机音乐的声音 //AudioManager.FLAG_SHOW_UI 调整声音时显示音量进度条 audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); if(null!=mediaPlayer) { mediaPlayer.start(); } break; case R.id.down: audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI); if(null!=mediaPlayer) { mediaPlayer.start(); } break; case R.id.mute: break; } } }
声音文件的位置
res/raw/bomb.mp3
布局xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/play" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="play"/> <Button android:id="@+id/up" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="up"/> <Button android:id="@+id/down" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="down"/> <ToggleButton android:id="@+id/mute" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="mute"/> </LinearLayout>
。。。