ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3

一、简介

1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity

2.PlayerActivity通过android.media.MediaPlayer实现播放,暂停、停止

二、代码
1.xml
(1)player.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="horizontal" android:paddingLeft="10dip"
android:paddingRight="10dip" android:paddingTop="1dip"
android:paddingBottom="1dip">
<ImageButton android:id="@+id/begin" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/begin" />
<ImageButton android:id="@+id/pause" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/pause" />
<ImageButton android:id="@+id/stop" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/stop" />
</LinearLayout>

(2)AndroidManifest.xml注册activity

 <activity android:name=".PlayerActivity" android:label="@string/app_name"/>

2.java
(1)LocalMp3ListActivity.java

点击歌曲时,启动PlayerActivity

     @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Mp3Info info = infos.get(position);
Intent intent = new Intent();
intent.putExtra("mp3Info", info);
intent.setClass(this, PlayerActivity.class);
startActivity(intent);
}

(2)PlayerActivity.java

 package tony.mp3player;

 import java.io.File;

 import tony.model.Mp3Info;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton; public class PlayerActivity extends Activity { private ImageButton beginBtn = null;
private ImageButton pauseBtn = null;
private ImageButton stopBtn = null; private MediaPlayer mediaPlayer = null;
private Mp3Info info = null; private boolean isPlaying = false;
private boolean isPause = false;
private boolean isRelease = false; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
Intent intent = getIntent();
info = (Mp3Info) intent.getSerializableExtra("mp3Info");
beginBtn = (ImageButton) findViewById(R.id.begin);
pauseBtn = (ImageButton) findViewById(R.id.pause);
stopBtn = (ImageButton) findViewById(R.id.stop); beginBtn.setOnClickListener(new BeginListener());
pauseBtn.setOnClickListener(new PauseListener());
stopBtn.setOnClickListener(new StopListener());
} class BeginListener implements OnClickListener {
@Override
public void onClick(View v) {
if(!isPlaying) {
String path = getMp3Path(info.getMp3Name());
mediaPlayer = MediaPlayer.create(PlayerActivity.this, Uri.parse("file://" + path));
mediaPlayer.start();
isPlaying = true;
isRelease = false;
}
}
} class PauseListener implements OnClickListener {
@Override
public void onClick(View v) {
if(mediaPlayer != null) {
if(!isRelease){
if(!isPause) {
mediaPlayer.pause();
isPause = true;
} else {
mediaPlayer.start();
isPause = false;
}
}
}
}
} class StopListener implements OnClickListener {
@Override
public void onClick(View v) {
if(mediaPlayer != null) {
if(isPlaying) {
if(!isRelease) {
mediaPlayer.stop();
mediaPlayer.release();
isRelease = true;
isPlaying = false;
}
}
}
}
} private String getMp3Path(String mp3Name) {
String sdCartRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
String path = sdCartRoot + File.separator + "mp3" + File.separator + mp3Name;
return path;
}
}
上一篇:ANDROID_MARS学习笔记_S01原始版_020_Mp3player001_歌曲列表


下一篇:asp解码.net传参