Mediaplayer无法准备我的自定义wave文件。准备失败。:状态= 0x1

时间:2019-05-29 19:29:02

标签: java android android-mediaplayer wav

我的媒体播放器有问题。设置数据源后将无法准备。我有带有元数据结尾的自定义wav文件。媒体播放器会正常播放其他wav文件,就像我在其他播放器中的自定义文件一样。

这是我要播放的文件。

public void onItemClick(AdapterView<?> parent, View view, int position, long id){

        String filePath = path+"/"+list_pos[position];

        final MediaPlayer player = new MediaPlayer();
        try {
            player.setDataSource(filePath);
            player.prepare();
            player.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

这是我的原始wav文件。我必须在文件中添加一些元数据,并且我认为可以在末尾添加它。它在其他玩家中正常播放。

public void save(String name,String surname, String title,String description){
        state = false;

        String status = Environment.getExternalStorageState();

        String dateAndTime = new SimpleDateFormat("yyyy-MM-dd',' HH:mm:ss",
                Locale.getDefault()).format(new Date());

        String metadata = name+"#" +surname+"#"+title+"#"+dateAndTime+"#"+description;

        if(Environment.MEDIA_MOUNTED.equals(status)){

            File recording  = new File(fileName);

            try {
                FileOutputStream os = new FileOutputStream(recording);
                os.write(prepareWavHeader(file.size() * Constans.bufferSize));
                for (byte[] b:file) {
                    os.write(b);
                }
                os.write(metadata.getBytes());
                os.close();
            }catch (Exception e){e.printStackTrace();}
        }
    }

    public byte[] prepareWavHeader(int pcmDataLengthInBytes)
    {
        int totalDataLen = pcmDataLengthInBytes + 36;
        byte[] wavHeader = prepareWavFileHeader(16,pcmDataLengthInBytes,totalDataLen,
                Constans.sampleRate,Constans.numberOfChannels,
                Constans.sampleRate*Constans.numberOfChannels*16/8);
        return wavHeader;
    }

    private byte[] prepareWavFileHeader(int bitsPerSample,long totalAudioLen,long totalDataLen,
                                        long longSampleRate, int channels, long byteRate){
        byte[] header = new byte[44];

        header[0] = (byte)'R'; // RIFF/WAVE header
        header[1] = (byte)'I';
        header[2] = (byte)'F';
        header[3] = (byte)'F';
        header[4] = (byte) (totalDataLen & 0xff);
        header[5] = (byte) ((totalDataLen >> 8) & 0xff);
        header[6] = (byte) ((totalDataLen >> 16) & 0xff);
        header[7] = (byte) ((totalDataLen >> 24) & 0xff);
        header[8] = (byte)'W';
        header[9] = (byte)'A';
        header[10] = (byte)'V';
        header[11] = (byte)'E';
        header[12] = (byte)'f'; // 'fmt ' chunk
        header[13] = (byte)'m';
        header[14] = (byte)'t';
        header[15] = (byte)' ';
        header[16] = 16; // 4 bytes: size of 'fmt ' chunk
        header[17] = 0;
        header[18] = 0;
        header[19] = 0;
        header[20] = 1; // format = 1
        header[21] = 0;
        header[22] = (byte) channels;
        header[23] = 0;
        header[24] = (byte) (longSampleRate & 0xff);
        header[25] = (byte) ((longSampleRate >> 8) & 0xff);
        header[26] = (byte) ((longSampleRate >> 16) & 0xff);
        header[27] = (byte) ((longSampleRate >> 24) & 0xff);
        header[28] = (byte) (byteRate & 0xff);
        header[29] = (byte) ((byteRate >> 8) & 0xff);
        header[30] = (byte) ((byteRate >> 16) & 0xff);
        header[31] = (byte) ((byteRate >> 24) & 0xff);
        header[32] = (byte) (2 * 16 / 8); // block align
        header[33] = 0;
        header[34] = (byte) bitsPerSample; // bits per sample
        header[35] = 0;
        header[36] = (byte)'d';
        header[37] = (byte)'a';
        header[38] = (byte)'t';
        header[39] = (byte)'a';
        header[40] = (byte) (totalAudioLen & 0xff);
        header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
        header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
        header[43] = (byte) ((totalAudioLen >> 24) & 0xff);

        return header;
    }

0 个答案:

没有答案