简单的android音乐播放器


在本地SD卡mnt/Music上需要有以上几首音乐。。。



package com.example.mediaplayer;


import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;


import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract.Contacts.Data;
import android.app.Activity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;


public class MPlayer extends Activity implements OnClickListener {
Button last, pause, next, stop;
ListView listview;
TextView currentname, nowTime, maxTime;
ArrayList<Map<String, String>> lists;
List<String> items, list, li;
MediaPlayer mPlayer;
Map<String, String> map;
private SeekBar seekbar;
boolean isPause = false;
int current = 0;
int maxtime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stop = (Button) findViewById(R.id.stop);
next = (Button) findViewById(R.id.next);
pause = (Button) findViewById(R.id.pause);
last = (Button) findViewById(R.id.last);
currentname = (TextView) findViewById(R.id.currentname);
nowTime = (TextView) findViewById(R.id.NowTime);
maxTime = (TextView) findViewById(R.id.MaxTime);
seekbar = (SeekBar) findViewById(R.id.seekbar);
seekbar.setOnSeekBarChangeListener(seekBarChangeListener);
stop.setOnClickListener(this);
next.setOnClickListener(this);
pause.setOnClickListener(this);
last.setOnClickListener(this);


listview = (ListView) findViewById(R.id.lv);
listview.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, getName()));
listview.setOnItemClickListener(listener);
mPlayer = new MediaPlayer();
getPath();
getData();
}


@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.stop:
mPlayer.stop();
isPause = false;
currentname.setText("");
break;
case R.id.next:
current = current + 1;
if (current >= li.size()) {
current = 0;
}
play(current);
break;
case R.id.pause:
if (mPlayer.isPlaying()) {
mPlayer.pause();
isPause = true;
} else if (!mPlayer.isPlaying() && isPause == false) {
play(0);
} else {
mPlayer.start();
isPause = false;
}


break;
case R.id.last:
current = current - 1;
if (current <= 0) {
current = li.size() - 1;
}
play(current);
break;


// case R.id.currentname:
//
// currentname.setText(list.get(current));
// break;
}
}


private OnItemClickListener listener = new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(MPlayer.this, arg2 + "-----", Toast.LENGTH_SHORT)
.show();
current = arg2;
play(current);
}


};


private void play(int id) {
// TODO Auto-generated method stub
currentname.setText(list.get(current));
mPlayer.reset();
try {
// mPlayer.setDataSource(items.get(id));
mPlayer.setDataSource(li.get(id));
mPlayer.prepare();
mPlayer.start();
   maxtime = mPlayer.getDuration();
Date date = new Date(maxtime);
SimpleDateFormat format = new SimpleDateFormat("mm:ss");
Log.d("TAG", format.format(date));
maxTime.setText(format.format(date));
getPosition();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


private List<String> getName() {
File f = new File("/mnt/sdcard/Music");
File[] files = f.listFiles();// 列出所有文件
list = new ArrayList<String>();
for (int i = 0; i < files.length; i++) {
File file = files[i];
list.add(file.getName());
// Log.d("TAG",list.get(i)+"---");
}
return list;
}


private List<String> getPath() {
File f = new File("/mnt/sdcard/Music");
File[] files = f.listFiles();// 列出所有文件
items = new ArrayList<String>();
for (int i = 0; i < files.length; i++) {
File file = files[i];
items.add(file.getAbsolutePath());
// Log.d("TAG", items.get(i) + "******");
}
return items;
}


private ArrayList<Map<String, String>> getData() {
File f = new File("/mnt/sdcard/Music");
File[] files = f.listFiles();// 列出所有文件
lists = new ArrayList<Map<String, String>>();
li = new ArrayList<String>();
for (int i = 0; i < files.length; i++) {
map = new HashMap<String, String>();
File file = files[i];
map.put("name", file.getName());
map.put("path", file.getPath());
lists.add(map);
Log.d("TAG", lists.get(i) + "---");
li.add(map.get("path"));
}
return lists;
}


private OnSeekBarChangeListener seekBarChangeListener = new OnSeekBarChangeListener() {


@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Log.d("TAG", "stop=-----");
}


@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
Log.d("TAG", "start=-----");
}


@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
}
};


public void getPosition() {
Timer time = new Timer();
TimerTask task = new TimerTask() {


@Override
public void run() {
// TODO Auto-generated method stub
int currentposition = mPlayer.getCurrentPosition();
Message msg = new Message();
msg.what = currentposition;
handle.sendMessage(msg);
}
};
time.schedule(task, 100, 1000);



}
    private Handler handle = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Log.d("TAG", "handle-----------"+msg.what+"-----"+changeDate(msg.what));
   nowTime.setText(changeDate(msg.what));
    double a =  msg.what;
    double b = maxtime;
   seekbar.setProgress((int)((a/b)*100));
}
    };
public String changeDate(int date) {
Date date1 = new Date(date);
SimpleDateFormat format = new SimpleDateFormat("mm:ss");
String time =format.format(date1);
return time;
}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值