package com.rj1b.medieplayerdemo;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity implements
SeekBar.OnSeekBarChangeListener, View.OnClickListener {
SeekBar skb;
TextView totaltv, playtv;
Button play, pause, stop;
List<String> pathlist;
int corrent=0;
public static void aa(){
}
//创建媒体播放对象
MediaPlayer mplayer;
Handler handler=new Handler();
boolean hadDestroy=false;
Runnable runs=new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
if(!hadDestroy){
handler.postDelayed(this, 500);
int currentTime =Math.round(mplayer.getCurrentPosition()/1000);
String playTime=String.format("%02d:%02d",currentTime/60,currentTime%60);
playtv.setText(playTime);
skb.setProgress(mplayer.getCurrentPosition());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
skb = (SeekBar) this.findViewById(R.id.seekBar1);
totaltv = (TextView) this.findViewById(R.id.totaltv);
playtv = (TextView) this.findViewById(R.id.playtv);
play = (Button) this.findViewById(R.id.play);
pause = (Button) this.findViewById(R.id.pause);
stop = (Button) this.findViewById(R.id.stop);
play.setOnClickListener(this);
pause.setOnClickListener(this);
stop.setOnClickListener(this);
skb.setOnSeekBarChangeListener(this);
mplayer =new MediaPlayer();
mplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
if(corrent==pathlist.size()-1){
//mplayer.isPlaying();
corrent=0;
//System.out.println(pathlist.size());
}else{
corrent++;
}
/*for(int corrent=0;corrent==pathlist.size()-1;corrent++){
System.out.print("下一首");
}*/
initMediaPler();
mplayer.start();
}
});
initMediaPler();
/*
* if (savedInstanceState == null) {
* getFragmentManager().beginTransaction() .add(R.id.container, new
* PlaceholderFragment()).commit(); }
*/
}
private void initMediaPler() {
// TODO Auto-generated method stub
pathlist=new ArrayList<String>();
File file1=new File(Environment.getExternalStorageDirectory()+"/music/"
+"englishworld-Happy Birthday.mp3");
File file2=new File(Environment.getExternalStorageDirectory()+"/music/"
+"englishsong-renguiqingweiliao.mp3");
pathlist.add(file1.getPath());
pathlist.add(file2.getPath());
try {
mplayer.setDataSource(pathlist.get(corrent));
//Log.d("path", file.getPath());
mplayer.prepare();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.play:
if(!mplayer.isPlaying()){
mplayer.start();
int totalTime=Math.round(mplayer.getDuration()/1000);
String totalstr=String.format("%02d:%02d", totalTime/60,totalTime%60);
totaltv.setText(totalstr);
skb.setMax(mplayer.getDuration());
handler.postDelayed(runs, 500);
}
break;
case R.id.pause:
if(mplayer.isPlaying()){
mplayer.pause();
}
break;
case R.id.stop:
if(mplayer.isPlaying()){
mplayer.reset();
initMediaPler();
}
break;
}
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
if(mplayer!=null){
if(fromUser){
//skb.setProgress(progress);
mplayer.seekTo(progress);
}
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
/* (non-Javadoc)
* @see android.app.Activity#onDestroy()
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(mplayer!=null){
mplayer.stop();
hadDestroy=true;
mplayer.release();
}
}
}
有问题 但是不会用Debug调试 求大神帮忙调试
下面是布局代码
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/totaltv"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="01:00" />
<TextView
android:id="@+id/playtv"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="right"
android:layout_height="wrap_content"
android:text="00:00" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放" />
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止" />
</LinearLayout>
权限加载为: android.permission.READ_EXTERNAL_STORAGE