-1

I spent over ten hours on this, trying many solutions I found on the internet, and feeling stupid... This function just loads an ogg file in a MediaPlayer. It gets the url of the file as a string argument. It first extracts the name of the file and tries to load it from res/raw/, if it exists there, else it gets it from the website.

MediaPlayer player ;

void snd_load(String url) {

    String id_snd = url.substring(url.lastIndexOf("/") + 1); // name of the file

    try{
        player.setDataSource( "android.resource://didi.a8bitpocketwrestlers/res/raw/" + id_snd );
    }
    catch (Exception e){
        Toast.makeText(getApplicationContext(), "can't open file, will try to download it"+"\n"+e , Toast.LENGTH_SHORT).show();
        try{ player.setDataSource(url); }
        catch (Exception e2){ Toast.makeText(getApplicationContext(), "can't download file"+"\n"+e2 , Toast.LENGTH_SHORT).show(); }
    }
}

I was getting java.lang.NULLPointerException error for both. I understand now, player was only of type MediaPlayer, it pointed to nothing yet. I was mistaken because there are properties inside so I thought the object existed. I must do either player = new MediaPlayer(); or player = MediaPlayer.create();.

Now I fixed this, I still can't load the file.

04-22 15:31:58.018 130-4150/? E/FileSource: Failed to open file 'android.resource://didi.a8bitpocketwrestlers/snd_title.ogg'. (No such file or directory) 04-22 15:31:58.018 4087-4099/didi.a8bitpocketwrestlers E/MediaPlayer: error (1, -2147483648) 04-22 15:31:58.018 4087-4087/didi.a8bitpocketwrestlers E/MediaPlayer: Error (1,-2147483648)

And it stops, it does not display the message and does not try to download it.

With player = MediaPlayer.create(getApplicationContext(), R.raw.snd_title ); it crashes because of my player.prepareAsync(); call. I read that .create already takes care of that and so I can't call .prepareAsync(); nor use .setOnPreparedListener(). This is a problem for me, I need to know when it is ready.

Anyway I must use the name in the variable id_snd, not just snd_title.ogg file, so I changed it for player = MediaPlayer.create(getApplicationContext(), getApplicationContext().getResources().getIdentifier(id_snd, "raw", getApplicationContext().getPackageName()) ); and I get

android.content.res.Resources$NotFoundException:Resource ID #0x0

New version

MediaPlayer player ;

void snd_load(String url) {

    String id_snd = url.substring(url.lastIndexOf("/") + 1); // name of the file
    id_snd = id_snd.substring( 0 , id_snd.indexOf(".") ) ; // to remove ".ogg" or ".aac"

    try{
        player = MediaPlayer.create(getApplicationContext(), getApplicationContext().getResources().getIdentifier(id_snd, "raw", getApplicationContext().getPackageName()) );
    }
    catch (Exception e){
        Toast.makeText(getApplicationContext(), "can't open file, will try to download it"+"\n"+e , Toast.LENGTH_SHORT).show();
        try{ player = MediaPlayer.create(getApplicationContext(), Uri.parse(url)); }
        catch (Exception e2){ Toast.makeText(getApplicationContext(), "can't download file"+"\n"+e2 , Toast.LENGTH_SHORT).show(); }
    }
}
Diego
  • 569
  • 1
  • 9
  • 28

1 Answers1

0

Get the resourceId of that file using this code

int resId = getResources().getIdentifier(audioName, "raw", getPackageName());

audioName should be without any extension.

And create your player object using this code

player = MediaPlayer.create(context,resId);
Naman Mehta
  • 26
  • 10
  • any reason to call `getIdentifier()` if you have `R.raw.snd_title`? – pskink Apr 22 '18 at 04:45
  • @pskink not really just I have this practice thats why I added it there... – Naman Mehta Apr 22 '18 at 07:50
  • what practice? you want to get `resId`, right? so use `R.raw.snd_title` - its your `resId` – pskink Apr 22 '18 at 07:54
  • @pskink yes, you are right... We can use that.. Should I edit my answer and write directly the `R.raw.snd_title` ? – Naman Mehta Apr 22 '18 at 08:04
  • snd_title.ogg is just one of the files, I wrote it to try in case there was a problem with my id_snd variable. Thanks for telling me how to get the id from the file name, this was what I needed. However, I need to add getApplicationContext(). before getResources() and getPackageName(). – Diego Apr 22 '18 at 14:08
  • @Diego when you are calling `getIdentifier(id_snd, "raw", getApplicationContext().getPackageName())`, the `id_snd` should be of without extension – Naman Mehta Apr 22 '18 at 15:54
  • Yes, I did add `id_snd = id_snd.substring( 0 , id_snd.indexOf(".") ) ;` – Diego Apr 22 '18 at 17:44
  • @Diego you log the url you are getting into the function and manually check whether it is available in your raw directory or not – Naman Mehta Apr 22 '18 at 18:38