[英]Getting input data from resource wav file in "raw" folder
我一直在嘗試使用InputStream
從位於原始文件夾中的wav
文件的字節中讀取緩沖區。
我想我不明白的是如何給出文件的正確位置。 R資源中的wav
文件是一個int,所以我不能這樣做:
InputStream is = new FileInputStream(R.raw.music);
因為FileInputStream
沒有確認int
。
基本上我的代碼是我發現的東西的修改版本:
public void read(short[] musicin) {
try {
// Create a DataInputStream to read the audio data back from the saved file.
InputStream is = new FileInputStream("R.raw.music");
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
int buffsize=512;
// Read the file into the music array.
int i = 0;
while (i<buffsize&&dis.available() > 0) {
musicin[i] = dis.readByte();
i++;
}
// Close the input streams.
dis.close();
} catch (Throwable t) {
Log.e("AudioTrack","Playback Failed");
}
}
那么我怎樣才能從原始文件夾中讀取呢?
在你的活動課上
Context c;
c=this;
new yourClass(c);
在你的課上
public yourclass(Context context)
{
InputStream in = context.getResources().openRawResource(R.raw.yourfilename);
}
我所做的是:
1-在線程開始之前將其添加到活動中:
final Context con;
con=this;
2-調用線程中的類
new Wav_File_Reader();
. . .
Wav_File_Reader.read(musicin,con);
2-將我的類修改為靜態:
public static void read(short[] musicin, Context ctx ) {
try {
// InputStream is = getBaseContext().getResources().openRawResource(R.raw.music);
InputStream is = ctx.getResources().openRawResource(R.raw.music);
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
int buffsize=512;
// Read the file into the music array.
int i = 0;
short in[]=new short[200000];
//while (dis.available() > 0) {
//while (i<buffsize&&dis.available() > 0) {
while (i<200000&&dis.available() > 0) {
//musicin[i] = dis.readByte();
//in[i]=dis.readByte();
in[i]=dis.readShort();
i++;
}
// Close the input streams.
dis.close();
} catch (Throwable t)
{
Log.e("AudioTrack","Playback Failed");
}
}
我能夠直接從R.raw
文件夾中讀取。
好吧,InputStream 實現從一開始就引人注目,將其更改為:
InputStream is = this.getResources().openRawResource(R.raw.music);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.