Playing .wav file in java from file from computer

前端 未结 1 1687
清酒与你
清酒与你 2020-12-22 11:48
package soundTest;

import java.applet.*;
import java.net.*;
import javax.swing.*;
import javax.sound.sampled.*;
import java.io.*;
import java.util.*;

public class          


        
相关标签:
1条回答
  • 2020-12-22 12:26

    First Attempt

    AudioClip clip1 = Applet.newAudioClip(new URL(new File("E0.wav").getAbsolutePath())); 
    

    This is not how you construct a URL to a File. Instead, you should use File#getURI#getURL

    AudioClip clip1 = Applet.newAudioClip(new File("/full/path/to/audio.wav").toURI().toURL());
    

    Second Attempt

    mediafire is returning a html response, not the audio file...You can test it with...

    URL url = new URL("http://www.mediafire.com/listen/tok9j9s1hnogj1y/downloads/E0.wav");    
    try (InputStream is = url.openStream()) {
        int in = -1;
        while ((in = is.read()) != -1) {
            System.out.print((char)in);
        }
    } catch (IOException exp) {
        exp.printStackTrace();
    }
    

    Third Attempt

    You open the clip, but never start it...

    URL url2 = new URL("http://www.villagegeek.com/downloads/webwavs/Austin_Powers_death.wav");
    Clip clip2 = AudioSystem.getClip();
    AudioInputStream ais2 = AudioSystem.getAudioInputStream(url2);
    clip2.open(ais2);
    clip2.start();
    
    0 讨论(0)
提交回复
热议问题