Play and Seek Audio File in java

可紊 提交于 2019-12-11 04:01:23

问题


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.


回答1:


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."



回答2:


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)



来源:https://stackoverflow.com/questions/6950626/play-and-seek-audio-file-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!