【安卓例項】3音樂播放器簡單版
阿新 • • 發佈:2019-02-11
- package com.terry;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import android.app.ListActivity;
- import android.media.MediaPlayer;
- import android.media.MediaPlayer.OnCompletionListener;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.Toast;
- public class musicActivity extends ListActivity {
- //播放物件
- private MediaPlayer myMediaPlayer;
- //播放列表
- private List<String> myMusicList=new ArrayList<String>();
- //當前播放歌曲的索引
- private int currentListItem=0;
- //音樂的路徑
- private static final String MUSIC_PATH=new String("/sdcard/");
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- myMediaPlayer=new MediaPlayer();
- findView();
- musicList();
- listener();
- }
- //繫結音樂
- void musicList(){
- File home=new File(MUSIC_PATH);
- if(home.listFiles(new MusicFilter()).length>0){
- for(File file:home.listFiles(new MusicFilter())){
- myMusicList.add(file.getName());
- }
- ArrayAdapter<String> musicList=new ArrayAdapter<String>
- (musicActivity.this,R.layout.musicitme, myMusicList);
- setListAdapter(musicList);
- }
- }
- //獲取按鈕
- void findView(){
- viewHolder.start=(Button)findViewById(R.id.start);
- viewHolder.stop=(Button)findViewById(R.id.stop);
- viewHolder.next=(Button)findViewById(R.id.next);
- viewHolder.pause=(Button)findViewById(R.id.pause);
- viewHolder.last=(Button)findViewById(R.id.last);
- }
- //監聽事件
- void listener(){
- //停止
- viewHolder.stop.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(myMediaPlayer.isPlaying()){
- myMediaPlayer.reset();
- }
- }
- });
- //開始
- viewHolder.start.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- playMusic(MUSIC_PATH+myMusicList.get(currentListItem));
- }
- });
- //下一首
- viewHolder.next.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- nextMusic();
- }
- });
- //暫停
- viewHolder.pause.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(myMediaPlayer.isPlaying()){
- myMediaPlayer.pause();
- }else{
- myMediaPlayer.start();
- }
- }
- });
- //上一首
- viewHolder.last.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- lastMusic();
- }
- });
- }
- //播放音樂
- void playMusic(String path){
- try {
- myMediaPlayer.reset();
- myMediaPlayer.setDataSource(path);
- myMediaPlayer.prepare();
- myMediaPlayer.start();
- myMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
- @Override
- public void onCompletion(MediaPlayer mp) {
- // TODO Auto-generated method stub
- nextMusic();
- }
- });
- } catch (Exception e) {
- // TODO: handle exception
- e.printStackTrace();
- }
- }
- //下一首
- void nextMusic(){
- if(++currentListItem>=myMusicList.size()){
- currentListItem=0;
- }
- else{
- playMusic(MUSIC_PATH+myMusicList.get(currentListItem));
- }
- }
- //上一首
- void lastMusic(){
- if(currentListItem!=0)
- {
- if(--currentListItem>=0){
- currentListItem=myMusicList.size();
- } else{
- playMusic(MUSIC_PATH+myMusicList.get(currentListItem));
- }
- } else{
- playMusic(MUSIC_PATH+myMusicList.get(currentListItem));
- }
- }
- //當用戶返回時結束音樂並釋放音樂物件
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- // TODO Auto-generated method stub
- if(keyCode==KeyEvent.KEYCODE_BACK){
- myMediaPlayer.stop();
- myMediaPlayer.release();
- this.finish();
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }
- //當選擇列表項時播放音樂
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- // TODO Auto-generated method stub
- currentListItem=position;
- playMusic(MUSIC_PATH+myMusicList.get(currentListItem));
- }
- }