一、简介

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原始版_023_MP3PLAYER003_播放mp3的更多相关文章

  1. ANDROID_MARS学习笔记_S01原始版_021_MP3PLAYER001_下载mp3文件

    一.简介 1.在onListItemClick()中new Intent,Intent以存储序列化后的mp2Info对象作为参数,启动serivce 2.DownloadService在onStart ...

  2. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER005_用广播BroacastReciever实现后台播放不更新歌词

    一.代码流程1.自定义一个AppConstant.LRC_MESSAGE_ACTION字符串表示广播"更新歌词" 2.在PlayerActivity的onResume()注册Bro ...

  3. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词

    一.流程分析 1.点击播放按钮,会根据lrc名调用LrcProcessor的process()分析歌词文件,得到时间队列和歌词队列 2.new一个hander,把时间队列和歌词队列传给自定义的线程类U ...

  4. ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast

    一.代码 1.xml(1)radio.xml <?xml version="1.0" encoding="utf-8"?> <LinearLa ...

  5. ANDROID_MARS学习笔记_S01原始版_004_TableLayout

    1.xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android ...

  6. ANDROID_MARS学习笔记_S01原始版_003_对话框

    1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest ...

  7. ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用

    一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...

  8. ANDROID_MARS学习笔记_S01原始版_001_Intent

    一.Intent简介 二.代码 1.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.co ...

  9. ANDROID_MARS学习笔记_S01原始版_022_MP3PLAYER002_本地及remote标签

    一.简介 1.在main.xml中用TabHost.TabWidget.FrameLayout标签作布局 2.在MainActivity中生成TabHost.TabSpec,调用setIndicato ...

随机推荐

  1. C#.net在后台执行javascript

    string script = string.Format("<script type='text/javascript' >alert('{0}');</script&g ...

  2. Opencv——将摄像头拍摄写成视频文件

    这里主要利用了Opencv打开摄像头的代码,以及写入视频的函数,只是这里要注意的是摄像头好像没有帧率,在cvCreateVideoWriter,时要自己设置 #include"cv.h&qu ...

  3. 初学android:四大组件之contentprovider

    一.ContentProvider的概念ContentProvider:为存储和获取数据提供统一的接口.可以在不同的应用程序之间共享数据.Android已经为常见的一些数据提供了默认的ContentP ...

  4. linux shell if参数

    shell 编程中使用到得if语句内判断参数 –b 当file存在并且是块文件时返回真 -c 当file存在并且是字符文件时返回真 -d 当pathname存在并且是一个目录时返回真 -e 当path ...

  5. 线程间通信--生产者消费者 升级版JDK5

    import java.util.concurrent.locks.*; /*1.新的解锁,上锁操作,据说是jdk5.0升级版,以前的枷锁,解锁都是隐藏的,默认的,现在变成显式 2.新的异常处理方式  ...

  6. [Effective Objective-C 读书笔记] 第1章 几条基本写法 (2~5条)

    我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3575599.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...

  7. CentOS 开启GD库

    在php.ini 中没有找到"extension=php_gd2.dll"这行代码,这是因为CentOS一般没有预装GD库. 解决办法: 1.在线安装GD库 yum -y inst ...

  8. 使用Notepad++将多行数据合并成一行

    1.按Ctrl+F,弹出“替换”的窗口: 2.选择“替换”菜单: 3.“查找目标”内容输入为:\r\n: 4.“替换为”内容为空: 5.“查找模式”选择为正则表达式: 6.设置好之后,点击“全部替换” ...

  9. PL/SQL Developer不安装客户端连接远程oracle数据库(转)

    1.下载敏捷客户端InstantClient(40M左右,选择适合自己的下载,我的是instantclient-basic-win32-11.2.0.1.0.zip ):http://www.orac ...

  10. VPN client on linux debian

    Install the pptp-linux and pptp-linux-client: sudo apt-get install pptp-linux pptp-linux-client Crea ...