1、视图介绍:

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

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_[currentfile_]);
}
});

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_[currentfile_]);
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布局属性: android:visibility
  2. android中填充界面布局的三种方式
  3. Android 多扩展布局ChipGroup使用
  4. android系统自带的适配布局activity_list_item
  5. Android视图绘制流程完全解析,带你一步步深入了解View(四)
  6. Android应用程序窗口(Activity)的测量(Measure)、布局(Layout)和绘制(Dr
  7. Android应用程序窗口(Activity)的测量(Measure)、布局(Layout)和绘制(Dr
  8. Android Studio 实时显示布局文件Preview窗口
  9. android 5种布局

随机推荐

  1. Android 获取手机IMEI 和 IMSI 号
  2. You need to use a Theme.AppCompat them
  3. 【Android(安卓)Debug】 Skipping insecu
  4. Android开发之广播机制
  5. android个人视频学习笔记(二)
  6. Android(安卓)Lib层打印log之------bioni
  7. 移动应用开发辅助服务推荐
  8. 在代码中实现按下Home键的效果
  9. android coredump 调试
  10. Android小技巧总结1