sound in java

import java.awt.*;

import java.io.FileInputStream;

import java.applet.*;

import sun.audio.*;

import hsa.Console;

import java.awt.*;

import java.io.FileInputStream;

import java.applet.*;

import sun.audio.*;

import hsa.Console;

import javax.swing.*;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JTextField;

import java.applet.*;

public class PlayMusic

{

static JTextArea Musicarea;

JFrame frame3;

// Create an array for five pieces of music

static AudioData[] audioData = new AudioData [5];

static AudioDataStream[] audioStream = new AudioDataStream [5];

static ContinuousAudioDataStream[] continuousAudioDataStream =

new ContinuousAudioDataStream [5];

public void Music ()

{

frame3 = new JFrame ("Box");

frame3.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

addComponentsToPane3 (frame3.getContentPane ());

frame3.pack ();

frame3.setSize (330, 300);

frame3.setLocationRelativeTo (null);

frame3.setVisible (true);

}

public void addComponentsToPane3 (Container pane3)

{

pane3.setLayout (null);

Musicarea = new JTextArea ();

Musicarea.setEditable (false);

Musicarea.setBounds (0, 0, 330, 210);

pane3.add (Musicarea);

}

public static void main (String[] args)

{

char c;

int choice, newChoice;

int ch = 0;

// JFrame c = new JFrame

/*c.println ("This program will randomly play one of five pieces " +

"music. It will start a");

c.println ("different piece of music each time you press a key. " +

"Press 'q' to quit"); */

//c.print ("\nLoading music... ");

loadMusic ("music0.au", 0);

loadMusic ("music1.au", 1);

loadMusic ("music2.au", 2);

loadMusic ("music3.au", 3);

loadMusic ("music4.au", 4);

//c.println ("Done.");

choice = (int) (Math.random () * 5);

//Musicarea.setText ("\nPlaying song #" + choice);

while (true)

{

Musicarea.setText ("\nPlaying song #" + choice);

loop (choice);

//ch = c.getChar ();

if (ch == 'q')

break;

do

newChoice = (int) (Math.random () * 5);

while (choice == newChoice);

stop (choice);

choice = newChoice;

}

} // main method

static void loadMusic (String fileName, int index)

{

try

{

FileInputStream fis = new FileInputStream (fileName);

AudioStream tempAudioStream = new AudioStream (fis);

audioData [index] = tempAudioStream.getData ();

audioStream [index] = null;

continuousAudioDataStream [index] = null;

}

catch (Exception e)

{

System.out.println ("Unable to load " + fileName + ": " + e);

System.exit (0);

}

} // loadMusic method

static void loop (int index)

{

continuousAudioDataStream [index] =

new ContinuousAudioDataStream (audioData [index]);

AudioPlayer.player.start (continuousAudioDataStream [index]);

} // loop (void)

static void play (int index)

{

audioStream [index] = new AudioDataStream (audioData [index]);

AudioPlayer.player.start (audioStream [index]);

} // play (void)

static void stop (int index)

{

if (audioStream [index] != null)

AudioPlayer.player.stop (audioStream [index]);

if (continuousAudioDataStream [index] != null)

AudioPlayer.player.stop (continuousAudioDataStream [index]);

} // stop ()

} // PlayMusic class

its very messy but i want it to play 5 random songs in a jframe and its not working atm can some1 PLZ fix it for me

a 100000 thanks

[3875 byte] By [GraDoNa] at [2007-11-27 11:44:32]
# 1

There's a Sound-forum here:

http://forum.java.sun.com/forum.jspa?forumID=541

When you decide to post there, you have the most success in getting a helpful answer when you:

- use code-tags when posting code (http://forum.java.sun.com/help.jspa?sec=formatting);

- don't use SMS spelling (not some1 and PLZ, but someone and please);

- ask specific questions instead of "can someone please fix it for me".

Good luck.

prometheuzza at 2007-7-29 17:56:25 > top of Java-index,Java Essentials,Java Programming...
# 2

you need to change your code and abandon the sun.audio.* package.

you should be using the javax.sound library:

http://java.sun.com/j2se/1.4.2/docs/api/javax/sound/sampled/package-summary.html

TuringPesta at 2007-7-29 17:56:25 > top of Java-index,Java Essentials,Java Programming...
# 3

thanks ill do that

GraDoNa at 2007-7-29 17:56:25 > top of Java-index,Java Essentials,Java Programming...