2017-01-09 52 views
0

這個數組的問題和答案, R.raw.ikhlas例子是回答R.raw.jwbalikhlas如何讓原始音頻文件的android的工作室形式的答案

int[] rawQuetion = {R.raw.alfalaq, R.raw.alikhlas, R.raw.alkafirun, R.raw.allahab}; 
int [] rawAnswer={R.raw.jwbaliklas}; 

這個問題方法隨機問題

//fisher-yates Shuffle 
public void playSoal() { 
    shuffleArray(rawQuetion); 
    try{ 
     int idx = new Random().nextInt(rawQuetion.length); 
     mp = MediaPlayer.create(this, rawQuetion[idx]); 
     mp.start(); 

    }catch(Exception e){ 
     Log.e("ERROR", "Media Player", e); 
     mp = null; 
     mp.release(); 
     mp.stop(); 
     e.printStackTrace(); 

    } 
} 

static void shuffleArray(int[] arr) 
{ 
    Random rnd = new Random(); 
    for (int i = arr.length - 1; i > 0; i--) 
    { 
     int index = rnd.nextInt(i + 1); 
     // Swap 
     int a = arr[index]; 
     arr[index] = arr[i]; 
     arr[i] = a; 
    } 
} 

我想在隨機選擇的問答題,將在這裏回答

public void audioFile() throws IOException{ 
    InputStream is = getResources().openRawResource(R.raw.jwbaliklas);//I want this to be obtained from the above array 

    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    BufferedInputStream in = null; 
    in = new BufferedInputStream(is); 

    int read; 
    byte[] buff = new byte[1024]; 
    while ((read = in.read(buff)) > 0) 
    { 
     out.write(buff, 0, read); 
    } 
    out.flush(); 
    byte[] audioBytes = out.toByteArray(); 

    for (int i = 0; i < audioBytes.length; i++) { 
     audioBytes[i] = (byte) ((audioBytes[i]) & 0xFF);  } 

    absNormalizedSignal = hitungFFT(audioBytes); 
    AppLog.logString("===== From audio File"); 


} 

回答

0

如果您要爲要打開的資源傳遞數組id,則可以直接從整數數組中使用索引(只要它在audioFile範圍內可見即可)。

public void audioFile(@RawRes int i) throws IOException { 
    InputStream is = getResources().openRawResource(rawQuestion[i]); 
    ... 
} 

而且,你必須改變包括註釋@RawRes

@RawRes int[] rawQuestion = {R.raw.alfalaq, R.raw.alikhlas, R.raw.alkafirun, R.raw.allahab}; 
@RawRes int[] rawAnswer = {R.raw.jwbaliklas}; 
+0

我明白了。但我仍然困惑,我怎麼能寫R.raw.jwbaliklas代碼是R.raw.alikhlas答案。這樣就有了InputStream is = getResources()。openRawResource(rawQuetion [i]); R.raw.jwbaliklas –

+0

你應該使用'@ RawRes'來註釋變量(正如我上面編輯的) –

+0

謝謝,我會先試一試。非常感謝你 –

相關問題