2015-12-15 126 views
0

我有一個SoundManager類和Sound類設置,以便我可以從我的代碼中的任何位置播放聲音。我的問題是,在家時,聲音片段播放良好(將其轉換爲.wav的正確音頻格式後)。然而,在學校,該程序崩潰,說音頻格式不支持。有沒有一種方法可以讓音頻在任何系統上運行,而不管Java版本如何?Java:聲音傳輸問題

感謝

+2

的Wav應始終支持,但不一定是所有采樣率等等。你得到的確切例外信息是什麼? –

+1

@ greg-449由於我目前不在學校,所以我不確定確切的消息,但是我的'.wav'的音頻格式是:'PCM_SIGNED 44100.0Hz,16位,立體聲,4字節/幀, endian',它說學校不支持 –

+1

這看起來像普通的CD質量音頻。系統可能不支持「小端」或類似的東西。您將不得不查看輸出SourceDataLine支持的格式,以查看它是否與支持的格式匹配。 –

回答

2

你可以看到,在Java聽起來使用像一臺機器上的API支持格式:

public static void displayMixerInfo() 
{ 
    Mixer.Info [] mixersInfo = AudioSystem.getMixerInfo(); 

    for (Mixer.Info mixerInfo : mixersInfo) 
    { 
    System.out.println("Mixer: " + mixerInfo.getName()); 

    Mixer mixer = AudioSystem.getMixer(mixerInfo); 

    Line.Info [] sourceLineInfo = mixer.getSourceLineInfo(); 
    for (Line.Info info : sourceLineInfo) 
     { 
     showLineInfo(info); 
     } 

    Line.Info [] targetLineInfo = mixer.getTargetLineInfo(); 
    for (Line.Info info : targetLineInfo) 
     { 
     showLineInfo(info); 
     } 
    } 
} 


private static void showLineInfo(Line.Info lineInfo) 
{ 
    System.out.println(" " + lineInfo.toString()); 

    if (lineInfo instanceof DataLine.Info) 
    { 
    DataLine.Info dataLineInfo = (DataLine.Info)lineInfo; 

    AudioFormat [] formats = dataLineInfo.getFormats(); 
    for (AudioFormat format : formats) 
     { 
     System.out.println(" " + format.toString()); 
     } 
    } 
} 

在我的Mac此顯示:

Mixer: Default Audio Device 
    interface SourceDataLine supporting 14 audio formats, and buffers of at least 32 bytes 
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian 
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian 
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian 
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian 
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian 
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian 
    interface Clip supporting 14 audio formats, and buffers of at least 32 bytes 
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian 
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian 
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian 
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian 
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian 
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian 
    interface TargetDataLine supporting 14 audio formats, and buffers of at least 32 bytes 
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian 
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian 
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian 
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian 
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian 
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian 
Mixer: Built-in Microphone 
    interface TargetDataLine supporting 14 audio formats, and buffers of at least 32 bytes 
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian 
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian 
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian 
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian 
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian 
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian 
Mixer: Built-in Output 
    interface SourceDataLine supporting 14 audio formats, and buffers of at least 32 bytes 
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian 
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian 
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian 
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian 
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian 
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian 
    interface Clip supporting 14 audio formats, and buffers of at least 32 bytes 
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian 
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 24 bit, mono, 3 bytes/frame, big-endian 
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian 
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, little-endian 
    PCM_SIGNED unknown sample rate, 24 bit, stereo, 6 bytes/frame, big-endian 
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian 
    PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, big-endian 
Mixer: Port Built-in Microphone 
    MICROPHONE source port 
Mixer: Port Built-in Output 
    HEADPHONE target port