首先, mp3文件複製到/res/raw夾, 名為sample.mp3.
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/play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play"
/>
<Button
android:id="@+id/continueplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Continue Play"
/>
<Button
android:id="@+id/pause"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pause"
/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop"
/>
<TextView
android:id="@+id/state"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
AndroidMediaPlayer.java
package com.AndroidMediaPlayer;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AndroidMediaPlayer extends Activity {
private Button buttonPlay, buttonContinuePlay, buttonPause, buttonStop;
private TextView textState;
private MediaPlayer mediaPlayer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonPlay = (Button)findViewById(R.id.play);
buttonContinuePlay = (Button)findViewById(R.id.continueplay);
buttonPause = (Button)findViewById(R.id.pause);
buttonStop = (Button)findViewById(R.id.stop);
textState = (TextView)findViewById(R.id.state);
mediaPlayer = new MediaPlayer();
buttonPlay.setOnClickListener(buttonPlayOnClickListener);
buttonContinuePlay.setOnClickListener(buttonContinuePlayOnClickListener);
buttonPause.setOnClickListener(buttonPauseOnClickListener);
buttonStop.setOnClickListener(buttonStopOnClickListener);
textState.setText("- idle -");
buttonPlay.setEnabled(true);
buttonContinuePlay.setEnabled(false);
buttonPause.setEnabled(false);
buttonStop.setEnabled(false);
}
Button.OnClickListener buttonPlayOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mediaPlayer = MediaPlayer.create(AndroidMediaPlayer.this, R.raw.sample);
try {
mediaPlayer.start();
textState.setText("- Playing -");
buttonPlay.setEnabled(false);
buttonContinuePlay.setEnabled(false);
buttonPause.setEnabled(true);
buttonStop.setEnabled(true);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Button.OnClickListener buttonContinuePlayOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mediaPlayer.start();
textState.setText("- Playing -");
buttonPlay.setEnabled(false);
buttonContinuePlay.setEnabled(false);
buttonPause.setEnabled(true);
buttonStop.setEnabled(true);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Button.OnClickListener buttonPauseOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mediaPlayer.pause();
textState.setText("- Pause -");
buttonPlay.setEnabled(true);
buttonContinuePlay.setEnabled(true);
buttonPause.setEnabled(false);
buttonStop.setEnabled(true);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
Button.OnClickListener buttonStopOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
mediaPlayer.stop();
textState.setText("- Stop -");
buttonPlay.setEnabled(true);
buttonContinuePlay.setEnabled(false);
buttonPause.setEnabled(false);
buttonStop.setEnabled(false);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}
沒有留言:
發佈留言