一、简介

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. 在ASP中限制同一表单被多次提交

    本文介绍在ASP应用中防止用户在当前会话期间多次提交同一表单的一个简单方法.它主要由四个子程序组成,在较为简单的应用场合,你只要将这些代码放在包含文件中直接引用即可:对于那些较为复杂的环境,我们在文章 ...

  2. gwt-问题解决

    最近在看gwt,写了个demo,但是总是出问题,困扰了好几天,后台也没报错,但就是加载不出来 第一次编译以后是可以的,但是改了代码后就不行了,后台也没报错,google了好长时间也没出来. 于是换了个 ...

  3. asp.net 中excel 导入数据库

    protected void Button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(Sy ...

  4. javascript RegExp类型 学习小记

    在js里面,可以有两种方法来定义正则表达式,第一种是通过字变量的形式,第二种则是通过构造函数的形式: 1 字变量形式,格式是长这样子的 var expression = /pattern/flag p ...

  5. [.Net MVC] 使用 log4net 日志框架

    项目:后台管理平台 意义:项目开发中提出增加日志功能,对关键的操作.程序运行中的错误信息进行记录,这对程序部署后的调试有很大意义. 注:本文只是对网上搜集的信息进行了整合,以备今后查询. 关键字:.N ...

  6. ceilometer

    控制节点: ceilometer-api: /etc/init.d/openstack-ceilometer-api  status ceilometer-collector /etc/init.d/ ...

  7. C++中map用法

    /************************************************************************** Map的特点: 1.存储Key-value对* ...

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

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

  9. MySQL字符串类型转换时间类型

    如果MySQL数据库里面的某个时间用的是varchar(或者是char)类型的,这样可以方便系统使用而不用随便转换时间类型来适应数据库版本的不同,当要把取出的字段转换成时间类型的时候,可以按如下方法操 ...

  10. 使用WebClient上传文件并同时Post表单数据字段到服务端

    之前遇到一个问题,就是使用WebClient上传文件的同时,还要Post表单数据字段,一开始以为WebClient可以直接做到,结果发现如果先 Post表单字段,就只能获取到字段及其值,如果先上传文件 ...