gaomingjun 发表于 2013-1-15 02:23:38

自制Android下的播放器(音频来源SD卡上的固定位置)

1、视图介绍:
http://b45.photo.store.qq.com/http_imgload.cgi?/rurl4_b=28bed8ef94e3a6508c56ea9f7ef499ecaf5bc711b3b6df92d58cbdfecb75a931158c9af53a2d7f7e6f30cbe982c7f6315f4cb13db03b4e33829cf1b20a834ff0c563064a413d38e6acdf3c2860b266d54c4e64cc&a=52&b=45
 
2、代码:
package com.android.player;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.SystemClock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class Player extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  playerTitle_ = (TextView) findViewById(R.id.playertitle);
  playerdurationText_ = (TextView) findViewById(R.id.playerdurationText);
  playControlBtn_ = (ImageButton) findViewById(R.id.playcontrolBtn);
  playControlBtn_.setOnClickListener(new playControlListener());
  playControlBtn_.setImageResource(android.R.drawable.ic_media_play);
  previousBtn_ = (ImageButton) findViewById(R.id.playpreviousfileBtn);
  previousBtn_.setOnClickListener(new PreviousBtnOnClickListener());
  previousBtn_.setEnabled(false);
  nextBtn_ = (ImageButton) findViewById(R.id.playnextfileBtn);
  nextBtn_.setOnClickListener(new NextBtnOnClickListener());
  palyprogressBar_ = (ProgressBar) findViewById(R.id.playProgressBar);
  palyprogressBar_.setMax(100);
  palyprogressBar_.setProgress(0);
  fileName_ = GetFiles(filePath_);
  playerFileList_ = (ListView) findViewById(R.id.playerfileList);
  playerFileList_.setAdapter(new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, fileName_));
  playerFileList_
    .setOnItemClickListener(new FileListOnItemClickListener());
 }
 class playControlListener implements OnClickListener {
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   if (playState_ == stop) {
    Log.v("test", "player start!!");
    currentfile_ = 0;
    playerStart();
   } else if (playState_ == pause) {
    Log.v("test", "player Restart!!");
    playerRestart();
   } else if (playState_ == playing) {
    Log.v("test", "player pause!!");
    playerPause();
   }
  }
 }
 class PreviousBtnOnClickListener implements OnClickListener {
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   currentfile_ -= 1;
   playerStop();
   playerStart();
  }
 }
 class NextBtnOnClickListener implements OnClickListener {
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   currentfile_ += 1;
   playerStop();
   playerStart();
  }
 }
 class MyOnCompletionListener implements OnCompletionListener {
  @Override
  public void onCompletion(MediaPlayer arg0) {
   // TODO Auto-generated method stub
   currentfile_ += 1;
   playerStop();
   playerStart();
  }
 }
 class FileListOnItemClickListener implements OnItemClickListener {
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
   // TODO Auto-generated method stub
   currentfile_ = arg2;
   playerStop();
   playerStart();
  }
 }
 class ProgressBarThread extends Thread {
  public void run() {
   myHandler.post(new Runnable() {
    @Override
    public void run() {
     // TODO Auto-generated method stub
     playerTitle_.setText(fileName_);
    }
   });
   palyprogressBar_.setProgress(0);
   palyprogressBar_.setMax(fileDuration_);
   int current_ = 0;
   while (playState_ != stop) {
    palyprogressBar_.setProgress(current_);
    current_ = mp_.getCurrentPosition() / 1000;
    durationText_.delete(0, durationText_.length());
    if ((current_ / 60) < 10) {
     durationText_.append(0);
    }
    durationText_.append(current_ / 60);
    durationText_.append(":");
    if ((current_ % 60) < 10) {
     durationText_.append(0);
    }
    durationText_.append(current_ % 60);
    durationText_.append("/");
    if ((fileDuration_ / 60) < 10) {
     durationText_.append(0);
    }
    durationText_.append(fileDuration_ / 60);
    durationText_.append(":");
    if ((fileDuration_ % 60) < 10) {
     durationText_.append(0);
    }
    durationText_.append(fileDuration_ % 60);
    myHandler.post(new Runnable() {
     @Override
     public void run() {
      // TODO Auto-generated method stub
      playerdurationText_.setText(durationText_.toString());
     }
    });
    SystemClock.sleep(100);
   }
  }
 }
 void playerRestart() {
  playState_ = playing;
  playControlBtn_.setImageResource(android.R.drawable.ic_media_pause);
  if (mp_ != null) {
   mp_.start();
  }
 }
 void playerStart() {
  if (currentfile_ == 0) {
   previousBtn_.setEnabled(false);
  } else {
   previousBtn_.setEnabled(true);
  }
  if (currentfile_ == fileName_.length - 1) {
   nextBtn_.setEnabled(false);
  } else {
   nextBtn_.setEnabled(true);
  }
  playState_ = playing;
  playControlBtn_.setImageResource(android.R.drawable.ic_media_pause);
  mp_ = new MediaPlayer();
  mp_.setOnCompletionListener(new MyOnCompletionListener());
  try {
   mp_.setDataSource(filePath_ + fileName_);
   mp_.prepare();
   fileDuration_ = mp_.getDuration() / 1000;
   mp_.start();
  } catch (IllegalArgumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IllegalStateException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  new ProgressBarThread().start();
 }
 void playerPause() {
  playState_ = pause;
  playControlBtn_.setImageResource(android.R.drawable.ic_media_play);
  if (mp_ != null) {
   mp_.pause();
  }
 }
 void playerStop() {
  playState_ = stop;
  playControlBtn_.setImageResource(android.R.drawable.ic_media_play);
  if (mp_ != null) {
   mp_.release();
   mp_ = null;
  }
 }
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  // TODO Auto-generated method stub
  if (keyCode == KeyEvent.KEYCODE_BACK) {
   this.playerStop();
  }
  return super.onKeyDown(keyCode, event);
 }
 String[] GetFiles(String path) {
  File dir = new File(path);
  String[] filenames = dir.list(new FilenameFilter() {
   @Override
   public boolean accept(File arg0, String arg1) {
    // TODO Auto-generated method stub
    if (arg1.endsWith("mp3")) {
     return true;
    }
    return false;
   }
  });
  return filenames;
 }
 TextView playerTitle_;
 TextView playerdurationText_;
 ImageButton playControlBtn_;
 ImageButton previousBtn_, nextBtn_;
 ListView playerFileList_;
 Handler myHandler = new Handler();
 // stop -1
 // playing 0
 // pause 1
 int playState_ = stop;
 static int stop = -1;
 static int playing = 0;
 static int pause = 1;
 MediaPlayer mp_;
 String filePath_ = Environment.getExternalStorageDirectory() + "/music/";
 String[] fileName_;
 int currentfile_ = 0;
 StringBuffer durationText_ = new StringBuffer();
 ProgressBar palyprogressBar_;
 int fileDuration_ = 0;
}
3、布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="wrap_content"
 android:layout_height="wrap_content">
 <TextView android:id="@+id/playertitle" android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 </TextView>
 <TextView android:id="@+id/playerdurationText"
  android:layout_width="wrap_content" android:layout_height="wrap_content">
 </TextView>
 <SeekBar android:id="@+id/playProgressBar" style="?android:attr/progressBarStyleHorizontal"
  android:layout_width="fill_parent" android:layout_height="wrap_content">
 </SeekBar>
 <LinearLayout android:orientation="horizontal"
  android:layout_width="fill_parent" android:layout_height="wrap_content">
  <ImageButton android:id="@+id/playpreviousfileBtn"
   style="@android:style/MediaButton.Previous" />
  <ImageButton android:id="@+id/playcontrolBtn" style="@android:style/MediaButton.Play" />
  <ImageButton android:id="@+id/playnextfileBtn" style="@android:style/MediaButton.Next" />
 </LinearLayout>
 <TextView android:text="List:" android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:textSize="20sp"></TextView>
 <ImageView android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:src="@drawable/divider_horizontal_dark_opaque"></ImageView>
 <ListView android:id="@+id/playerfileList"
  android:layout_width="wrap_content" android:layout_height="wrap_content">
 </ListView>
</LinearLayout>
页: [1]
查看完整版本: 自制Android下的播放器(音频来源SD卡上的固定位置)