androidで音楽を再生してみる
res/raw ディレクトリに音楽ファイルを格納します。
ADTではファイルを格納すると、格納したファイルに対応するidが自動的にR.javaに生成されます。
再生ファイル毎にボタン割り当てるユーザインターフェースにしました。シークバーで音量を調整できるようにしてみました。
Activityのxmlです。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.test03.MusicActivity" tools:ignore="MergeRootFrame" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/music01" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/music02" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop" /> <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" android:progress="50" /> </LinearLayout> </FrameLayout>
APIはMediaPlayerを使います。
package com.example.test03; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; public class MusicActivity extends Activity implements View.OnClickListener, OnSeekBarChangeListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_music); Button button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(this); Button button2 = (Button)findViewById(R.id.button2); button2.setOnClickListener(this); Button button3 = (Button)findViewById(R.id.button3); button3.setOnClickListener(this); SeekBar seekbar = (SeekBar) findViewById(R.id.seekBar1); seekbar.setOnSeekBarChangeListener(this); } MediaPlayer mp = null; @Override public void onClick(View v) { int id = v.getId(); if (mp != null) { mp.stop(); SeekBar seekbar = (SeekBar) findViewById(R.id.seekBar1); int max = seekbar.getMax(); int progress = seekbar.getProgress(); mp.setVolume((float)progress/max, (float)progress/max); } if (id == R.id.button3) { mp = null; } if (id == R.id.button1) { mp = MediaPlayer.create(this, R.raw.music01); } if (id == R.id.button2) { mp = MediaPlayer.create(this, R.raw.music02); } if (mp != null) { mp.seekTo(0); mp.start(); } } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { int max = seekBar.getMax(); Log.d("MusicActivity", "progress=" + progress + ", max=" + max); if (mp != null) { mp.setVolume((float)progress/max, (float)progress/max); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }
嵌った点
キャストを忘れてシークバーを動かしたら音量がゼロになってしまいました。
mp.setVolume(progress/max, progress/max);