public class PlayAudio extends Activity {
private MediaPlayer mPlayer;
private Button bplay,bpause,bstop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bplay = (Button)findViewById(R.id.play);
bpause = (Button)findViewById(R.id.pause);
bstop = (Button)findViewById(R.id.stop);
bplay.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
if (mPlayer != null) {
try {
mPlayer = MediaPlayer.create(PlayAudio.this, R.raw.beethoven);
}catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
// 再生開始
mPlayer.start();
}
}
});
bpause.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO 自動生成されたメソッド・スタブ
if(mPlayer != null){
mPlayer.pause();
}
}
});
bstop.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO 自動生成されたメソッド・スタブ
if(mPlayer != null){
mPlayer.stop();
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
// 使わなくなった時点でMediaPlayerに結びつけられているリソースを解放する
mPlayer.release();
}
}
2.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="オーディオ再生" />
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/play" />
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/pause" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/stop" />
</LinearLayout>
0 件のコメント:
コメントを投稿