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原始版_023_MP3PLAYER003_播放mp3的更多相关文章
- ANDROID_MARS学习笔记_S01原始版_021_MP3PLAYER001_下载mp3文件
一.简介 1.在onListItemClick()中new Intent,Intent以存储序列化后的mp2Info对象作为参数,启动serivce 2.DownloadService在onStart ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER005_用广播BroacastReciever实现后台播放不更新歌词
一.代码流程1.自定义一个AppConstant.LRC_MESSAGE_ACTION字符串表示广播"更新歌词" 2.在PlayerActivity的onResume()注册Bro ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词
一.流程分析 1.点击播放按钮,会根据lrc名调用LrcProcessor的process()分析歌词文件,得到时间队列和歌词队列 2.new一个hander,把时间队列和歌词队列传给自定义的线程类U ...
- ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast
一.代码 1.xml(1)radio.xml <?xml version="1.0" encoding="utf-8"?> <LinearLa ...
- ANDROID_MARS学习笔记_S01原始版_004_TableLayout
1.xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android ...
- ANDROID_MARS学习笔记_S01原始版_003_对话框
1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest ...
- ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用
一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- ANDROID_MARS学习笔记_S01原始版_001_Intent
一.Intent简介 二.代码 1.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.co ...
- ANDROID_MARS学习笔记_S01原始版_022_MP3PLAYER002_本地及remote标签
一.简介 1.在main.xml中用TabHost.TabWidget.FrameLayout标签作布局 2.在MainActivity中生成TabHost.TabSpec,调用setIndicato ...
随机推荐
- .Net获取iis版本
有以下办法获取iis版本. DirectoryEntry getEntity = new DirectoryEntry("IIS://localhost/W3SVC/INFO"); ...
- db2相关问题及解决方法
DB2相关问题及解决方法: 一.DB2中的代码页(codepage)问题. DB2备份时发生过代码页错误的问题,修改代码页后备份正常,但创建数据库时又发生代码页的错误.这是DB2服务器使用的代码页配置 ...
- Python获取本机的mac,ip,name
Python获取mac 获取计算机名字和ip(内网ip) 指定网卡ip
- python 笔记(一)
1.Python优点 简单,优雅,明确 强大的模块第三方库 易移植 面向对角 可扩展 2.缺点 代码不能加密 执行速度慢 3.变量定义 第一个字母必须是字母表中的大小写,或下划线.不能以数字为开头. ...
- oracle里面的时间转字符串to_char(),字符串转时间to_date(),以及substr和instr的使用。
工作中编写过的一条语句 select * from Bt_Holiday where to_char(Setting_DATE,'YYYY')=Substr('2015-03-00',1,4) AND ...
- OpenJudge/Poj 1661 帮助 Jimmy
1.链接地址: bailian.openjudge.cn/practice/1661 http://poj.org/problem?id=1661 2.题目: 总Time Limit: 1000ms ...
- opensuse安装mysql-workbench
首先安装好mysqlserver,做本地测试其实不用安装mysqlclient. 到http://software.opensuse.org/package/mysql-workbench 下载相应版 ...
- VB winform自动更新 笔记
看网上各种自动更新方法,最后自己找了个比较简单的,在此做个笔记. 服务器上的共享盘里存放生成的可执行文件和XML格式的配置: <?xml version="1.0" enco ...
- html+ashx 缓存问题
最近采用html+ashx的方式做了一个项目的几个配置页面的功能,由于浏览器的缓存问题,每次更新数据提交后,页面总是不会刷新,也就是说除了第一次加载页面会向一般处理(ashx)拿数据外,其他情况都是优 ...
- 在linux下安装memcacheq
#!/bin/bash mkdir ~/build cd ~/build wget http://download.oracle.com/berkeley-db/db-5.1.19.tar.gz .t ...