先看看我的效果图吧

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. 使用npm构建前端项目基本流程

    现在各种前端框架, 库文件基本都托管到npm上, 我们平常下载到别人的项目文件, 也基本是用npm 构建的, 不了解点node和npm那是寸步难行. 下面介绍的代码示例不敢说是最佳实践, 但都是我亲自 ...

  2. pandas(四)唯一值、值计数以及成员资格

    针对Series对象,从中抽取信息 unique可以得到Series对象的唯一值数组 >>> obj = Series(['c','a','d','a','a','b','b','c ...

  3. Mark一下 mysql 误删除root用户的解决方法

    今天学习mysql用户管理,不小心将mysql.user表中的root用户给删掉了,然后就无法登录mysql了,网上找到了linux下的解决方法,我做了简单的修改,改成了我的windows版,恢复方法 ...

  4. beego——session控制

    beego内置了session模块,目前session模块支持的后端引擎包括memory.cookie.file.mysql.redis.couchbase.memcache.postgres, 用户 ...

  5. js 添加css属性

    $(".active").css('border','1px solid #ddd')curLi.css('border','2px solid red')curLi.css('b ...

  6. 自己写的一个简单PHP采集器

    自己写的一个简单PHP采集器 <?php //**************************************************************** $url = &q ...

  7. bootstrap datatable 参考文档

    start:http://bootstrap-table.wenzhixin.net.cn/zh-cn/getting-started/ 扩展  http://issues.wenzhixin.net ...

  8. 【转】PCA与Whitening

    PCA: PCA的具有2个功能,一是维数约简(可以加快算法的训练速度,减小内存消耗等),一是数据的可视化. PCA并不是线性回归,因为线性回归是保证得到的函数是y值方面误差最小,而PCA是保证得到的函 ...

  9. linux:查看磁盘硬件信息hdparm,smartctl

    smartctl 命令 这个一个用于控制和监控支持smart技术的硬盘的命令.通常配合 -a 选项我们可以查看到比较详尽的硬盘信息(比如序列号.硬盘容量.已运行时间.硬盘健康状况等).用法如下: sm ...

  10. WINDOWS和UNIX换行符的理解

    # WINDOWS和UNIX换行符的理解 **file1.txt**17.143.161.37   其他    美国54.163.255.40   其他    美国 弗吉尼亚州 亚马逊公司 **[ro ...