Java Sound API unterstützt keine nativen MP3-Dateien. Um MP3-Dateien abzuspielen, können Sie die JMF-basierte mp3plugin.jar zum Laufzeitklassenpfad der Anwendung hinzufügen.
Um eine MP3-Datei abzuspielen, können Sie das folgende Snippet verwenden:
import javax.sound.sampled.*; import java.io.File; public class MP3Player { public static void main(String[] args) { try { // Open the MP3 file AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("your_mp3_file.mp3")); // Get the audio format AudioFormat audioFormat = audioInputStream.getFormat(); // Create a data line to play the audio SourceDataLine dataLine = AudioSystem.getSourceDataLine(audioFormat); dataLine.open(audioFormat); // Start playing the audio dataLine.start(); // Read the audio data from the file and write it to the data line byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = audioInputStream.read(buffer)) != -1) { dataLine.write(buffer, 0, bytesRead); } // Stop playing the audio dataLine.stop(); // Close the data line and audio input stream dataLine.close(); audioInputStream.close(); } catch (Exception e) { e.printStackTrace(); } } }
Das obige ist der detaillierte Inhalt vonWie spielt man MP3-Dateien mithilfe der Java Sound API mit JMF ab?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!