2012/12/04

Android ProgressBar結合AudioManager

這個程式就是將當前鈴聲的音量結合ProgressBar的圖形做變化,寫給學長的





Code:
package com.cyfang.audio;

import android.app.Activity;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class AudioMain extends Activity {
    private static ProgressBar bar = null;
    private static Button btn_Add = null;
    private static Button btn_Minus = null;
    private static AudioManager audioManager = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio_main);
        init();
    }

    private final void init() {
        // 初始物件
        bar = (ProgressBar) findViewById(R.id.progressBar1);
        btn_Add = (Button) findViewById(R.id.btn_Add);
        btn_Minus = (Button) findViewById(R.id.btn_Minus);
        audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        // 設定物件動作
        bar.setVisibility(View.VISIBLE);
        bar.setMax(7);
        bar.setProgress(getVolume());
        btn_Add.setOnClickListener(clickListener);
        btn_Minus.setOnClickListener(clickListener);
        //
    }

    private Button.OnClickListener clickListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.btn_Add:
                setAudio(0);
                break;
            case R.id.btn_Minus:
                setAudio(1);
                break;
            }
        }
    };

    private static int getVolume() {
        return audioManager.getStreamVolume(AudioManager.STREAM_RING); // 取得目前鈴聲音量
    }

    private final void setAudio(int flag) {
        if (flag == 0) { // Max
            audioManager.adjustVolume(AudioManager.STREAM_SYSTEM, 0); // 放大鈴聲音量
        } else if (flag == 1) { // Min
            audioManager.adjustVolume(AudioManager.ADJUST_LOWER, 0); // 減少鈴聲音量
        }
        Log.i("Tag", getVolume() + "");
        bar.setProgress(getVolume());
    }
}


XML:
<RelativeLayout 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" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/btn_Add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="190dp"
        android:layout_marginLeft="66dp"
        android:text="+" />

    <Button
        android:id="@+id/btn_Minus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btn_Add"
        android:layout_alignBottom="@+id/btn_Add"
        android:layout_marginLeft="69dp"
        android:layout_toRightOf="@+id/btn_Add"
        android:text="-" />

</RelativeLayout>


參考資料:
http://blog.csdn.net/Android_Tutor/article/details/5695170
http://android.tgbus.com/Android/tutorial/201105/352830.shtml