不能玩的MediaPlayer倒.wav文件 [英] Can't play a reversed .wav file with MediaPlayer

查看:213
本文介绍了不能玩的MediaPlayer倒.wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个记录音频的应用程序,节省样品到SD卡上,然后播放出来,使用记录和播放按钮。我需要扭转这种样本。我可以做这一切与反向样品以不同的名称保存到SD卡上。最初的样本 test.wav 和扭转同一样品保存为 revFile.wav 。当我尝试玩 revFile.wav Android的说,它不能播放这种格式。

我litterally把样品中的数组,然后逆转的内容,东西告诉我,有可能是在那首先需要分条,任何想法样品开始的头信息。谢谢。

这是我迄今。

 公共类录音机延伸活动{
    MediaRecorder myRecorder = NULL;
    DataInputStream以DIS = NULL;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
    }
    公共无效onClickPlay(视图v){
        Log.v(onClickplay,点击播放);        尝试{
            MediaPlayer的熔点为新的MediaPlayer();
            mp.setDataSource(Environment.getExternalStorageDirectory()的getPath()+/test.wav);
            MP prepare()。
            mp.start();
        }赶上(例外E3){
            e3.printStackTrace();
        }
        TextView的文本=(TextView的)findViewById(R.id.TextView01);
        text.setText(打);
    }    公共无效onClickRecord(视图v){
        Log.v(onClickRecord,记录点击);        文件路径= Environment.getExternalStorageDirectory();
        Log.v(文件路径,+ path.getAbsolutePath());        档案文件=新的文件(路径,test.wav);
        如果(file.exists()){
            file.delete();
        }
        path.mkdirs();
        Log.v(文件路径,+ file.getAbsolutePath());
        myRecorder =新MediaRecorder();
        myRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        myRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        myRecorder.setAudioEn codeR(MediaRecorder.AudioEn coder.AMR_NB);        myRecorder.setOutputFile(file.getAbsolutePath());
        Log.i(myrecorder,即将prepare记录);
        尝试{
            myRecorder prepare()。
        }赶上(例外五){
            e.printStackTrace();
        }        Log.i(myrecorder,prepared);
        myRecorder.start(); //记录现在开始
        Log.i(myrecorder,记录);
        TextView的文本=(TextView的)findViewById(R.id.TextView01);
        text.setText(记录);
    }    公共无效onClickStop(视图v){
        Log.v(onClickStop,停止点击);            尝试{
             myRecorder.stop();
             myRecorder.reset(); //可以回到setAudioSource重用对象()步
             myRecorder.release(); //现在对象不能重复使用
            }赶上(例外五){}            TextView的文本=(TextView的)findViewById(R.id.TextView01);
             text.setText(停止录制);        }公共无效onClickReverse(视图v){
        Log.v(onClickReverse,反向点击);        文件f = Environment.getExternalStorageDirectory();
        字符串路径= f.getAbsolutePath();
        路径=路径+/test.wav
        Log.v(路径=,+路径);
        Log.v(DIR =,+ f.getAbsolutePath());
        Log.v(测试文件存在=?,+ f.getAbsolutePath()+/ test.wav);        文件F2 =新的文件(路径);
        Log.v(F2 =,+ f2.getAbsolutePath());        尝试{
            InputStream为=新的FileInputStream(F2);
            二BufferedInputStream为=新的BufferedInputStream(是);
            DIS =新DataInputStream所(之二);
        }赶上(例外五){
            e.printStackTrace();
        }        INT文件长度=(int)的f2.length();
        字节[]缓冲区=新的字节[文件长度]        / *文件reversedFile = Environment.getExternalStorageDirectory();
          文件revFile =新的文件(reversedFilereversedFile.wav);
          Log.v(reversedfile路径,+ revFile.getAbsolutePath());          如果(revFile.exists()){
              revFile.delete();
          }          reversedFile.mkdirs();
    * /        字节[]的字节数组=新的字节[文件长度+1]
        Log.v(字节组大小=,+ byteArray.length);        尝试{
            而(dis.read(缓冲液)!= - 1){
                dis.read(缓冲液);
                Log.v(即将读缓冲器,缓冲区);                的字节数组=缓冲;
            }            Log.v(缓冲区大小=,+ buffer.length);
        }赶上(IOException异常五){
            e.printStackTrace();
        }          字节[] = tempArray新的字节[文件长度]          INT J = 0;
          的for(int i = byteArray.length-1; ​​I> = 0;我 - ){
              tempArray [J ++] =的字节数组[我]
          }          文件revPath = Environment.getExternalStorageDirectory();
          Log.v(revpath路径,+ revPath.getAbsolutePath());          文件revFile =新的文件(revPathrevFile.wav);
          Log.v(revfile路径,+ revFile.getAbsolutePath());
          如果(revFile.exists()){
              revFile.delete();
          }          revPath.mkdirs();          尝试{
              OutputStream的OS =新的FileOutputStream(revFile);
              BOS的BufferedOutputStream =新的BufferedOutputStream(OS);
              DataOutputStream类DOS =新的DataOutputStream类(BOS);
              Log.v(temparray大小=,+ tempArray.length);
              dos.write(tempArray);
              dos.flush();
              dos.close();
          }赶上(例外五){
              e.printStackTrace();
          }          尝试{
              MediaPlayer的熔点为新的MediaPlayer();
              mp.setDataSource(Environment.getExternalStorageDirectory()的getPath()
                                                                    +/ revFile.wav);              MP prepare()。
              mp.start();
         }赶上(例外E3){
                e3.printStackTrace();
         }
          TextView的文本=(TextView的)findViewById(R.id.TextView01);
           text.setText(打颠倒文件);
      }} // onclickrev结束


解决方案

有是.WAV头在的链接文本

另外请注意,文件中的所有数据存储为Little Endian顺序(1多字节数低位字节存储在最低的地址......),所以你不能只是扭转字节。你需要看多少字节宽各样品,(通常是16,但检查头),并扭转他们在大小的区块。

I've created an app that records audio, saves the sample to the sd card then plays it back, using the record and play buttons. I need to reverse this sample. I can do all this and the the reversed sample is saved on the SD card under a different name. The original sample is test.wav and the same sample reversed is save as revFile.wav. when i try play revFile.wav android says it can't play this format.

I've litterally put the sample in an array then reversed the contents, something is telling me that there could be header info at the start of the sample that needs striping first, any ideas. thanks.

Here's what i have so far.

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

解决方案

There is a good description of the .WAV header at link text

Also note that all data in the file is stored as Little Endian order (low order byte of 1 multi byte number is stored at the lowest address....) so you can't just reverse the bytes. you need to see how many bytes wide each sample is, (usually 16, but check the header) and reverse them in chunks of that size

这篇关于不能玩的MediaPlayer倒.wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆