Java Can';t使用MediaPlayer播放反向的.wav文件
我创建了一个应用程序,可以录制音频,将样本保存到sd卡,然后使用录制和播放按钮播放。我需要把这个样品倒过来。我可以做所有这些,并且反转的样本以不同的名称保存在SD卡上。原始样本是Java Can';t使用MediaPlayer播放反向的.wav文件,java,android,wav,Java,Android,Wav,我创建了一个应用程序,可以录制音频,将样本保存到sd卡,然后使用录制和播放按钮播放。我需要把这个样品倒过来。我可以做所有这些,并且反转的样本以不同的名称保存在SD卡上。原始样本是test.wav,相同的样本被反向保存为revFile.wav。当我尝试播放revFile.wavandroid说它不能播放这种格式 我随便把样本放在一个数组中,然后把内容颠倒过来,有东西告诉我,在样本的开头可能有标题信息,需要首先进行条带化,任何想法。谢谢 这是我到目前为止所拥有的 public class recor
test.wav
,相同的样本被反向保存为revFile.wav
。当我尝试播放revFile.wav
android说它不能播放这种格式
我随便把样本放在一个数组中,然后把内容颠倒过来,有东西告诉我,在样本的开头可能有标题信息,需要首先进行条带化,任何想法。谢谢
这是我到目前为止所拥有的
public class recorder extends Activity {
MediaRecorder myRecorder = null;
DataInputStream dis = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickPlay(View v){
Log.v("onClickplay", "play clicked");
try{
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(Environment.getExternalStorageDirectory().getPath() + "/test.wav");
mp.prepare();
mp.start();
} catch(Exception e3) {
e3.printStackTrace();
}
TextView text = (TextView)findViewById(R.id.TextView01);
text.setText("playing");
}
public void onClickRecord(View v){
Log.v("onClickRecord", "record clicked");
File path = Environment.getExternalStorageDirectory();
Log.v("file path", ""+path.getAbsolutePath());
File file = new File(path, "test.wav");
if(file.exists()){
file.delete();
}
path.mkdirs();
Log.v("file path", ""+file.getAbsolutePath());
myRecorder = new MediaRecorder();
myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
myRecorder.setOutputFile(file.getAbsolutePath());
Log.i("myrecorder", "about to prepare recording");
try{
myRecorder.prepare();
} catch(Exception e) {
e.printStackTrace();
}
Log.i("myrecorder", "prepared");
myRecorder.start(); // Recording is now started
Log.i("myrecorder", "recording");
TextView text = (TextView)findViewById(R.id.TextView01);
text.setText("recording");
}
public void onClickStop(View v){
Log.v("onClickStop", "stop clicked");
try{
myRecorder.stop();
myRecorder.reset(); // You can reuse the object by going back to setAudioSource() step
myRecorder.release(); // Now the object cannot be reused
}catch(Exception e){}
TextView text = (TextView)findViewById(R.id.TextView01);
text.setText("recording stopped");
}
public void onClickReverse(View v){
Log.v("onClickReverse", "reverse clicked");
File f = Environment.getExternalStorageDirectory();
String path = f.getAbsolutePath();
path = path + "/test.wav";
Log.v("path = ", ""+path);
Log.v("dir = ", ""+f.getAbsolutePath());
Log.v("test file exists? = ", ""+f.getAbsolutePath()+"/test.wav");
File f2 = new File(path);
Log.v("f2 = ", ""+f2.getAbsolutePath());
try {
InputStream is = new FileInputStream(f2);
BufferedInputStream bis = new BufferedInputStream(is);
dis = new DataInputStream(bis);
} catch (Exception e) {
e.printStackTrace();
}
int fileLength = (int)f2.length();
byte[] buffer = new byte[fileLength];
/*File reversedFile = Environment.getExternalStorageDirectory();
File revFile = new File(reversedFile, "reversedFile.wav");
Log.v("reversedfile path", ""+ revFile.getAbsolutePath());
if(revFile.exists()){
revFile.delete();
}
reversedFile.mkdirs();
*/
byte[] byteArray = new byte[fileLength +1];
Log.v("bytearray size = ", ""+byteArray.length);
try {
while(dis.read(buffer) != -1 ) {
dis.read(buffer);
Log.v("about to read buffer", "buffer");
byteArray = buffer;
}
Log.v(" buffer size = ", ""+ buffer.length);
} catch (IOException e) {
e.printStackTrace();
}
byte[] tempArray = new byte[fileLength];
int j=0;
for (int i=byteArray.length-1; i >=0; i--) {
tempArray[ j++ ] = byteArray[i];
}
File revPath = Environment.getExternalStorageDirectory();
Log.v("revpath path", ""+revPath.getAbsolutePath());
File revFile = new File(revPath, "revFile.wav");
Log.v("revfile path ", ""+revFile.getAbsolutePath());
if(revFile.exists()){
revFile.delete();
}
revPath.mkdirs();
try {
OutputStream os = new FileOutputStream(revFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
Log.v("temparray size = ", ""+ tempArray.length);
dos.write(tempArray);
dos.flush();
dos.close();
} catch (Exception e) {
e.printStackTrace();
}
try{
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(Environment.getExternalStorageDirectory().getPath()
+"/revFile.wav");
mp.prepare();
mp.start();
} catch(Exception e3) {
e3.printStackTrace();
}
TextView text = (TextView)findViewById(R.id.TextView01);
text.setText("playing reversed file");
}
}// end of onclickrev
我很确定你是对的,问题是你不能仅仅反转文件中的字节来反转波形,因为你正在破坏头信息。您应该试试看是否有一个好的库来做这件事,因为我对.wav文件的使用经验不多。在
还要注意的是,文件中的所有数据都以小尾端顺序存储(1个多字节数的低阶字节存储在最低地址…),因此不能仅反转字节。您需要查看每个示例有多少字节宽(通常为16字节,但请检查标题),并将其反转为该大小的块。WAV文件格式包括一个44字节的标题块。大多数WAV文件由这个44字节的头文件组成,后面是实际的样本数据。因此,要反转WAV文件,您应该首先从原始文件复制44字节的头文件,然后在头文件之后从原始文件复制反转的样本数据。如果您只是颠倒原始整个文件的字节顺序,它肯定不会工作。如果您复制头文件,然后反转文件其余部分的字节顺序,那么它也不起作用(实际上,它会起作用,除非您得到的只是噪声)。实际上,您需要反转帧,其中帧大小取决于每个采样的字节数以及文件是立体声还是单声道(例如,如果文件是立体声,每个采样2个字节,那么每个帧是4个字节) 请注意,并非所有WAV文件都是这样的“规范”。WAV文件实际上是RIFF文件的一种变体,因此从技术上讲,您需要更复杂的代码来查找原始文件中标题和示例数据的各个部分。然而,大多数WAV文件只是头文件,后跟示例(如果您自己录制音频,这肯定是正确的),在这种情况下,您可以节省大量工作
Joe Cullity
的链接很好地描述了WAV文件格式。好的,谢谢,我是android新手,以前从未使用过音频,但这不仅仅是一个查找头文件中有多少字节,然后将其剥离的例子。我可以反转数组的其余部分,然后在前面添加头字节?