views:

62

answers:

2

I was trying out the method of creating a background music for a java program, but it displayed an IO excedption error when i clicked the play button.

package javaentertainment;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.*;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class Music
{

    public static void main(String args[])
    {
        JFrame frame=new JFrame();
        frame.setSize(100,100);
        JButton button=new JButton("P L A Y");
        frame.add(button);
        button.addActionListener(new AL());
        frame.show();
    }

   public static class AL implements ActionListener
   {

        public void actionPerformed(ActionEvent e) {
            music();
        }
    }

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

        try
        {
            BGM = new AudioStream(new FileInputStream("Vision.wmv"));
            MD=BGM.getData();
            loop=new ContinousAudioDataStream(MD);

        }
        catch (IOException ex)
        {
           System.out.println(ex);
        }

        MGP.start(loop); // word loop was underlined by netbeans
    }
}

When I run the program and click on play it displays the following error, java.io.IOException: could not create audio stream from input stream

A: 

I would be surprised if Java can hand Windows Media format samples - try converting the .wmv to a .wav file and see if it works then.

Will A
Well now the previous error is ok but it displays java.io.IOException: could not create AudioData object
Yoosuf
+1  A: 

You should use JMF (Java Media Framework). For your interest: The list of accepted formats can be found here.

In short, it supports AIFF, AVI, GSM, MVR, MID, MPG, MP2, MOV, AU and WAV files.

But there is a workarond as stated here:

On a side note, if you add a mime-setting in JMFRegistry to map Windows Media content (such as .asf and .wmv) to the content-type "video/mpeg", JMF can actually play Windows Media or any other DirectShow file (and only file - http wont work).

Daniel