package com.king.android.media;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.king.android.R;
public class MusicBActivity extends Activity implements OnClickListener{
public static final String TAG="MusicActivity";
static List<File> allFiles = new ArrayList<File>();
private int mCurrentJMusic = 0; // 当前播放的音乐
private MediaPlayer mMediaPlayer = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
refreshFile(getSDCardPath(),".mp3");
if(allFiles.size()<=0){
Toast.makeText(MusicBActivity.this, "没有歌曲", Toast.LENGTH_SHORT).show();
finish();
}
mMediaPlayer = new MediaPlayer();
initView();
}
public String getSDCardPath(){
String sdPath = null;
boolean hasCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if(hasCard){
sdPath = Environment.getExternalStorageDirectory().toString();
}else{
sdPath = Environment.getDataDirectory().toString();
}
return sdPath;
}
private void initView(){
setContentView(R.layout.media_layout);
Button btnMedia = (Button) findViewById(R.id.btn_last);
btnMedia.setOnClickListener(this);
btnMedia = (Button) findViewById(R.id.btn_pause);
btnMedia.setOnClickListener(this);
btnMedia = (Button) findViewById(R.id.btn_stop);
btnMedia.setOnClickListener(this);
btnMedia = (Button) findViewById(R.id.btn_play);
btnMedia.setOnClickListener(this);
btnMedia = (Button) findViewById(R.id.btn_next);
btnMedia.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_play:
playMusic(allFiles.get(mCurrentJMusic));
break;
case R.id.btn_next:
nextMusic();
break;
case R.id.btn_last:
FrontMusic();
break;
case R.id.btn_pause:
if(mMediaPlayer.isPlaying()){
//暂停
mMediaPlayer.pause();
}else{
//播放
mMediaPlayer.start();
}
break;
case R.id.btn_stop:
//是否在播放
if(mMediaPlayer.isPlaying()){
//重置MediaPlayer到初始状态
mMediaPlayer.reset();
}
break;
}
}
private void playMusic(File file){
try{
//重置MediaPlayer
mMediaPlayer.reset();
//设置播放文件 的路径
//mMediaPlayer.setDataSource(file.getAbsolutePath()+File.separator+file.getName());
mMediaPlayer.setDataSource(file.getAbsolutePath());
//播放文件
mMediaPlayer.prepare();
//开始播放
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//播放完成后,进行下一首。
nextMusic();
}
});
}catch(Exception e){
e.printStackTrace();
}
}
//下一首
private void nextMusic(){
if(++mCurrentJMusic>=allFiles.size()){
mCurrentJMusic = 0;
}else{
playMusic(allFiles.get(mCurrentJMusic));
}
}
//上一首
private void FrontMusic(){
if(--mCurrentJMusic>=0){
mCurrentJMusic = allFiles.size();
}else{
playMusic(allFiles.get(mCurrentJMusic));
}
}
/** public void refreshFile(String path,String fileType){
File file = new File(path);
File[] files = file.listFiles();
if(null==files)
return;
for(int i = 0;i<files.length;i++){
if(files[i].isDirectory()){
refreshFile(files[i].getAbsolutePath(),fileType); //注意这里的递归方法
}else{
if(files[i].getName().endsWith(fileType))
allFiles.add(files[i]);
}
}
}
}
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.king.android.R;
public class MusicBActivity extends Activity implements OnClickListener{
public static final String TAG="MusicActivity";
static List<File> allFiles = new ArrayList<File>();
private int mCurrentJMusic = 0; // 当前播放的音乐
private MediaPlayer mMediaPlayer = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
refreshFile(getSDCardPath(),".mp3");
if(allFiles.size()<=0){
Toast.makeText(MusicBActivity.this, "没有歌曲", Toast.LENGTH_SHORT).show();
finish();
}
mMediaPlayer = new MediaPlayer();
initView();
}
public String getSDCardPath(){
String sdPath = null;
boolean hasCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if(hasCard){
sdPath = Environment.getExternalStorageDirectory().toString();
}else{
sdPath = Environment.getDataDirectory().toString();
}
return sdPath;
}
private void initView(){
setContentView(R.layout.media_layout);
Button btnMedia = (Button) findViewById(R.id.btn_last);
btnMedia.setOnClickListener(this);
btnMedia = (Button) findViewById(R.id.btn_pause);
btnMedia.setOnClickListener(this);
btnMedia = (Button) findViewById(R.id.btn_stop);
btnMedia.setOnClickListener(this);
btnMedia = (Button) findViewById(R.id.btn_play);
btnMedia.setOnClickListener(this);
btnMedia = (Button) findViewById(R.id.btn_next);
btnMedia.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_play:
playMusic(allFiles.get(mCurrentJMusic));
break;
case R.id.btn_next:
nextMusic();
break;
case R.id.btn_last:
FrontMusic();
break;
case R.id.btn_pause:
if(mMediaPlayer.isPlaying()){
//暂停
mMediaPlayer.pause();
}else{
//播放
mMediaPlayer.start();
}
break;
case R.id.btn_stop:
//是否在播放
if(mMediaPlayer.isPlaying()){
//重置MediaPlayer到初始状态
mMediaPlayer.reset();
}
break;
}
}
private void playMusic(File file){
try{
//重置MediaPlayer
mMediaPlayer.reset();
//设置播放文件 的路径
//mMediaPlayer.setDataSource(file.getAbsolutePath()+File.separator+file.getName());
mMediaPlayer.setDataSource(file.getAbsolutePath());
//播放文件
mMediaPlayer.prepare();
//开始播放
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//播放完成后,进行下一首。
nextMusic();
}
});
}catch(Exception e){
e.printStackTrace();
}
}
//下一首
private void nextMusic(){
if(++mCurrentJMusic>=allFiles.size()){
mCurrentJMusic = 0;
}else{
playMusic(allFiles.get(mCurrentJMusic));
}
}
//上一首
private void FrontMusic(){
if(--mCurrentJMusic>=0){
mCurrentJMusic = allFiles.size();
}else{
playMusic(allFiles.get(mCurrentJMusic));
}
}
/**
*
递归法获取文件夹文件
*TODO:TODO
*Author:Andy.Liu
*Create Time:2012-7-29 下午11:40:33
*TAG:@param path 文件路径,一般选择根目录
*TAG:@param fileType 文件类型。
*Return:void
*/
File file = new File(path);
File[] files = file.listFiles();
if(null==files)
return;
for(int i = 0;i<files.length;i++){
if(files[i].isDirectory()){
refreshFile(files[i].getAbsolutePath(),fileType); //注意这里的递归方法
}else{
if(files[i].getName().endsWith(fileType))
allFiles.add(files[i]);
}
}
}
}
获取文件夹中的文件夹,方法有很多,但递归法效率最高,要用最好的办法去解决问题。。。