ex_musicPlayer




    

    

        

        

        

        

        
    





	

	
	
		
		

		
	
		
		


package com.ouling.ex_musicPlayer;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//import android.app.Activity;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class ex_musicPlayer extends ListActivity {
	// 播放对象
	private MediaPlayer m_musicplayer;
	// 播放列表
	private List m_playlist = new ArrayList();
	// 当前播放索引位置
	private int m_list_item = 0;
	// 音乐路径
	private static String m_musicpath = new String("/sdcard/");

	public Button last;
	public Button stop;
	public Button start;
	public Button pause;
	public Button next;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		m_musicplayer = new MediaPlayer();
		findView();// 获取按钮
		musicList();// 获取播放列表
		listener();// 监听
	}

	private void listener() {
		// TODO Auto-generated method stub
		// 停止
		this.stop.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (m_musicplayer!=null) {
					m_musicplayer.stop();
				}
			}
		});

		// 开始
		start.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				playMusic(m_playlist.get(m_list_item));
			}

		});
		// 下一首
		next.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				nextMusic();
			}
		});
		// 暂停
		pause.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (m_musicplayer.isPlaying()) {
					m_musicplayer.pause();
				} else {
					m_musicplayer.start();
				}
			}
		});
		// 上一首
		last.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				lastMusic();
			}
		});

	}

	// 获取SD音乐
	private void musicList() {
		// TODO Auto-generated method stub
		m_playlist.clear();
		search(m_musicpath, "mp3", m_playlist);

				SimpleAdapter adapter = new SimpleAdapter(this, this.getData(),
				R.layout.m_musicitem,
				new String[] { "song_name", "info", "img" }, new int[] {
						R.id.song_name, R.id.info, R.id.img });

		setListAdapter(adapter);
	}

	// 遍历路径下指定后缀名
	private void search(String dir, final String suffix, List list) {
		File file = new File(dir);
		// 遍历该目录中所有文件
		File[] files = file.listFiles();
		if ((files != null) && (files.length > 0)) {
			for (File tmpfile : files) {
				// 如果是文件夹,继续遍历该目录
				if (tmpfile.isDirectory()) {
					search(tmpfile.getPath(), suffix, list);
				} else {
					//判断文件后缀名
					if (tmpfile.getPath().endsWith(suffix)) {
						list.add(tmpfile.getPath());
					}
				}
			}
		}
	}

	private List> getData() {
		List> m_list = new ArrayList>();
		Map map;	
		for (int i = 0; i < m_playlist.size(); i++) {
			map = new HashMap();
			//获取MP3中的歌曲信息
			File file = new File(m_playlist.get(i)); 
			map.put("song_name", file.getName());
			map.put("info", "test");
			map.put("img", R.drawable.panda);
			m_list.add(map);
		}
		return m_list;
	}
	
	 
		 

	// 获取按钮
	private void findView() {
		// TODO Auto-generated method stub
		start = (Button) findViewById(R.id.start);
		stop = (Button) findViewById(R.id.stop);
		last = (Button) findViewById(R.id.last);
		next = (Button) findViewById(R.id.next);
		pause = (Button) findViewById(R.id.pause);
	}

	// 播放音乐
	void playMusic(String path) {
		try {
			m_musicplayer.reset();
			m_musicplayer.setDataSource(path);
			m_musicplayer.prepare();// 准备同步
			m_musicplayer.start();
			if (m_playlist.isEmpty() == true) {
				Toast.makeText(this, "播放列表为空", 1000).show();
				return;
			}
			m_musicplayer.setOnCompletionListener(new OnCompletionListener() {
				@Override
				public void onCompletion(MediaPlayer arg0) {
					// TODO Auto-generated method stub
					nextMusic();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 上一首
	void lastMusic() {
		if (m_list_item == 0) {
			m_list_item = m_playlist.size() - 1;
		} else {
			m_list_item--;
		}
		String path = m_playlist.get(m_list_item);
		playMusic(path);
	}

	// 下一首
	void nextMusic() {
		if (m_list_item == (m_playlist.size() - 1)) {
			m_list_item = 0;
		} else {
			m_list_item++;
		}
		String path = m_playlist.get(m_list_item);
		playMusic(path);
	}

	// 选择列表项时,播放音乐
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		m_list_item = position;
		String path = m_playlist.get(m_list_item);
		playMusic(path);
	}

	// 当用户返回时结束音乐并释放音乐对象
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			m_musicplayer.stop();
			m_musicplayer.release();
			this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}


你可能感兴趣的:(android)