3

I'm making some mini java games and I was wondering how I can add sound/music to my programs. I watched a video on youtube and followed the code provided, however I get the following error: java.io.IOException: could not create audio stream from input stream

I noticed that others have asked the same question with the same code but the answers were not helpful. Here is the code:

import sun.audio.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class Project1 
{
public static void main(String[] args)
{
    JFrame frame = new JFrame();
    frame.setSize(200,200);
    frame.setLocationRelativeTo(null);
    JButton button = new JButton("Click me");
    frame.add(button);
    button.addActionListener(new AL());
    frame.setVisible(true);
}
    public static class AL implements ActionListener{
        public final void actionPerformed(ActionEvent e){
            music();
    }
}

    public static void music() 
    {       
        AudioPlayer MGP = AudioPlayer.player;
        AudioStream BGM;
        AudioData MD;

        ContinuousAudioDataStream loop = null;

        try
        {
            InputStream test = new FileInputStream("C:\\Music1.wmv");
            BGM = new AudioStream(test);
            AudioPlayer.player.start(BGM);
            //MD = BGM.getData();
            //loop = new ContinuousAudioDataStream(MD);

        }
        catch(FileNotFoundException e){
            System.out.print(e.toString());
        }
        catch(IOException error)
        {
            System.out.print(error.toString());
        }
        MGP.start(loop);
    }


}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
JavaDude
  • 75
  • 1
  • 3
  • 8
  • 1
    First get sound working in a simple non-Swing non-GUI program. Only then should you try the concepts out in Swing in a background thread. Else your code gets too large and complex that it's hard to know what might be causing what. Simplify then solve. Remember these words. – Hovercraft Full Of Eels Dec 31 '13 at 22:12

3 Answers3

3
InputStream test = new FileInputStream("C:\\Music1.wmv");

Java Sound has no support for WMV & I've not heard of a Service Provider Interface that will support it. You'll need to convert the sound to an older file type.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I've tried using multiple file types. WMV just happened to be what I used in the code at the time. – JavaDude Dec 28 '13 at 14:54
  • I don't know why I missed that comment! Note that most media types are 'container formats' that might support a number of different **encodings** and it is the encodings that are vital. Java Sound only supports older (less compressive) encodings. Try your code with the (non-MP3) media files seen at [my media files](http://pscode.org/media/#sound). They *are* compatible with Java Sound. Another tip. Change code of the form `catch (Exception e) { ..` to `catch (Exception e) { e.printStackTrace(); // very informative! ..` It is more informative than `System.out.print(e.toString());`. – Andrew Thompson Dec 29 '13 at 11:01
  • I was able to get the audio to play using one of your media files and I think the main problem is the file size. The file I'm trying to play is 4846 MB. – JavaDude Dec 29 '13 at 20:32
  • *"I think the main problem is the file size."* The file size will be a problem for any method that attempts to load the entire file at once, but there are ways of playing audio that simply load a few bytes, play them, then load a few more.. They can handle files of ***any*** size. – Andrew Thompson Dec 30 '13 at 01:20
0

Use AudioInputStream for Taking audio files as input, Refer this

swapnil7
  • 808
  • 1
  • 9
  • 22
0

Try using:

InputStream test = new FileInputStream("C:\\Music1.au");

Or:

InputStream test = new FileInputStream("C:\\Music1.wav");

Just convert your file to one of these audio files.

CaptainKnee
  • 139
  • 5