1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import javax.activation.DataSource;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import static java.lang.System.*;
import java.net.URL;
public class Teste {
public static void main(String[] args) {
// TODO Raccord de méthode auto-généré
Fenetre fen = new Fenetre();
class Play implements Runnable
{
// ------------------------------ FIELDS ------------------------------
/**
* true if want debugging output
*/
private static final boolean DEBUGGING = false;
/**
* information about the format of the sound data.
*/
private final AudioFormat af;
/**
* stream current background thread is playing.
*/
private final AudioInputStream ais;
/**
* information about the output sound rendering device
*/
private final DataLine.Info info;
/**
* size of buffer used to copy sound bytes across
*/
private final int buffSize;
// -------------------------- PUBLIC STATIC METHODS --------------------------
/**
* play a sound file supported by javax.sound.sampled e.g. a signed PCM 8-bit au resource.
*
* @param url usually a resource to play created with Class.getResource.
*
* @throws UnsupportedAudioFileException if you select a sound file type not supported on this platform.
* @throws IOException if problem retrieving the URL
*/
public void play( URL url ) throws UnsupportedAudioFileException, IOException
{
url = new URL ("http://pagesperso-orange.fr/midinet/films/660theme.mid");
play( AudioSystem.getAudioInputStream( url ) );
}
/**
* play a sound file supported by javax.sound.sampled e.g. a signed PCM 8-bit au resource.
*
* @param file a sound file to play
*
* @throws UnsupportedAudioFileException if you select a sound file type not supported on this platform.
* @throws IOException if problem retrieving the file.
*/
public void play( File file ) throws UnsupportedAudioFileException, IOException
{
file = new File ( "/home/pasian/Musique/music.mid");
play( AudioSystem.getAudioInputStream( file ) );
}
// -------------------------- PUBLIC INSTANCE METHODS --------------------------
/**
* background thread to feed bytes from stream to sound renderer
*/
public void run()
{
synchronized ( getClass() )
{
try
{
final SourceDataLine line = ( SourceDataLine ) AudioSystem.getLine( info );
line.open( af, buffSize );
// start streaming in from the resource
line.start();
final byte[] data = new byte[buffSize];
int bytesRead;
/* copy from stream to sound renderer */
while ( ( bytesRead = ais.read( data, 0, data.length ) ) != -1 )
{
line.write( data, 0, bytesRead );
}
/* sound data stream is finished, wait for sound to finish */
line.drain();
line.stop();
line.close();
}
catch ( LineUnavailableException e )
{
// throwing an exception in a background thread probably won't be caught properly.
err.println( "Line unavailable to play a sound" );
}
catch ( IOException e )
{
err.println( "Problems fetching data to play a sound" );
}
}
}
// -------------------------- STATIC METHODS --------------------------
/**
* play a sound file supported by javax.sound.sampled e.g. a signed PCM 8-bit au resource.
*
* @param ais AudioIunputStream to play
*/
private synchronized void play( final AudioInputStream ais )
{
// check out stream ahead of time so Exception will be fielded.
final AudioFormat af = ais.getFormat();
if ( DEBUGGING )
{
System.out.println( "audio format: " + af.toString() );
}
final DataLine.Info info = new DataLine.Info( SourceDataLine.class, af );
if ( !AudioSystem.isLineSupported( info ) )
{
throw new IllegalArgumentException( "Cannot play an unsupported audio format" );
}
final int frameRate = ( int ) af.getFrameRate();
final int frameSize = af.getFrameSize();
final int buffSize = frameRate * frameSize / 10;
if ( DEBUGGING )
{
out.println( "Frame Rate: " + frameRate );
out.println( "Frame Size: " + frameSize );
out.println( "Buffer Size: " + buffSize );
}
// We can't start another thread until previous one has terminated
// because this method and run are both synchronised on the class object.
// Start up a background thread to play the sound.
new Thread( new Play( ais, af, info, buffSize ) ).start();
}
// --------------------------- CONSTRUCTORS ---------------------------
/**
* constructor to create a thread run object to play a sound in the background.
*
* @param ais audion input stream to play
* @param af audio format of the stream
* @param info info about the line that renders the sound
* @param buffSize size of buffer when copying bytes to rendering device.
*/
private Play( AudioInputStream ais, AudioFormat af, DataLine.Info info, int buffSize )
{
this.ais = ais;
this.af = af;
this.info = info;
this.buffSize = buffSize;
}
}
}
} |
Partager