简体   繁体   中英

Saving file in raw directory

This is how I save an audio file:

try (FileOutputStream out = new FileOutputStream(getFilesDir() + "/output.mp3")) {
    System.out.println(getFilesDir());
    out.write(audioContents.toByteArray());
    System.out.println("Audio content written to file \"output.mp3\"");
}

That works fine but I would like to play that audio file. So I could use:

MediaPlayer mp = MediaPlayer.create(this, R.raw.output);
mp.start();

That wouldn't work, since the second parameter is not the correct path of my audio file. So how can I save my audio file in src/main/res/raw, so that I can use R.raw.ouput ?

You can't do that. The raw folder is part of the APK. It's meant for raw data you want to include in your app, but not for adding files at runtime.

See following on how to use MediaPlayer with a file. Simple mediaplayer play mp3 from file path?

This is how I could solve it:

String myFile = getFilesDir() + "/output.mp3";
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(myFile);
mediaPlayer.prepare();
mediaPlayer.start();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM