Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android之播放器

android之播放器

編輯:關於Android編程

本部分代碼在《Android應用開發揭秘》中提到,但是在eclipse環境下調試時出現異常,幾番糾結,代碼終於可以播放器音樂、並成功移植到手機上......

 

package com.example.calculate;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
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.ImageButton;
import android.widget.ListView;

@SuppressLint(SdCardPath)
public class Music extends ListActivity{

	private static ImageButton mFrontImageButton = null;
	private static ImageButton mStopImageButton = null;
	private static ImageButton mPlayImageButton = null;
	private static ImageButton mPauseImageButton = null;
	private static ImageButton mNextImageButton = null;
	
	//MediaPlayer對象
	private MediaPlayer mMediaPlayer = null;
	
	//歌曲播放列表
	private List mMusicList = new ArrayList();
	
	//當前歌曲播放索引
	private int currentListItem = 0;
	
	//音樂路徑
//	private static final String MUSIC_PATH = new String(/sdcard/);	//該路徑是用於eclipse 下仿真用的
	private static final String MUSIC_PATH = new String(/storage/sdcard1/qqmusic/song/);	//該路徑是用於手機,這個路徑是我手機歌曲的路徑
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.music);
	//	setContentView(R.layout.activity_calculate);
		
		mMediaPlayer = new MediaPlayer();	
		//更新播放列表
		musicList();
		

		mStopImageButton = (ImageButton)findViewById(R.id.imageButton2);
		
		mPlayImageButton =  (ImageButton)findViewById(R.id.imageButton3);
		mPlayImageButton.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View v) 
			{
				playMusic(MUSIC_PATH + mMusicList.get(currentListItem));
			}
		});
		
		mPauseImageButton =  (ImageButton)findViewById(R.id.imageButton4);
		mPauseImageButton.setOnClickListener(new OnClickListener() 
		{	
			public void onClick(View v) 
			{
				if (mMediaPlayer.isPlaying())
					mMediaPlayer.stop();
				else 
					mMediaPlayer.start();
			}
		});
		
		mNextImageButton =  (ImageButton)findViewById(R.id.imageButton5);
		mNextImageButton.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View v) 
			{
				nextMusic();
			}
		});

		mFrontImageButton = (ImageButton)findViewById(R.id.imageButton1);
		mFrontImageButton.setOnClickListener(new OnClickListener() 
		{
			public void onClick(View v)
			{
				FrontMusic();
			}
		});
		

		listener();
	}
	
	   //監聽事件
	   void listener(){
	       //停止
		   mStopImageButton.setOnClickListener(new OnClickListener() {
	        
	        @Override
	        public void onClick(View v) {
	            // TODO Auto-generated method stub
	            if(mMediaPlayer.isPlaying()){
	                mMediaPlayer.reset();
	            }
	        }
	    });
	  }

	public void onClick(View view) {
		if (view == mFrontImageButton){
			
		}else if (view == mStopImageButton) {
			if (mMediaPlayer.isPlaying()) {
				mMediaPlayer.reset();
			}
		}else if (view == mPlayImageButton) {
			playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
			
		}else if (view == mPauseImageButton) {
			if (mMediaPlayer.isPlaying())
				mMediaPlayer.stop();
			else 
				mMediaPlayer.start();
			
		}else if (view == mNextImageButton) {
			nextMusic();
		}else if (view == mFrontImageButton) {
			FrontMusic();
		}
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			mMediaPlayer.stop();
			mMediaPlayer.release();
			this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	protected void onListItemClick(ListView l, View v, int position, long id) {
		currentListItem = position;
		playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
	}
	
	public void playMusic(String path) {
		try {
			//mMediaPlayer在重新播放一首歌曲之前要之行下面的操縱,
			//reset()重置,
			//setDataSource(path)播放歌曲路徑.
			//prepare()、start() 准備、播放.
			mMediaPlayer.reset();
			mMediaPlayer.setDataSource(path);
			mMediaPlayer.prepare();
			mMediaPlayer.start();
			mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
				public void onCompletion(MediaPlayer arg0) {
					//播放完一首歌曲之後進行下一首
					nextMusic();
				}
			});
			
		}catch (IOException e) {
			 e.printStackTrace();
		}
	}
	
	public void nextMusic() {
		if (++currentListItem >= mMusicList.size()) {
			currentListItem = 0;
		}else {
			playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
		}
	}
	
	public void FrontMusic() {
		if (--currentListItem >= mMusicList.size()) {
			currentListItem = 0;
		}else {
			playMusic(MUSIC_PATH+mMusicList.get(currentListItem));
		}
	}
	
    //綁定音樂
    void musicList(){
        File home=new File(MUSIC_PATH);
        if(home.listFiles(new MusicFilter()).length>0){
            for(File file:home.listFiles(new MusicFilter())){
            	mMusicList.add(file.getName());
            }
            ArrayAdapter musicList=new ArrayAdapter(Music.this,R.layout.c1, mMusicList);
            setListAdapter(musicList);
        }
    }
}

/* 過濾文件類型 */
class MusicFilter implements FilenameFilter
{
	public boolean accept(File dir, String name)
	{
		//這裡還可以設置其他格式的音樂文件
		return (name.endsWith(.mp3));
	}
}

以下是music.xml界面布局

 



    

    

    

    

    

    
    


下面是c.xml,因為這個文件導致我花了好長的時間,這個文件會被ArrayAdapter()調用......不吐槽了,直接上碼

 

 



最終是AndroidManifest.xml

 

 

 

    
      
 	 
    
    
    
        
            
                

                
            
        
        
        
        
    



 

手機運行的效果:

\

 

 

過程很糾結,堅持!過程問題越多,受教越廣,去享受那份糾結吧。。。。

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved