本文主要是介绍Android AudioManager 音频管理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="shortcut.song.com.myapplication.AudioTestActivity"><Button
android:id="@+id/audio_play"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="play"/><Button
android:id="@+id/vol_up"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="V+"/><Button
android:id="@+id/vol_down"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="V-"/><ToggleButton
android:id="@+id/mute"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout>
package shortcut.song.com.myapplication;import android.app.Service;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;public class AudioTestActivity extends AppCompatActivity {Button play,up,down;ToggleButton mute;AudioManager audioManager;MediaPlayer mediaPlayer;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_audio_test);audioManager = (AudioManager) getSystemService(Service.AUDIO_SERVICE);play = (Button)findViewById(R.id.audio_play);up = (Button)findViewById(R.id.vol_up);down = (Button)findViewById(R.id.vol_down);mute = (ToggleButton)findViewById(R.id.mute);play.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// 初始化MediaPlay对象,准备播放音乐mediaPlayer = MediaPlayer.create(AudioTestActivity.this, R.raw.boot);// 设置循环mediaPlayer.setLooping(true);// 开始播放mediaPlayer.start();}});up.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// 指定调节乐音频,音量增大,显示图形意图audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);}});down.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// 指定调节乐音频,音量减小,显示图形意图audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);}});mute.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// 根据isChecked设置静音audioManager.setStreamMute(AudioManager.STREAM_MUSIC, isChecked);}});}@Overrideprotected void onDestroy() {super.onDestroy();mediaPlayer.stop();}
}
这篇关于Android AudioManager 音频管理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!