1

Trying to play a video located in R.raw. I have an xml array that I get the file that is to be played. The video works fine if I hardcode it, like this:

VideoView myVideoView = (VideoView)findViewById(R.id.videoview0);
myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName()+"/"+R.raw.test));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();

However, if I fetch the video from my array, my Error listener sets of and the video do not play. The string that is parsed is excatly the same as the above. This is how I do it (code modified for simplicity):

String uriParse = "android.resource://" + getPackageName() +"/R.raw." + getResources().getResourceEntryName(xmlArr.getResourceId(intVideoToPlay));
myVideoView.setVideoURI(Uri.parse(uriParse));
myVideoView.setMediaController(new MediaController(this));
myVideoView.requestFocus();
myVideoView.start();

My xml-array looks like this:

string-array name="arrTest"
item>@raw/test1 /item
item>@raw/test2 /item
item>@raw/test3 /item
/string-array

2 Answers2

0

Because R.raw.test actually is not a string, but an int representation of the id of the resource you are accessing. (you can print its value in LogCat)
So, getPackageName() +"/R.raw." + getResources()..... wont' work.

Andy Res
  • 15,963
  • 5
  • 60
  • 96
0

Expanding on Andy Res's (non) answer... he's right that won't work... but "why"?

Because R.raw.myvideo is a variable in the R class which maps to some number (which is the int Andy is talking about that your URI is looking for).

If you want to translate that String at runtime to that int here's how you do it...

    @SuppressWarnings("rawtypes")
    public static int getResourceId(String name,  Class resType){       
        try {          
            return resType.getField(name).getInt(null);        
        }
        catch (Exception e) {
           // Log.d(TAG, "Failure to get drawable id.", e);
        }
        return 0;
    }

   int resId = getResourceId(
    getResources().getResourceEntryName(xmlArr.getResourceId(intVideoToPlay))
   , R.raw.class); //this will be 0 if it can't find the String in the raw class or the relevant id
  //keep in mind that I have no idea if your xmlArr method is valid... make sure to log what it's returning to verify that you're getting the String you're expecting from it
try{

   String uriParse = "android.resource://" + getPackageName() +"/" + resId;
   //... now you can use your uriParse as you do above.

}catch(Exception e){

}
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236