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. html5 video自动播放-Android、IOS video自动播放-移动端video自
  3. Android(安卓)音频播放
  4. Media Player of Android
  5. 动画(补间动画)的基础知识
  6. [Android] Android(安卓)视频播放总结
  7. Android(安卓)各种音量的获取和设置
  8. android 静音方法
  9. android播放视频(三)利用原生的MediaPlayer+SurfaceView之提高

随机推荐

  1. 腾讯暑假实习面试总结
  2. android修改虚拟内存(方法)
  3. android 借助AccessibilityService实现模
  4. 阿里Android开发规范:四大基本组件
  5. 新装Android(安卓)studio无法联网,总是报
  6. Android Studio解决未识别Java文件(出现红
  7. Android Metro风格的Launcher开发系列第
  8. Android配置Scheme使用浏览器唤起APP的方
  9. Android多媒体学习五:利用Service实现背景
  10. ADB通过WIFI连接Android设备