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 implementsSeekBar.OnSeekBarChangeListener, View.OnClickListener {SeekBar skb;TextView totaltv, playtv;Button play, pause, stop;List pathlist;int corrent=0;public static void aa(){}//创建媒体播放对象MediaPlayer mplayer;Handler handler=new Handler();boolean hadDestroy=false;Runnable runs=new Runnable(){@Overridepublic void run() {// TODO Auto-generated method stubif(!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());}}};@Overrideprotected 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() {@Overridepublic void onCompletion(MediaPlayer mp) {// TODO Auto-generated method stubif(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 stubpathlist=new ArrayList();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 blocke.printStackTrace();} }@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch(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;}}@Overridepublic void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {// TODO Auto-generated method stubif(mplayer!=null){if(fromUser){//skb.setProgress(progress);mplayer.seekTo(progress);}}}@Overridepublic void onStartTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}@Overridepublic void onStopTrackingTouch(SeekBar seekBar) {// TODO Auto-generated method stub}@Overridepublic 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;}@Overridepublic 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() {}@Overridepublic 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() */@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();if(mplayer!=null){mplayer.stop();hadDestroy=true;mplayer.release();}}}

有问题 但是不会用Debug调试 求大神帮忙调试

下面是布局代码

            

权限加载为: android.permission.READ_EXTERNAL_STORAGE

更多相关文章

  1. android 开发真机测试,无法安装调试解决
  2. [置顶] android 无线调试
  3. Linux调试工具之:ftrace
  4. Android使用线程做开始暂停
  5. 秒懂Android(安卓)Studio的奇技淫巧
  6. Android下用gdb远程调试
  7. Android(安卓)Debug keystore系统位置
  8. Cordova项目使用Android(安卓)Studio真机调试
  9. Android(安卓)studio安装与调试

随机推荐

  1. ASP.NET Core环境设置教程(2)_实用技巧
  2. 详解关于Blog实现一个日历的实例教程
  3. ASP.NET中的参数与特殊类型的实例详解
  4. ASP.NET Core新建项目教程(3)_实用技巧
  5. C/C++中用空指针简化代码实例
  6. ASP.NET Core项目结构教程(4)_实用技巧
  7. ASP.NET中healthMonitor属性的用法教程
  8. ASP.NET Core Project.json文件(5)_实用技
  9. 关于继承和多态性的实例代码
  10. ASP.NET Core项目配置教程(6)_实用技巧