Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android 2.3上使用MediaPlayer从SD卡播放声音_Android - Fatal编程技术网

在android 2.3上使用MediaPlayer从SD卡播放声音

在android 2.3上使用MediaPlayer从SD卡播放声音,android,Android,这适用于android 4.2: SOUNDS_DIR = getFilesDir().getAbsolutePath() + "/"; SoundToPlay = new MediaPlayer(); SoundToPlay.setDataSource(SOUNDS_DIR + ResourceID + ".mp3"); SoundToPlay.prepare(); SoundToPlay.start(); 当我在android 2.3上运行它时,LogCat会打印: Med

这适用于android 4.2:

SOUNDS_DIR = getFilesDir().getAbsolutePath() + "/";
SoundToPlay = new MediaPlayer();
SoundToPlay.setDataSource(SOUNDS_DIR + ResourceID
        + ".mp3");
SoundToPlay.prepare();
SoundToPlay.start();
当我在android 2.3上运行它时,LogCat会打印:

MediaPlayer错误(1,-2147483648)


如果我使用与resource完全相同的文件(/res/raw/),在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\u DIR+ResourceID
+“.mp3”)

设置


可能在验证您的路径之前尝试,如:

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\u DIR+ResourceID
+“.mp3”)

设置

试试这个:

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();
}
祝你好运。

试试这个:

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();
}

祝你好运。

不幸的是,我需要将.mp3文件存储为私人文件。我还尝试了SoundToPlay.setDataSource(新文件(SOUNDS_DIR+“/”+ResourceID+“.mp3”).getAbsolutePath();我想是因为
我需要将我的.mp3文件存储为私有文件。
让我知道它是否有效,我忘记了已知外部存储的Android会自动确保你是,在
准备后
不幸的是,我需要将我的.mp3文件存储为私有文件。我还尝试了SoundToPlay.setDataSource(新文件(SOUNDS_DIR+“/”+ResourceID+“.mp3”).getAbsolutePath();我想是因为
我需要将我的.mp3文件存储为私有文件。
让我知道它是否工作,我忘记了已知外部存储的Android会自动确保你是,在
准备
从MediaPlayer编辑的代码后。创建(c,uri);创建(这个,uri);MediaPlayer.create(this,Uri.fromFile(new File(SOUNDS_DIR+“/”+ResourceID+“.mp3”))生成错误(1,-25…what is SOUNDS_DIR=?我测试了它。它工作了。SOUNDS_DIR.post行代码的值是多少。从MediaPlayer.create(c,Uri);到MediaPlayer.create(this,Uri);MediaPlayer.create(this,Uri.fromFile)(new File(SOUNDS_DIR+“/)+ResourceID+“.mp3”))生成错误(1,-25…what is SOUNDS\u DIR=?我测试它。它工作。SOUNDS\u DIR的值是多少。post代码行。