2013-01-04 67 views
1

試圖播放位於R.raw的視頻。我有一個xml數組,我得到要播放的文件。如果我硬編碼,這樣的視頻作品罰款:setVideoUri(uri.parse(res))error

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

但是,如果我取從我的數組視頻,我的錯誤監聽集和視頻不玩了。被解析的字符串與上述相同。這就是我要做的事(修改簡單的代碼):

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

我的XML陣看起來是這樣的:

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

回答

0

因爲R.raw.test其實不是字符串,但ID的int表示您正在訪問的資源。 (你可以在LogCat中打印它的值)
所以,getPackageName() +"/R.raw." + getResources().....將不會工作。

0

擴大Andy Res的(非)答案......他說得對,這不會奏效......但「爲什麼」?

因爲R.raw.myvideo是映射到某個數字的R類中的一個變量(這是int Andy在談論您的URI正在尋找的變量)。

如果你想該字符串在運行時向INT翻譯這裏是你如何做到這一點...

@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){ 

}