开发者

Play and Seek Audio File in java

开发者 https://www.devze.com 2023-03-26 14:32 出处:网络
I used this class to play my Wav file. Its very good but How to start my wav file on some position (KB or second)?

I used this class to play my Wav file.

Its very good but How to start my wav file on some position (KB or second)?

auline.start();
    int nBytesRead = 0;
    byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];



    try {
        while (开发者_如何学编程nBytesRead != -1) {
            nBytesRead = audioInputStream.read(abData, 0, abData.length);
            System.out.println("s");
            if (nBytesRead >= 0)
                auline.write(abData, 0, nBytesRead);
        }
    } catch (IOException e) {
        return;
    } finally {
        auline.drain();
        auline.close();
    }

This is part of the code.


A Clip(1) makes it easy to start a sound from wherever is needed (in seconds). For an example see the Clip code in the JavaSound info. page.

  1. See especially.
    • Clip.setMicrosecondPosition(long). "Sets the media position in microseconds."
    • Clip.setFramePosition(int) "Sets the media position in sample frames."
    • Clip.setLoopPoints(int,int). "Sets the first and last sample frames that will be played in the loop."


Use this value offset :

0 < offset < lengthOfArray

so it will start reading from the current value of offset and hence write only the read data. Now you are using the value of offset = 0

auline.write(abData, 0, nBytesRead) --- > auline.write(abData, offset, nBytesRead)

where offset is greater than 0 but less than nBytesRead See the Doc #write(byte[], int, int)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号