0

这适用于 android 4.2:

SOUNDS_DIR = getFilesDir().getAbsolutePath() + "/";
SoundToPlay = new MediaPlayer();
SoundToPlay.setDataSource(SOUNDS_DIR + ResourceID
        + ".mp3");
SoundToPlay.prepare();
SoundToPlay.start();

当我在 android 2.3 上运行它时,LogCat 打印:

媒体播放器错误(1,-2147483648)

如果我使用与资源完全相同的文件(/res/raw/),也可以使用 2.3

4

2 回答 2

3

也许之前尝试验证您的路径,例如:

File path = Environment.getExternalStoragePublicDirectory(
    Environment.DIRECTORY_MUSIC);
 File file = new File(path, ResourceID+".mp3");

 // if file exists ....

之后可以写:

SoundToPlay = new MediaPlayer();
SoundToPlay.setDataSource(path + "/" + ResourceID  + ".mp3");
SoundToPlay.prepare();
SoundToPlay.start();

[编辑]

代替

SoundToPlay.setDataSource(SOUNDS_DIR + ResourceID + ".mp3");

FileInputStream fileInputStream = new FileInputStream(SOUNDS_DIR + ResourceID
    + ".mp3");
SoundToPlay.setDataSource(fileInputStream.getFD());         
SoundToPlay.prepare(); 
于 2013-09-22T19:23:10.477 回答
0

尝试这个:

String path = Environment.getExternalStorageDirectory() + "/sound.mp3";
File file = new File(path);
Uri uri = Uri.fromFile(file);
MediaPlayer mp = MediaPlayer.create(this, uri);

if (mp != null)
{
   mp.setOnCompletionListener(new OnCompletionListener() {
      @Override
      public void onCompletion(MediaPlayer mp) {
         mp.release();
      }
   });
   mp.start();
}

祝你好运。

于 2013-09-22T19:26:48.240 回答