Question

I know there are lots of similar problems to mine, but for some reason the answers to those questions don't seem to work. I am using android and have a sound file named 'backgroundbeat.wav' and I'm trying to import it but instead I get the error:

  "failed to open file 'res\raw\backgroundbeat.wav'. (No such file in directory)"

Here is the code where I use the media player:

mp = new MediaPlayer();

        try{
            mp.setDataSource("res/raw/backgroundbeat.wav");
            mp.prepare();
            mp.start();
        }catch(Exception e){
            e.printStackTrace();
        }
Was it helpful?

Solution

Use your app package name to define the correct path :

mp.setDataSource("android.resource://<YOURPACKAGE_NAME>/raw/backgroundbeat");

example:

 mp.setDataSource("android.resource://com.jorgesys.myapp/raw/backgroundbeat");

or other way to load a media file from /raw folder, if you are inside an activity you could use:

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

or You can use a VideoView to play your file Videoview on splash screen

OTHER TIPS

Not much experienced in this but you can try to use mediaPlayer.setDataSource(FileDescriptor fd) instead of mediaPlayer.setDataSource(String path). See MediaPlayer setDataSource, better to use path or FileDescriptor? . Apparently if you call prepare() while using string as a parameter to setDataSource, it is causing some issues as per the answers.

You may try something like:

// Use the proper context instead of *context* variable
InputStream in = context.getResources().openRawResource(R.raw.backgroundbeat);
mediaPlayer.setDataSource(in.getFD())

Let me know if it helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top