先看看我的效果图吧

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卡中读取歌曲的更多相关文章

  1. Android BaseAdapter ListView (SD卡中文件目录显示出来)

    首先搭建activity_main.xml布局 搭建ListView中显示的布局 创建适配器 将File数据和UI适配 MainActivity中将ListView设置适配器,并设置监听 //获取SD ...

  2. Android中从SD卡中获取歌词并与歌曲同步

    先看看效果图吧,再看代码 转换文件的编码格式 package com.xm; import java.io.BufferedInputStream; import java.io.BufferedRe ...

  3. 将文件放到Android模拟器的SD卡中的两种解决方法

    两种方式:一.窗口界面操作1.打开DDMS页面2.打开File Explorer页,如果没有,在Window --> Show View -->File Explorer3.一般就在mnt ...

  4. Android模拟器使用SD卡

    在Android的应用开发中经常要用到与SD卡有关的调试,本文就是介绍关于在Android模拟器中SD卡的使用 一.      准备工作 在介绍之前首先做好准备工作,即配好android的应用开发环境 ...

  5. android 读取sd卡中的图片

    一.获取读取SD卡的权限 <!--在SDCard中创建与删除文件权限  -->    <uses-permission android:name="android.perm ...

  6. 转-Android 之 使用File类在SD卡中读取数据文件

    如果需要在程序中使用sdcard进行数据的存储,那么需要在AndroidMainfset.xml文件中 进行权限的配置: Java代码:   <!-- 在sd中创建和删除文件的权限 --> ...

  7. Android中播放本地SD卡中歌曲须要的加入的权限

    使用MediaPlayer播放本地Mp3文件时.须要注意的訪问路径的问题以及訪问权限的问题. 1.訪问路径:/storage/emulated/0 此路径即为手机的根路径,能够通过下载ES文件浏览器软 ...

  8. Android--手持PDA读取SD卡中文件

    近两年市场上很多Wince设备都开始转向Android操作系统,最近被迫使用Android开发PDA手持设备.主要功能是扫描登录,拣货,包装,发货几个功能.其中涉及到商品档的时候大概有700左右商品要 ...

  9. 【Android 界面效果30】Android中ImageSwitcher结合Gallery展示SD卡中的资源图片

    本文主要是写关于ImageSwitcher结合Gallery组件如何展示SDCard中的资源图片,相信大家都看过API Demo 中也有关于这个例子的,但API Demo 中的例子是展示工程中Draw ...

随机推荐

  1. 一次rna-seq的过程-知乎live转

    数据分析流程 来自知乎孟浩巍的“快速入门生物信息学的”Live,超棒的~ 1.数据质控 首先是质控部分,使用fastqc进行对结果分析. 对于Illumia二代测序的结果质控包括两个方面,去掉测序质量 ...

  2. Hadoop的分布式架构改进与应用

    1.  背景介绍 谈到分布式系统,就不得不提到Google的三驾马车:GFS[1],MapReduce[2]和BigTable[3]. 虽然Google没有开源这三个技术的实现源码,但是基于这三篇开源 ...

  3. Python(文件处理)

    二.基本操作 #r''------------------>> r:原生字符串,不判断符号的含义#文件处理 f=open(r’c:\a.txt’,’r’,encoding=’utf-8’) ...

  4. cdoj1328卿学姐与诡异村庄

    地址:http://acm.uestc.edu.cn/#/problem/show/1328 题目: 卿学姐与诡异村庄 Time Limit: 4500/1500MS (Java/Others)    ...

  5. Loopback接口的作用

    Loopback接口是虚拟接口,是一种纯软件性质的虚拟接口.任何送到该接口的网络数据报文都会被认为是送往设备自身的.大多数平台都支持使用这种接口来模拟真正的接口.这样做的好处是虚拟接口不会像物理接口那 ...

  6. Django- 反向生成url

    Django中提供了一个关于URL的映射的解决方案, 1.客户端的浏览器发起一个url请求,Django根据URL解析,把url中的参数捕获,调用相应的试图,获取相应的数据,然后返回给客户端显示 2. ...

  7. django---ORM之Q查询

    filter 等方法中的关键字参数查询都是一起进行“AND” 的. 如果你需要执行更复杂的查询(例如OR 语句),你可以使用Q对象 调用Q from django.db.models import Q ...

  8. 源码编译配置lnmp部署zabbix

    环境说明: [root@wcy ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@wcy ~]# uname -a Linux ...

  9. SpringBoot 事务隔离性和传播性

    propergation 传播性 Spring中七种Propagation类的事务属性详解:  REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务.这是最常见的选择.  SUPPORTS ...

  10. Jquery简单的选项卡实现

    概述 原来对jQuery用的不是很多,主要就是表单验证这些部分,最近想要更深入的学习jQuery和JavaScript编码,就找来了一些视频进行学习,然后就做了这个简单的选项卡示例.视频学习地址见最后 ...