欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實(shí)現(xiàn)音量調(diào)節(jié)的方法

 更新時間:2015年09月26日 12:31:16   作者:Ruthless  
這篇文章主要介紹了Android實(shí)現(xiàn)音量調(diào)節(jié)的方法,涉及Android頁面布局及多媒體播放的設(shè)置技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實(shí)例講述了Android實(shí)現(xiàn)音量調(diào)節(jié)的方法。分享給大家供大家參考。具體如下:

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); //設(shè)置是否靜音
      }
    });
  }
  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);//設(shè)置循環(huán)播放
        mediaPlayer.start();//播放聲音  
        break;
      case R.id.btnUpper://增多音量
        //adjustStreamVolume: 調(diào)整指定聲音類型的音量
        audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, 
            AudioManager.ADJUST_RAISE, 
            AudioManager.FLAG_SHOW_UI);  //調(diào)高聲音
        break;
      case R.id.btnLower://減少音量
        //第一個參數(shù):聲音類型
        //第二個參數(shù):調(diào)整音量的方向
        //第三個參數(shù):可選的標(biāo)志位
        audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, 
            AudioManager.ADJUST_LOWER, 
            AudioManager.FLAG_SHOW_UI);//調(diào)低聲音
        break;
      }
    }
  };
}

運(yùn)行結(jié)果:

希望本文所述對大家的Android程序設(shè)計有所幫助。

相關(guān)文章

最新評論