音量控制和音频播放

1.MainActivity.java

  

public class MainActivity extends AppCompatActivity {
    Button play,up,down;
    ToggleButton mute;
    AudioManager aManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取系统的音频服务
        aManager = (AudioManager)getSystemService(Service.AUDIO_SERVICE);
        play = (Button)findViewById(R.id.Play);
        up = (Button)findViewById(R.id.up);
        down = (Button)findViewById(R.id.down);

        mute = (ToggleButton)findViewById(R.id.mute);
        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //初始化MediaPlayer对象 ,准备播放音乐
                MediaPlayer mPlayer = MediaPlayer.create(MainActivity.this,R.raw.music);
                //设置循环播放
                mPlayer.setLooping(true);
                //start
                mPlayer.start();
            }
        });
        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                   aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                           AudioManager.ADJUST_RAISE,AudioManager.FLAG_SHOW_UI);
            }
        });
        down.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //指定调节音乐的音频,降低音量和且显示音量图形示意
                   aManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,
                           AudioManager.ADJUST_LOWER,AudioManager.FLAG_SHOW_UI);
            }
        });

        mute.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                   aManager.setStreamMute(AudioManager.STREAM_MUSIC,isChecked);
            }
        });
        
    }
}
 
  
2.xml文件 
  activity_main.xml
  
xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sh.appaudiomanager.MainActivity">

<Button
    android:id="@+id/Play"
    android:text="play"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

  <Button
      android:id="@+id/up"
      android:text="up"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/down"
        android:text="down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

   <ToggleButton
       android:id="@+id/mute"
       android:text="mute"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"/>

LinearLayout>
 
  

你可能感兴趣的:(Android,开发)