2017-05-31 39 views
0

我有原始文件夾和裏面,我已經添加3 mp3文件。和我獲取文件名是這樣的:如何在Android中設置openRawResourceId值?

Field[] fields=R.raw.class.getFields(); 
for(int count=0; count < fields.length; count++){ 
    Log.e("RawName: ", fields[count].getName()); 
} 
String resid=fields[0].getName(); 

我只需要第0個位置的文件。現在,我已經這樣設置值,

AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.test); 
    // Build and start first player 
    MediaPlayer player1 = MediaPlayer.create(this, R.raw.test); 
    player1.start(); 
    MediaPlayer player2 = MediaPlayer.create(this, R.raw.test); 

我的問題是如何設置的值渣油中說:「R.raw.test」。

回答

1
Field[] fields=R.raw.class.getFields(); 
    for(int count=0; count < fields.length; count++){ 
     Log.e("RawName: ", fields[count].getName()); 
    } 
    String resid=fields[0].getName(); 
    try { 
     Class res = R.raw.class; 
     Field field = res.getField(resid); 
     rawId = field.getInt(null); 
    } 
    catch (Exception e) { 
     Log.e("MyTag", "Failure to get drawable id.", e); 
    } 

    /*music repeat*/ 
    afd = getResources().openRawResourceFd(rawId); 
    // Build and start first player 
    player1 = MediaPlayer.create(this, rawId); 
    player1.start(); 
    player2 = MediaPlayer.create(this, rawId); 
    player1.setNextMediaPlayer(player2); 
相關問題