1. 程式人生 > >android Mediaplayer用法

android Mediaplayer用法

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;

publicclass musicActivity extends ListActivity {
    
//播放物件private MediaPlayer myMediaPlayer;
    
//播放列表private List<String> myMusicList=new ArrayList<String>();
    
//當前播放歌曲的索引
privateint currentListItem=0;
    
//音樂的路徑privatestaticfinal String MUSIC_PATH=new String("/sdcard/");
    
    
/** Called when the activity is first created. */
    @Override
    
publicvoid 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
        
publicvoid onClick(View v) {
            
// TODO Auto-generated method stubif(myMediaPlayer.isPlaying()){
                myMediaPlayer.reset();
            }
        }
    });
       
//開始       viewHolder.start.setOnClickListener(new OnClickListener() {
        
        @Override
        
publicvoid onClick(View v) {
            
// TODO Auto-generated method stub            playMusic(MUSIC_PATH+myMusicList.get(currentListItem));
        }
    });
       
//下一首       viewHolder.next.setOnClickListener(new OnClickListener() {
        
        @Override
        
publicvoid onClick(View v) {
            
// TODO Auto-generated method stub            nextMusic();
        }
    });
       
//暫停       viewHolder.pause.setOnClickListener(new OnClickListener() {
        
        @Override
        
publicvoid onClick(View v) {
            
// TODO Auto-generated method stubif(myMediaPlayer.isPlaying()){
                myMediaPlayer.pause();
            }
else{
                myMediaPlayer.start();
            }
        }
    });
       
//上一首       viewHolder.last.setOnClickListener(new OnClickListener() {
        
        @Override
        
publicvoid 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
            
publicvoid 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
    
publicboolean onKeyDown(int keyCode, KeyEvent event) {
        
// TODO Auto-generated method stubif(keyCode==KeyEvent.KEYCODE_BACK){
               myMediaPlayer.stop();
               myMediaPlayer.release();
               
this.finish();
               
returntrue;
           }
        
returnsuper.onKeyDown(keyCode, event);
    }
   
       
//當選擇列表項時播放音樂        @Override
    
protectedvoid onListItemClick(ListView l, View v, int position, long id) {
        
// TODO Auto-generated method stub        currentListItem=position;
        playMusic(MUSIC_PATH
+myMusicList.get(currentListItem));
    }
   
}