一、简介

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. sqlserver 2012 重启是 ID 自动增长 1000的问题

    1. Open "SQL Server Configuration Manager"2. Click "SQL Server Services" on the ...

  2. [转]十年前的老文:以 Linux 的名义

    一.灰姑娘的狂欢 今年初,林纳斯·托瓦兹承认:“如果在12年前,有人告诉我Linux会发展到今天的模样,我肯定会惊得目瞪口呆.” 托瓦兹说的是实话.1991年,这名21岁的芬兰赫尔辛基大学的学生,偶然 ...

  3. 基于游标的定位DELETE/UPDATE语句

    如果游标是可更新的(也就是说,在定义游标语句中不包括Read Only 参数),就可以用游标从游标数据的源表中DELETE/UPDATE行,即DELETE/UPDATE基于游标指针的当前位置的操作: ...

  4. 第一篇、Swift_Textkit的基本使用

    简介: iOS7 的发布给开发者的案头带来了很多新工具.其中一个就是 TextKit(文本工具箱).TextKit 由许多新的 UIKit 类组成,顾名思义,这些类就是用来处理文本的. 1.NSTex ...

  5. 解决自定义BackItem与Pop Gesture冲突的问题

    在做项目的时候遇到的这个问题, 一开始项目要求自定义导航栏返回按钮,结果发生了没法手势返回的问题,以为是需要添加拖拽手势呢,结果折腾了一下午没有实现想要的效果.接着一直百度问题,才发现跑偏了,犯了一个 ...

  6. 通过google找网站后台的方法

    转自:http://cyuyanbiancheng.blog.hexun.com/69239903_d.html site:url.com '查看这个站点上的信息最好不加www,可以查看到不少的二级域 ...

  7. 13_CXF和Spring整合发布服务

    [服务端] 第一步:建立一个Web项目 第二步:填充CXF jar包 第三步:创建接口及服务类 [工程截图(对比之前的WebService_CXF_Server00)] [applicationCon ...

  8. 由Double类型数据到数据的格式化包java.text

    需求:Double类型数据截取操作保留两位小数 解决方案: java.text.DecimalFormat df =new java.text.DecimalFormat("#.00&quo ...

  9. 替换a链接的href和title

    新项目准备验收,客户检测网页有安全隐患,说是当前网页使用“http://”有风险,指定外部链接不用“http://”怎么整…… 后来想到用JS替换字符串去操作,找了半天总算找到合用的,最终是用JQ去更 ...

  10. html5在手机端关于 map area中的自适应

    https://github.com/stowball/jQuery-rwdImageMaps用这一个插件可自适应!!!