package com.example.video;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity {
private MyHandler handler;
private SeekBar seek;
private TextView time;
// 定时器
private Timer timer;
private String path;
private ListView listview;
private ArrayList list;
// 当前播放歌曲的索引
private int currentListItem = 0;
private ImageButton start;
private ImageButton stop;
private ImageButton big;
private ImageButton small;
private ImageButton xunhuan;
private ImageButton shunxu;
private ImageButton shang;
private ImageButton xia;
MediaPlayer mp;
// 声音处理类
private AudioManager am;
private boolean start_flag = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 实例一个声音处理类
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
start = (ImageButton) this.findViewById(R.id.start);
stop = (ImageButton) this.findViewById(R.id.stop);
big = (ImageButton) this.findViewById(R.id.da);
small = (ImageButton) this.findViewById(R.id.xiao);
xunhuan = (ImageButton) this.findViewById(R.id.xunhuan);
shunxu = (ImageButton) this.findViewById(R.id.shunxu);
shang = (ImageButton) this.findViewById(R.id.shang);
xia = (ImageButton) this.findViewById(R.id.xia);
listview = (ListView) this.findViewById(R.id.listView);
seek = (SeekBar) this.findViewById(R.id.seek);
time = (TextView) this.findViewById(R.id.sj);
myOnclick mo = new myOnclick();
xunhuan.setOnClickListener(mo);
shunxu.setOnClickListener(mo);
start.setOnClickListener(mo);
stop.setOnClickListener(mo);
big.setOnClickListener(mo);
small.setOnClickListener(mo);
shang.setOnClickListener(mo);
xia.setOnClickListener(mo);
handler = new MyHandler();
list = new ArrayList();
// 读取/mnt/sdcard/music/路径下的文件
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// File path = Environment.getExternalStorageDirectory();// 获得SD卡路径
File path = new File("/mnt/sdcard/music/");
File[] files = path.listFiles(); // 读取
getFileName(files);
}
// 将读取到的文件名显示在textview上
SimpleAdapter adapter = new SimpleAdapter(this, list,
R.layout.lv_item1, new String[] { "Name" },
new int[] { R.id.text });
for (int i = 0; i < list.size(); i++) {
Log.i("zeng", "list. name: " + list.get(i));
}
// 将list列表显示在listview上
listview.setAdapter(adapter);
mp = new MediaPlayer();
mysetmp ms = new mysetmp();
mp.setOnCompletionListener(ms);
timer = new Timer();
// 定时器来控制seekbar的位置
myTimerTask tt = new myTimerTask();
timer.schedule(tt, 100, 10);
// seekbar的监听处理
mySeekBarOn sb = new mySeekBarOn();
seek.setOnSeekBarChangeListener(sb);
}
// 播放音乐
public void mpplay() {
mp.reset();
//path = "/mnt/sdcard/mucic/你的香气 claire.mp3";
// 设置输出音频流
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mp.setDataSource(list.get(currentListItem) + "");
mp.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
private int getseekBarPosition() {
if (mp.isPlaying()) {
// TODO Auto-generated method stub
// 得到视频当前播放的长度
int position = mp.getCurrentPosition();
// 得到视频总的长度
int fileLength = mp.getDuration();
// 得到seekbar的总的长度
int seekBarLength = seek.getMax();
if (fileLength != 0) {
return (seekBarLength * position) / fileLength;
}
return 0;
}
return 0;
}
public String getBoFangShiJian() {
if (mp.getDuration() != 0) {
//当前播放时间
int dangquan = mp.getCurrentPosition();
int h = (dangquan / 1000) / 3600;
int m = (dangquan / 1000) % 3600 / 60;
int s = (dangquan / 1000) % 60;
//文件总时长
int zhong = mp.getDuration();
int zh = (zhong / 1000) / 3600;
int mh = (zhong / 1000) % 3600 / 60;
int sh = (zhong / 1000) % 60;
String bfsj = h + ":" + m + ":" + s + "/" + zh + ":" + mh + ":"
+ sh;
return bfsj;
}
return null;
}
private void getFileName(File[] files) {
if (files != null) { // 先判断目录是否为空,否则会报空指针
for (File file : files) {
if (file.isDirectory()) {
getFileName(file.listFiles());
} else {
String fileName = file.getName();
//读取指定目录下的mp3文件
if (fileName.endsWith(".mp3")) {
//将所有的mp3文件存储在HashMap里
HashMap map = new HashMap();
String s = fileName.substring(0,
fileName.lastIndexOf(".")).toString();
map.put("Name", fileName.substring(0,
fileName.lastIndexOf(".")));
list.add(map);
}
}
}
}
}
// 当选择列表项时播放音乐
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
currentListItem = position;
mpplay();
}
// 监听返回键,实现对话框功能退出
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("")
.setMessage(R.string.quit)
.setNegativeButton(R.string.no,
//按下取消执行空
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
})
.setPositiveButton(R.string.ok,
//按下确定退出
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
dialog.dismiss();
android.os.Process
.killProcess(android.os.Process
.myPid());
}
}).show();
return true;
} else {
// return context.onKeyDown(keyCode, event);
return false;
}
}
class mysetmp implements OnCompletionListener {
@Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
currentListItem++;
if (++currentListItem >= list.size()) {
currentListItem = 0;
}
mpplay();
mp.start();
}
}
// 通过TimerTask方法让handler不断传送数据
class myTimerTask extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
Message msm = new Message();
handler.sendMessage(msm);
}
}
// 让多媒体文件播放时不断更新时间和进度
class MyHandler extends Handler {
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
seek.setProgress(getseekBarPosition());
time.setText(getBoFangShiJian() + "");
}
}
// 创建一个seekbar 的监听
class mySeekBarOn implements OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
// 判断是否是人为操作 如果是优先考虑
if (fromUser) {
int i = seek.getMax();
int i2 = (mp.getDuration() * progress) / i;
// seekTo 这个方法添加改变
mp.seekTo(i2);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
//监听按钮
class myOnclick implements View.OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
//播放
if (start_flag) {
Toast.makeText(MainActivity.this, R.string.start,
Toast.LENGTH_SHORT).show();
mp.start();
start.setImageResource(R.drawable.stop);
start_flag = false;
} else {
//暂停
Toast.makeText(MainActivity.this, R.string.stop,
Toast.LENGTH_SHORT).show();
mp.pause();
start.setImageResource(R.drawable.kuaijin);
start_flag = true;
}
break;
//停止
case R.id.stop:
Toast.makeText(MainActivity.this, R.string.end,
Toast.LENGTH_SHORT).show();
start.setImageResource(R.drawable.kuaijin);
start_flag = true;
mp.stop();
break;
//增大音量
case R.id.da:
am.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
break;
//减小音量
case R.id.xiao:
am.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
Log.e("================", AudioManager.ADJUST_LOWER + "");
break;
//循环播放
case R.id.xunhuan:
Toast.makeText(MainActivity.this, R.string.xunhuan,
Toast.LENGTH_SHORT).show();
mp.setLooping(true);
break;
//顺序播放
case R.id.shunxu:
Toast.makeText(MainActivity.this, R.string.shunxu,
Toast.LENGTH_SHORT).show();
mp.setLooping(false);
break;
//上一曲
case R.id.shang:
Toast.makeText(MainActivity.this, R.string.up,
Toast.LENGTH_SHORT).show();
mp.stop();
if (currentListItem != 0) {
if (--currentListItem >= 0) {
currentListItem = list.size();
} else {
mpplay();
}
} else {
mpplay();
}
mp.start();
break;
//下一曲
case R.id.xia:
Toast.makeText(MainActivity.this, R.string.next,
Toast.LENGTH_SHORT).show();
mp.stop();
if (++currentListItem >= list.size()) {
currentListItem = 0;
}
currentListItem++;
mpplay();
mp.start();
break;
}
}
}
}
6 个解决方案
#1
看下log 文件呢
#2
log列表各种报错
#3
log列表各种报错 用debug又看不明白
#4
#5
关了防火墙看看
#6
mp.setDataSource(list.get(currentListItem) + "");
mp.prepare();后面没有mp.start();
mp.prepare();后面没有mp.start();
#1
看下log 文件呢
#2
log列表各种报错
#3
log列表各种报错 用debug又看不明白
#4
#5
关了防火墙看看
#6
mp.setDataSource(list.get(currentListItem) + "");
mp.prepare();后面没有mp.start();
mp.prepare();后面没有mp.start();