Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Javazoom修剪wav文件的方法有时是有效的_Java_Android_Audio_Wav - Fatal编程技术网

使用Javazoom修剪wav文件的方法有时是有效的

使用Javazoom修剪wav文件的方法有时是有效的,java,android,audio,wav,Java,Android,Audio,Wav,我正在研究一种方法,将wav文件裁剪为用户所做的选择。基本思想是使用Javazoom的WaveFile类编写一个wav文件,该文件只包含用户选择的声音数据。问题是,我编写的代码有一半的时间工作得很好,另一半时间生成静态代码。在完全相同的情况下,它似乎起作用,而不是起作用。wav文件由MediaPlayer在其他时间加载,并以其他方法作为输入流。这可能是问题的根源吗?我试图关闭流并发布MediaPlayer,但仍然存在同样的问题 public void TrimToSelection(double

我正在研究一种方法,将wav文件裁剪为用户所做的选择。基本思想是使用Javazoom的WaveFile类编写一个wav文件,该文件只包含用户选择的声音数据。问题是,我编写的代码有一半的时间工作得很好,另一半时间生成静态代码。在完全相同的情况下,它似乎起作用,而不是起作用。wav文件由MediaPlayer在其他时间加载,并以其他方法作为输入流。这可能是问题的根源吗?我试图关闭流并发布MediaPlayer,但仍然存在同样的问题

public void TrimToSelection(double startTime, double endTime){ // Time in seconds
    InputStream wavStream = null; // InputStream to stream the wav to trim
    File trimmedSample = null;  // File to contain the trimmed down sample
    File sampleFile = new File(samplePath); // File pointer to the current wav sample

    // If the sample file exists, try to trim it
    if (sampleFile.isFile()){
        trimmedSample = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "trimmedSample.wav");
        if (trimmedSample.isFile()) trimmedSample.delete(); // Delete if already exists

        // Trim the sample down and write it to file
        try {
            wavStream = new BufferedInputStream(new FileInputStream(sampleFile));
            // Javazoom class WaveFile is used to write the wav
            WaveFile waveFile = new WaveFile();
            waveFile.OpenForWrite(trimmedSample.getAbsolutePath(), (int)audioFormat.getSampleRate(), (short)audioFormat.getSampleSizeInBits(), (short)audioFormat.getChannels());
            // The number of bytes of wav data to trim off the beginning
            long startOffset = (long)(startTime * audioFormat.getSampleSizeInBits() * audioFormat.getSampleRate() / 4);
            // The number of bytes to copy
            long length = (long)(endTime * audioFormat.getSampleSizeInBits() * audioFormat.getSampleRate() / 4) - startOffset;
            wavStream.skip(44); // Skip the header
            wavStream.skip(startOffset);
            byte[] buffer = new byte[1024 * 16];
            int bufferLength;
            for (long i = startOffset; i < length + startOffset; i += buffer.length){
                bufferLength = wavStream.read(buffer);
                short[] shorts = new short[buffer.length / 2];
                ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
                waveFile.WriteData(shorts, shorts.length);
            }
            waveFile.Close(); // Complete writing the wave file
            wavStream.close(); // Close the input stream
        } catch (IOException e) {e.printStackTrace();}
        finally {
            try {if (wavStream != null) wavStream.close();} catch (IOException e){}
        }
    }
    // Delete the original wav sample
    sampleFile.delete();
    // Copy the trimmed wav over to replace the sample
    trimmedSample.renameTo(sampleFile);
}

更新:我更改了long startOffset=longstartTime*audioFormat.getSampleSizeInBits*audioFormat.getSampleRate/4;to long startOffset=longstartTime*audioFormat.getSampleSizeInBits*longaudioFormat.getSampleRate/4;长度也是一样。出于某种原因,改变演员阵容的位置似乎解决了静态问题,尽管我不知道为什么。现在,我想我需要调整缓冲区循环,因为示例的结尾被切断了。

我不确定问题的根源,但这可能会帮助您找到答案:是一个开源铃声创建者。它内置了一个波形编辑器。我曾经下载过它,使用波形编辑器,并为一个有趣的项目定制了它。这可能会帮助你到达你需要去的地方。享受

我的更新中的问题是,startTime to long的演员阵容有效地舍入到最接近的秒数。下面的代码是有效的,尽管我仍然不确定我最初的铸造方法为什么不起作用

public void TrimToSelection(double startTime, double endTime){
    InputStream wavStream = null; // InputStream to stream the wav to trim
    File trimmedSample = null;  // File to contain the trimmed down sample
    File sampleFile = new File(samplePath); // File pointer to the current wav sample

    // If the sample file exists, try to trim it
    if (sampleFile.isFile()){
        trimmedSample = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC), "trimmedSample.wav");
        if (trimmedSample.isFile()) trimmedSample.delete();

        // Trim the sample down and write it to file
        try {
            wavStream = new BufferedInputStream(new FileInputStream(sampleFile));
            // Javazoom WaveFile class is used to write the wav
            WaveFile waveFile = new WaveFile();
            waveFile.OpenForWrite(trimmedSample.getAbsolutePath(), (int)audioFormat.getSampleRate(), (short)audioFormat.getSampleSizeInBits(), (short)audioFormat.getChannels());
            // The number of bytes of wav data to trim off the beginning
            long startOffset = (long)(startTime * audioFormat.getSampleRate()) * audioFormat.getSampleSizeInBits() / 4;
            // The number of bytes to copy
            long length = ((long)(endTime * audioFormat.getSampleRate()) * audioFormat.getSampleSizeInBits() / 4) - startOffset;
            wavStream.skip(44); // Skip the header
            wavStream.skip(startOffset);
            byte[] buffer = new byte[1024];
            int i = 0;
            while (i < length){
                if (length - i >= buffer.length) {
                    wavStream.read(buffer);
                }
                else { // Write the remaining number of bytes
                    buffer = new byte[(int)length - i];
                    wavStream.read(buffer);
                }
                short[] shorts = new short[buffer.length / 2];
                ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
                waveFile.WriteData(shorts, shorts.length);
                i += buffer.length;
            }
            waveFile.Close(); // Complete writing the wave file
            wavStream.close(); // Close the input stream
        } catch (IOException e) {e.printStackTrace();}
        finally {
            try {if (wavStream != null) wavStream.close();} catch (IOException e){}
        }
    }
}

谢谢,那肯定很有帮助。它完成修剪一个wav文件使用基本上相同的方法,我。我可能已经解决了代码中的主要问题,但我不知道为什么。请参阅原始帖子中的我的编辑。