Android中从SD卡中读取歌曲
先看看我的效果图吧

Activity类
private TextView nameTextView;
private SeekBar seekBar;
private ListView listView;
private List<Map<String, String>> data;
private int current;
private MediaPlayer player;
private Handler handler = new Handler();
private Button ppButton;
private boolean isPause;
private boolean isStartTrackingTouch;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainb);
ib3=(ImageButton) findViewById(R.id.ib3);
ib3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent it=new Intent(Music_b.this,Music_gc.class);
String name=data.get(current).get("name");
it.putExtra("name", name);
startActivityForResult(it, 1);
}
});
tv=(TextView)findViewById(R.id.tvbb);
sp=(Spinner) findViewById(R.id.sp);
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View view,
int arg2, long arg3) {
TextView tv1=(TextView)view;
String str=tv1.getText().toString();
tv.setText(str);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
sp1=(Spinner) findViewById(R.id.sp1);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() { @Override
public void onItemSelected(AdapterView<?> arg0, View view,
int arg2, long arg3) {
TextView tv1=(TextView)view;
String str=tv1.getText().toString();
tv.setText(str);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) { }
});
nameTextView = (TextView) findViewById(R.id.name);
seekBar = (SeekBar) findViewById(R.id.seekBar);
listView = (ListView) findViewById(R.id.list);
ppButton = (Button) findViewById(R.id.pp);
//创建一个音乐播放器
player = new MediaPlayer();
//显示音乐播放器
generateListView();
//进度条监听器
seekBar.setOnSeekBarChangeListener(new MySeekBarListener());
//播放器监听器
player.setOnCompletionListener(new MyPlayerListener());
//意图过滤器
IntentFilter filter = new IntentFilter();
}
private final class MyPlayerListener implements OnCompletionListener {
//歌曲播放完后自动播放下一首歌区
public void onCompletion(MediaPlayer mp) {
next();
}
}
public void next(View view) {
next();
}
public void previous(View view) {
previous();
}
private void previous() {
current = current - 1 < 0 ? data.size() - 1 : current - 1;
play();
}
private void next() {
current = (current + 1) % data.size();
play();
}
private final class MySeekBarListener implements OnSeekBarChangeListener {
//移动触发
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
//起始触发
public void onStartTrackingTouch(SeekBar seekBar) {
isStartTrackingTouch = true;
}
//结束触发
public void onStopTrackingTouch(SeekBar seekBar) {
player.seekTo(seekBar.getProgress());
isStartTrackingTouch = false;
}
}
private void generateListView() {
List<File> list = new ArrayList<File>();
//获取sdcard中的所有歌曲
findAll(Environment.getExternalStorageDirectory(), list);
//播放列表进行排序,字符顺序
Collections.sort(list);
data = new ArrayList<Map<String, String>>();
for (File file : list) {
Map<String, String> map = new HashMap<String, String>();
map.put("name", file.getName());
map.put("path", file.getAbsolutePath());
data.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.mainmp3_item, new String[] { "name" }, new int[] { R.id.mName });
listView.setAdapter(adapter);
listView.setOnItemClickListener(new MyItemListener());
}
private final class MyItemListener implements OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
current = position;
play();
}
}
private void play() {
try {
//重播
player.reset();
//获取歌曲路径
player.setDataSource(data.get(current).get("path"));
//缓冲
player.prepare();
//开始播放
player.start();
//显示歌名
nameTextView.setText(data.get(current).get("name"));
//设置进度条长度
seekBar.setMax(player.getDuration());
//播放按钮样式
ppButton.setText("||");
//发送一个Runnable,handler收到之后就会执行run()方法
handler.post(new Runnable() {
public void run() {
// 更新进度条状态
if (!isStartTrackingTouch)
seekBar.setProgress(player.getCurrentPosition());
// 1秒之后再次发送
handler.postDelayed(this, 1000);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void findAll(File file, List<File> list) {
File[] subFiles = file.listFiles();
if (subFiles != null)
for (File subFile : subFiles) {
if (subFile.isFile() && subFile.getName().endsWith(".mp3"))
list.add(subFile);
else if (subFile.isDirectory())//如果是目录
findAll(subFile, list); //递归
}
}
public void pp(View view) {
//默认从第一首歌开始播放
if (!player.isPlaying() && !isPause) {
play();
return;
}
Button button = (Button) view;
//暂停/播放按钮
if ("||".equals(button.getText())) {
pause();
button.setText("|>");
}else if("|>".equals(button.getText())) {
resume();
button.setText("||");
}
}
private void resume() {
if (isPause) {
player.start();
isPause = false;
}
}
private void pause() {
if (player != null && player.isPlaying()) {
player.pause();
isPause = true;
}
}
}
示例代码
xml类
<TableLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
</TableRow>
</TableLayout>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50px"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageButton
android:id="@+id/ib3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/b"
android:background="#00000000"
android:layout_alignParentBottom="true"
/>
<SeekBar
android:id="@+id/seekBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="88dip"
android:progressDrawable="@drawable/seekbar"
android:thumb="@drawable/thumb"
android:layout_alignTop="@id/ib3"
android:max="100"
android:maxHeight="2.0dip"
android:minHeight="2.0dip"
/>
<Button
android:id="@+id/bt021"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|▶"
android:onClick="previous"
android:layout_toRightOf="@id/ib3"
android:background="#00000000"
android:layout_marginRight="30px"
android:paddingLeft="20dip"
android:layout_alignParentBottom="true"
/>
<Button
android:id="@+id/pp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="▶"
android:onClick="pp"
android:layout_toRightOf="@id/bt021"
android:background="#00000000"
android:layout_marginRight="30px"
android:layout_alignParentBottom="true"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="▶|"
android:onClick="next"
android:layout_toRightOf="@id/pp"
android:background="#00000000"
android:layout_marginTop="20dip"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
示例代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
>
<TableLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<TextView
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</TableRow>
</TableLayout> </LinearLayout>
第二个xml
Android中从SD卡中读取歌曲的更多相关文章
- Android BaseAdapter ListView (SD卡中文件目录显示出来)
首先搭建activity_main.xml布局 搭建ListView中显示的布局 创建适配器 将File数据和UI适配 MainActivity中将ListView设置适配器,并设置监听 //获取SD ...
- Android中从SD卡中获取歌词并与歌曲同步
先看看效果图吧,再看代码 转换文件的编码格式 package com.xm; import java.io.BufferedInputStream; import java.io.BufferedRe ...
- 将文件放到Android模拟器的SD卡中的两种解决方法
两种方式:一.窗口界面操作1.打开DDMS页面2.打开File Explorer页,如果没有,在Window --> Show View -->File Explorer3.一般就在mnt ...
- Android模拟器使用SD卡
在Android的应用开发中经常要用到与SD卡有关的调试,本文就是介绍关于在Android模拟器中SD卡的使用 一. 准备工作 在介绍之前首先做好准备工作,即配好android的应用开发环境 ...
- android 读取sd卡中的图片
一.获取读取SD卡的权限 <!--在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.perm ...
- 转-Android 之 使用File类在SD卡中读取数据文件
如果需要在程序中使用sdcard进行数据的存储,那么需要在AndroidMainfset.xml文件中 进行权限的配置: Java代码: <!-- 在sd中创建和删除文件的权限 --> ...
- Android中播放本地SD卡中歌曲须要的加入的权限
使用MediaPlayer播放本地Mp3文件时.须要注意的訪问路径的问题以及訪问权限的问题. 1.訪问路径:/storage/emulated/0 此路径即为手机的根路径,能够通过下载ES文件浏览器软 ...
- Android--手持PDA读取SD卡中文件
近两年市场上很多Wince设备都开始转向Android操作系统,最近被迫使用Android开发PDA手持设备.主要功能是扫描登录,拣货,包装,发货几个功能.其中涉及到商品档的时候大概有700左右商品要 ...
- 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片
本文主要是写关于ImageSwitcher结合Gallery组件如何展示SDCard中的资源图片,相信大家都看过API Demo 中也有关于这个例子的,但API Demo 中的例子是展示工程中Draw ...
随机推荐
- Python(调用函数、定义函数)
函数的返回值: return 值:只能返回一次,只要执行return函数就终止 返回值:没有类型限制,也没有个数限制 没有return:None 返回一个值 返回多个值:元组 先定义,后使用,定义阶段 ...
- 终端创建scrapy项目时报错(转)
在终端创建scrapy项目时报错 PS D:\scrapy_project> scrapy startproject fangFatal error in launcher: Unable to ...
- redhat 9.0安装完不能上网解决办法(补) - 并附上redhat9.0 下载链接
昨天一位网友Q我,说我的开发环境搭建教程按步骤最后上不了网怎么解决我才突然想起9.0版本在VM7,8中存在问题,于是今天我就简单说下解决的方法. 由于本人习惯使用redhat 9.0版本所以到现在还是 ...
- springmvc pojo
/** * Spring MVC 会按请求参数名和 POJO 属性名进行自动匹配, 自动为该对象填充属性值.支持级联属性. * 如:dept.deptId.dept.address.tel 等 */ ...
- ActionScript和js交互
新建的ActionScript项目,默认新建会在“默认包”中创建一个和项目名称相同以as结尾的文件,as项目开始执行时要new一个这样的类在类上方加入一些参数可以为生成的swf初始化一些样式 [SWF ...
- nginx反向代理服务器端口问题
nginx可以很方便的配置成反向代理服务器 server { listen 80; server_name bothlog.com; location / { proxy_set_header H ...
- Java JDBC概要总结一(基本操作和SQL注入问题)
JDBC定义: JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API.JDBC是Java访问数据库的标准规范,可以为不同的关系 ...
- 使用net.sf.json包提供的JSONObject.toBean方法时,日期转化错误解决办法
解决办法: 需要在toBean之前添加配置 String[] dateFormats = new String[] {"yyyy-MM-dd HH:mm:ss"}; JSONUti ...
- 求逆元 HDU 2516
A/B Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Python中有趣的数据结构
链表 链表的基本操作 >>> a = [66.25,333,333,1,1234.5] >>> print a.count(333),a.count(66.25), ...