8

我的目录中已经有一个 .wav 文件。同时我需要将它与一个 mp3 文件一起播放。我用了,

String recordedFile = "/storage/sdcard0/PINOYKARAOKE/1373597371359.wav";

MediaPlayer recordedSong = new MediaPlayer();
try{
recordedSong = MediaPlayer.create(ctx, Uri.fromFile(recordedFile));
recordedSong.prepare();
recordedSong.start();
}
catch(Exception e){
}

错误:创建失败并抛出 IOException

4

3 回答 3

11

尝试创建原始文件夹并将文件放在那里,使用它

public void number(int num, Context ctx) {
                 AssetManager am;
        try {
            am = ctx.getAssets();
            AssetFileDescriptor afd = am.openFd("android.resource://"+getPackageName+"/"+R.raw.your_file_name);
            player = new MediaPlayer();
            player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
                    afd.getLength());
            player.prepare();
            player.start();
            player.setOnCompletionListener(new OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {
                    // TODO Auto-generated method stub
                    mp.release();
                }

            });
            player.setLooping(false);
                    } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }
于 2013-07-12T06:52:21.417 回答
7

我试过@aangwi 的答案,但得到了 FileNodeFoundException

final AssetFileDescriptor afd = myactivity.getResources().openRawResourceFd(R.raw.your_file);
final FileDescriptor fileDescriptor = afd.getFileDescriptor();
MediaPlayer player = new MediaPlayer();
try {
    player.setDataSource(fileDescriptor, afd.getStartOffset(),
    afd.getLength());
    player.setLooping(false);
    player.prepare();
    player.start();
} catch (IOException ex) {
    LOGGER.error(ex.getLocalizedMessage(), ex);
}
于 2018-02-07T14:01:12.000 回答
2

这对我有用(Kotlin):

val mediaPlayer = MediaPlayer.create(
                    context,
                    R.raw.sound)
mediaPlayer.start()
于 2020-12-02T14:14:10.947 回答