android の音量設定

android の音量設定

今回の実行結果は
htcj

音量を設定するには
android.media.AudioManager クラスの
setStreamVolum() を使う

音量を設定する前に、まず設定可能な値の範囲を
getStreamMaxVolum() で調べる

これは設定できる最大の値が
ストリームタイプにより異なるため

ストリームタイプは
STREAM_RING
着信音

STEAM_NOTIFICATION
通知音

STEAM_ALARM
アラーム音

STEAM_MUSIC
音楽

STEAM_SYSTEM
システム音

STEAM_VOICE_CALL
ボイス音

USE_DEFAULT_STREAM_TYPE
システムが使うデフォルト音

STEAM_DTMF
DTMF音

とりあえずテストで作成
プロジェクト名は
Volcontrol

Android 4.0.3 を対象に作成
実機検証で行うのはエミュレータが遅いため

実行には Nexus 7 と HTC J

まずレイアウトファイルをつくる
res/layout の上で
右クリック > new > other

Android XML Layout File
を指定

RootElement には
LinearLayout を指定

1
2
3
4
5
6
7
8
9
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="音量設定テスト"/>
 
<TextView
    android:id="@+id/text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

を追加

次に MainActivity の変更

ActionBarActivity

Activity にして
Ctrl + shift + o でインポートを補完

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
 
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
 
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
 
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
 
/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {
 
    public PlaceholderFragment() {
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

はいらないので削除

あとは onCreate() の中に書いていく

まずtextView インスタンスを取得

1
TextView text =(TextView)findViewById(R.id.text);

次にAudioManager インスタンス取得

1
AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

そして変数に表示する内容を格納

String.format() で
第1引数には表示する文
第2引数にはその内容を記述

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
String volInfo = String.format("STREAM_ALARM: %d/%d\n"
                +"STREAM_MUSIC: %d/%d\n"
                +"STREAM_NOTIFICATION: %d/%d\n"
                +"STREAM_RING: %d/%d\n"
                +"STREAM_SYSTEM: %d/%d\n"
                +"STREAM_VOLUM_CALL: %d/%d\n",
                 
                //各ストリームタイプごとの音量の現在値と最大値を得る
                manager.getStreamVolume(AudioManager.STREAM_ALARM),
                manager.getStreamMaxVolume(AudioManager.STREAM_ALARM),
                 
                manager.getStreamVolume(AudioManager.STREAM_MUSIC),
                manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                 
                manager.getStreamVolume(AudioManager.STREAM_NOTIFICATION),
                manager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION),
                 
                manager.getStreamVolume(AudioManager.STREAM_RING),
                manager.getStreamMaxVolume(AudioManager.STREAM_RING),
                 
                manager.getStreamVolume(AudioManager.STREAM_SYSTEM),
                manager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM),
                 
                manager.getStreamVolume(AudioManager.STREAM_VOICE_CALL),
                manager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL)
                );
 
        

そして、得られた情報を textview へ表示

1
text.setText(volInfo);

メディア再生音量の最大値を得るために

1
int maxVol = manager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)

;

これは最大音量の半分にする計算に使う

media 再生音量を最大の半分にする

1
manager.setStreamVolume(AudioManager.STREAM_MUSIC, maxVol/2, AudioManager.FLAG_SHOW_UI);

AudioManager.FLAG_SHOW_UI

音量の調整をする

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です