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(); }
}
}