|
|
自己在教材基础音乐播放器上做的一些扩展,整理一下以备后用,功能比较简单,包括播放列表,播放列表点击功能,进度条,循环播放等。喜欢的可以参考,写的比较乱,不好意思了。参考资料为《Google Android SDK开发范例大全》
import java.io.IOException;import java.util.List;import android.app.Activity;import android.media.MediaPlayer;import android.media.MediaPlayer.OnCompletionListener;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.ListView;import android.widget.SeekBar;import android.widget.TextView;public class MyMusicPlayer extends Activity { private ImageButton mPause, mNext, mBefore, mStart, mStop; private TextView mTextView1; private ImageView mImageView1; private boolean bIsReleased = false; private boolean bIsPaused = false; public SeekBar seekBr; public ListView musics; public MediaPlayer myPlayer1 = new MediaPlayer(); public int musicIndex = 0; private String _DIR = "/sdcard/"; private List<String> musicList = FindMusics.getSD(); private int count = musicList.size(); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mStart = (ImageButton) findViewById(R.id.myImageButton1); mStop = (ImageButton) findViewById(R.id.myImageButton2); mPause = (ImageButton) findViewById(R.id.pause); mNext = (ImageButton) findViewById(R.id.next); mBefore = (ImageButton) findViewById(R.id.before); mImageView1 = (ImageView) findViewById(R.id.myImageView1); mTextView1 = (TextView) findViewById(R.id.myTextView1); seekBr = (SeekBar) findViewById(R.id.seekb); musics = (ListView) findViewById(R.id.musicList); ArrayAdapter< String> adapter = new ArrayAdapter< String>( this, android.R.layout.simple_list_item_1); for(int i = 0; i < count; i++){ adapter.add(musicList.get(i).toString()); } musics.setAdapter(adapter); musics.setOnItemClickListener(new AdapterView.OnItemClickListener(){ public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { startMusic(arg2); } }); seekBr.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub myPlayer1.seekTo(seekBr.getProgress()); } }); /*秨﹍秙 */ mStart.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub mStart.setImageResource(R.drawable.stars); mImageView1.setImageResource(R.drawable.dance); mPause.setImageResource(R.drawable.pause); startMusic(musicIndex); process(); myPlayer1.setOnCompletionListener(new OnCompletionListener() { // @Override public void onCompletion(MediaPlayer arg0) { mTextView1.setText(R.string.str_finished); mStart.setImageResource(R.drawable.star); if(musicIndex >= count -1 ){ musicIndex = 0; }else{ musicIndex ++; } myPlayer1.reset(); startMusic(musicIndex); process(); mTextView1.setText(musicList.get(musicIndex).toString()); } }); } }); mPause.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View view) { if (myPlayer1 != null) { if(bIsReleased == false) { if(bIsPaused==false) { myPlayer1.pause(); bIsPaused = true; mTextView1.setText(R.string.str_pause); mStart.setImageResource(R.drawable.star); mPause.setImageResource(R.drawable.pause_2); } else if(bIsPaused==true) { myPlayer1.start(); bIsPaused = false; mTextView1.setText(R.string.str_start); mStart.setImageResource(R.drawable.stars); mPause.setImageResource(R.drawable.pause); } } } } }); mNext.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub mStart.setImageResource(R.drawable.stars); mImageView1.setImageResource(R.drawable.dance); if(musicIndex >= count-1){ musicIndex = 0; }else{ musicIndex++; } startMusic(musicIndex); process(); } }); mBefore.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub mStart.setImageResource(R.drawable.stars); mImageView1.setImageResource(R.drawable.dance); startMusic(musicIndex); process(); } }); mStop.setOnClickListener(new ImageButton.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub if(myPlayer1.isPlaying()==true) { myPlayer1.reset(); mTextView1.setText(R.string.str_stopped); mStart.setImageResource(R.drawable.star); mPause.setImageResource(R.drawable.pause); mImageView1.setImageResource(R.drawable.black); } } }); } public void process(){ seekBr.setMax(myPlayer1.getDuration()); new Thread(new Runnable(){ public void run() { while(true){ seekBr.setProgress(myPlayer1.getCurrentPosition()); //获得当前播放的进度值 if(seekBr.getMax() == myPlayer1.getCurrentPosition()){ seekBr.setProgress(0); } try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}).start(); } public void startMusic(int musicIndex){ try { if(myPlayer1.isPlaying()==true) { myPlayer1.reset(); } myPlayer1.setDataSource(_DIR + musicList.get(musicIndex).toString()); myPlayer1.prepare(); myPlayer1.start(); mTextView1.setText(musicList.get(musicIndex).toString()); } catch (IllegalStateException e) { // TODO Auto-generated catch block mTextView1.setText(e.toString()); e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block mTextView1.setText(e.toString()); e.printStackTrace(); } } }
读取sdCard中歌曲的类
import java.io.File;import java.util.ArrayList;import java.util.List;public class FindMusics{ public static List<String> getSD() { List<String> musicList = new ArrayList<String>(); File f = new File("/sdcard"); File[] files = f.listFiles(); for (int i = 0; i < files.length; i++) { File file = files[i]; if (getMusicFile(file.getName())) { musicList.add(file.getName()); } } return musicList; } public static boolean getMusicFile(String path) { // boolean rs; String end = path.substring(path.lastIndexOf(".") + 1, path.length()) .toLowerCase(); if (end.equals("mp3")) return true; else return false; }}
main.xml<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 建立一個TextView --> <TextView android:id="@+id/myTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_y="236px" android:text="@string/hello" /> <!-- 建立第一個ImageButton --> <ImageButton android:id="@+id/myImageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="111px" android:layout_y="206px" android:background="@drawable/black" android:src="@drawable/star" /> <!-- 建立第二個ImageButton --> <ImageButton android:id="@+id/pause" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/black" android:layout_x="141px" android:layout_y="206px" android:src="@drawable/pause" /> <!-- 建立第三個ImageButton --> <ImageButton android:id="@+id/before" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/black" android:layout_x="61px" android:layout_y="206px" android:src="@drawable/before" /> <!-- 建立第四個ImageButton --> <ImageButton android:id="@+id/next" android:layout_height="wrap_content" android:layout_width="wrap_content" android:background="@drawable/black" android:layout_x="181px" android:layout_y="206px" android:src="@drawable/next" /> <!-- 建立第五個ImageButton --> <ImageButton android:id="@+id/myImageButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="206px" android:layout_y="206px" android:background="@drawable/black" android:src="@drawable/stop" /> <!-- 建立一個ImageView --> <ImageView android:id="@+id/myImageView1" android:layout_width="207px" android:layout_height="170px" android:layout_x="72px" android:layout_y="32px" /> <SeekBar android:id="@+id/seekb" android:max="100" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <ListView android:id="@+id/musicList" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_y="260px" /></AbsoluteLayout> |
|