通過AudioManager的adjustVolume()方法調節音量: 調節最相關聲源(stream)的音量. 例如, 如果正在通話中, 話音音量的優先權最高. 一個呼叫處於活動狀態,這將有最高的優先權,不論是否在通話屏幕顯示. 又例如, 如果不是在通話中, 而後台正在播放音樂, 音樂的音量將被調整.
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/volup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="VOL +"
/>
<Button
android:id="@+id/voldown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="VOL -"
/>
</LinearLayout>
AndroidVolume.java
package com.AndroidVolume;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AndroidVolume extends Activity {
AudioManager audioManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonVolUp = (Button)findViewById(R.id.volup);
Button buttonVolDown = (Button)findViewById(R.id.voldown);
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
buttonVolUp.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, 0);
}});
buttonVolDown.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, 0);
}});
}
}
沒有留言:
發佈留言