-Android MediaPlayerがmp 3を再生した例
6176 ワード
Step 1:MediaPlayerDemoという名前のAndroidプロジェクトを新規作成します.
Step 2:素材を用意し、resの下にrawフォルダを作成し、foollove.mp 3をインポートし、プレイします.png,pause.png,stop.pngはres/drawableフォルダの下にインポートする.
Step 3:UIレイアウトを設計し、main.xmlにImageButtonを3つ入れます(ここではAbsoluteLayout、またはRelativeLayoutで実現できますが、後者を使います).コードは次のとおりです.
Step 4:メインコントロールプログラムMediaPlayerDemo.JAvaの実装、コードは以下の通りです.
Step 2:素材を用意し、resの下にrawフォルダを作成し、foollove.mp 3をインポートし、プレイします.png,pause.png,stop.pngはres/drawableフォルダの下にインポートする.
Step 3:UIレイアウトを設計し、main.xmlにImageButtonを3つ入れます(ここではAbsoluteLayout、またはRelativeLayoutで実現できますが、後者を使います).コードは次のとおりです.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageButton
android:id="@+id/myButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/pause"
android:layout_alignTop="@+id/myButton1"
android:layout_toRightOf="@+id/myButton1"
>
</ImageButton>
<ImageButton
android:id="@+id/myButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop"
android:layout_alignTop="@+id/myButton1"
android:layout_toRightOf="@+id/myButton3"
>
</ImageButton>
<TextView
android:id="@+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="104dp"
android:layout_marginTop="32dp"
android:text="@string/hello_world" />
<ImageButton
android:id="@+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="28dp"
android:src="@drawable/play" />
</RelativeLayout>
Step 4:メインコントロールプログラムMediaPlayerDemo.JAvaの実装、コードは以下の通りです.
package com.example.mymusic;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity {
private ImageButton mb1,mb2,mb3;
private TextView tv;
private MediaPlayer mp;
// , false
private boolean isPaused = false;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// findViewById
mb1 = (ImageButton)findViewById(R.id.myButton1);
mb2 = (ImageButton)findViewById(R.id.myButton2);
mb3 = (ImageButton)findViewById(R.id.myButton3);
tv = (TextView)findViewById(R.id.myTextView1);
// MediaPlayer , raw lovefool.mp3
mp = MediaPlayer.create(this,R.raw.putao);
//
mb1.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
try {
if(mp != null)
/* {
mp.stop();
}
mp.prepare(); */
mp.start();
tv.setText(" ...");
} catch (Exception e) {
tv.setText(" ...");
e.printStackTrace();
}
}
});
//
mb2.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
try {
if(mp !=null)
{
mp.stop();
tv.setText(" ...");
}
} catch (Exception e) {
tv.setText(" ...");
e.printStackTrace();
}
}
});
//
//
mb3.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
try {
if(mp !=null)
{
if(isPaused==false)
{
mp.pause();
isPaused=true;
tv.setText(" !");
}
else if(isPaused==true)
{
mp.start();
isPaused = false;
tv.setText(" !");
}
}
} catch (Exception e) {
tv.setText(" ...");
e.printStackTrace();
}
}
});
/* MediaPlayer.OnCompletionLister Listener */
mp.setOnCompletionListener(
new MediaPlayer.OnCompletionListener()
{
// @Override
/* */
public void onCompletion(MediaPlayer arg0)
{
try
{
/* MediaPlayer
* */
mp.release();
/* TextView */
tv.setText(" !");
}
catch (Exception e)
{
tv.setText(e.toString());
e.printStackTrace();
}
}
});
/* MediaPlayer.OnErrorListener Listener */
mp.setOnErrorListener(new MediaPlayer.OnErrorListener()
{
@Override
/* */
public boolean onError(MediaPlayer arg0, int arg1, int arg2)
{
// TODO Auto-generated method stub
try
{
/* MediaPlayer */
mp.release();
tv.setText(" !");
}
catch (Exception e)
{
tv.setText(e.toString());
e.printStackTrace();
}
return false;
}
});
}
}