문제

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();
        }
도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top