mediaPlayer = MediaPlayer.create(this, Uri.parse("file://" + filePath));
发现一个奇怪问题,如果filePath指向的文件是SD卡根目录下的一个mp3就可以实例化mediaPlayer,但是如果同样的文件被放到文件夹里,然后filePath指向这个文件夹里的mp3就不可以播放了,试过了很多遍都是一样效果,很奇怪啊,难道是什么permission问题?可我也加了<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>public class PlayRecordActivity extends Activity {
private ImageButton startBtn = null;
private ImageButton pauseBtn = null;
private ImageButton stopBtn = null;
private String filePath = null;
private MediaPlayer mediaPlayer = null;
private boolean isPlaying= false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record_play);
findViews();
setListeners();
initData();
}

public void findViews(){
startBtn = (ImageButton) this.findViewById(R.id.record_play);
pauseBtn = (ImageButton) this.findViewById(R.id.record_pause);
stopBtn = (ImageButton) this.findViewById(R.id.record_stop);
}
public void setListeners(){
startBtn.setOnClickListener(new ButtonOnClickListener());
pauseBtn.setOnClickListener(new ButtonOnClickListener());
stopBtn.setOnClickListener(new ButtonOnClickListener());
}
public void initData(){
Intent intent = this.getIntent();
String fileName = intent.getExtras().getString("file_name");
filePath=AppConstant.saveDir+File.separator+fileName;
}
public void playRecord(){
System.out.println(filePath);
Intent intent = this.getIntent();
String fileName = intent.getExtras().getString("file_name");
filePath=AppConstant.saveDir+File.separator+fileName;
File f = new File(filePath);
System.out.println(f.length());
mediaPlayer = MediaPlayer.create(this, Uri.parse("file://" + filePath));
mediaPlayer.setLooping(false);
mediaPlayer.start();
isPlaying = true;
}

public void pauseRecord(){
if(isPlaying == true){
mediaPlayer.pause();
}else{
mediaPlayer.start();
}
isPlaying = isPlaying ? false : true;
}
public void stopRecord(){
if(isPlaying == true){
mediaPlayer.stop();
mediaPlayer.release();
}
}
public final class ButtonOnClickListener implements View.OnClickListener{ @Override
public void onClick(View v) {
switch (v.getId()){
case R.id.record_play :
playRecord();
case R.id.record_pause:
pauseRecord();
case R.id.record_stop:
stopRecord();
}
}
}
}

解决方案 »

  1.   

    读写sd卡的权限?我已经加了
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    我甚至为了调试这样写了:
    File file=new File(filePath);
    System.out.print(file.length);
    mediaPlayer = MediaPlayer.create(this, Uri.parse("file://" + filePath));
    这样都能打印出文件大小了(667765)
      

  2.   

    filePath=AppConstant.saveDir+File.separator+fileName;
    AppConstant.saveDir+File这个是sd卡根目录?
    File.separator是 /
    fileName是mp3文件名称
    文件夹目录呢?在AppConstant.saveDir+File里面?这个目录是否被正确创建
      

  3.   

    奥,不好意思,AppConstant.saveDir被定义成返回字符串了,如下:saveDir=Environment.getExternalStorageDirectory().toString()+File.separator+"recordDir";我甚至为了调试这样写了:
    File file=new File(filePath);
    System.out.print(file.length);
    mediaPlayer = MediaPlayer.create(this, Uri.parse("file://" + filePath));
    这样都能打印出文件大小了(667765)
      

  4.   

    原因被我自己发现了,汗,为了省事我把一个mp3文件后缀改为了3gp,没有网上找现有的3gp,结果导致的原因是android mediaplayer识别不了这个文件。
      

  5.   

    哥们,这个播放器能不能放全啊, 大家也学习下
    请问一下,AppConstant是什么方法?
      

  6.   

    AppConstant只是我自己定义的一个类,里面放应用程序里用到的常量而已