无法在Android中播放原始文件?

时间:2018-07-16 10:20:07

标签: android file-descriptor

我尝试了下面的代码来播放mp3文件,但它会播放所有铃声。 谁能帮我为什么播放铃声?

            MediaPlayer mMediaPlayer1 = new MediaPlayer();
            AssetFileDescriptor fd = context.getResources().openRawResourceFd(R.raw.dialtone);
            FileInputStream in = new FileInputStream(fd.getFileDescriptor());



            mMediaPlayer1.setDataSource(in.getFD());

            mMediaPlayer1.setLooping(true);
            mMediaPlayer1.prepare();
            mMediaPlayer1.start();`

1 个答案:

答案 0 :(得分:1)

尝试一下:

public static void startSound(Context context, int soundResId) {
    if (mMediaPlayer != null) {
        mMediaPlayer.reset();
        mMediaPlayer = null;
    }
    mMediaPlayer = MediaPlayer.create(context, soundResId);
    mMediaPlayer.start();
}

然后

startSound(this, R.raw.dialtone);