Which can play sound files java code? Thank you

Category: Java SE
 
wangyingjie888
2012-06-25 10:13:41

Sponsored Links
Which can play sound files java code? Thank you very much

Sponsored Links

zjx880126
2012-06-25 10:22:44
/ / applet program
import java.applet. *;
; import java.awt. *;
class A extends Applet
{
public void ; init ()
{
AudioClip = ac;
; setSize (500,460);
ac = getAudioClip (getCodeBase (), "Music / a.mid");
}
public void start ()
{ ;
ac.loop ();
}
}
/ / In this code place to build a folder Music, in which you put the name of the sound file note
avalonprince
2012-06-25 10:32:13
learn.

this is. mid format
If requirements to play. mp3 format it?
c303914104
2012-06-25 10:50:16
JMF go online to find information. There are a lot of.
I also found a MP3 player code yet.
panamaviation
2012-06-25 11:04:31
public Demo (String file_name) {
try {
AudioPlayer.player.start (new FileInputStream (file_name));
} catch (Exception e) {
}
}
fqy_dc
2012-06-25 11:17:22
import javax.media.bean.playerbean. *; / / this package to use JMF
import java.awt. *;
import java.awt.event. *;
import java.applet. *;
public class mp31 extends Applet ; implements ActionListener
{

Button b1, b2;
MediaPlayer player;
public void init ()
{
player = new MediaPlayer ();

setLayout (new FlowLayout ());
player.setMediaLocation ("file :/ F: \ \ Music Wind \ \ East Wind Breaks. mp3" ;) ;/ / audio file path
b1 = new Button ("Play");
b2 = new Button ("Stop");
add (b1) ;
add (b2);

b1.addActionListener (this);
b2.addActionListener (this);
setSize (200,200);
setVisible (true);
}
public void actionPerformed (ActionEvent e)
{
if (e.getSource () == b1)
{
player.start ();

}
else if (e.getSource () == b2)
{
player.stop ();

}
}
}
sgwxj
2012-06-25 11:29:03
Thank purerabbit, I tried the above code, we can really!
firepsd
2012-06-25 11:47:54
import java.io.File;
import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

public class MediaPlayer{
public static void main(String[] args)throws Exception {
Sequencer midi2 = MidiSystem.getSequencer(false);
String filename="D:/My Documents/My Music/778616.mid";
midi2.open();
        Sequence s1 = MidiSystem.getSequence(new File(filename));
        midi2.setSequence(s1);
        midi2.start();
        Thread.sleep(midi2.getMicrosecondLength()/1000);
        System.exit(1);
}
}
mabowen20100214
2012-06-25 12:00:33
/ *
* Created on Jun 11, 2008
*
* Copyright by cuckoo
* /
package prepare;

import static org. jouvieje.FmodEx.Defines.FMOD_TIMEUNIT.FMOD_TIMEUNIT_MS;
import static org.jouvieje.FmodEx.Enumerations.FMOD_RESULT.FMOD_ERR_CHANNEL_STOLEN;
import static org.jouvieje.FmodEx.Enumerations.FMOD_RESULT.FMOD_ERR_INVALID_HANDLE;
import static org.jouvieje.FmodEx.Enumerations.FMOD_RESULT.FMOD_OK;
import static org.jouvieje.FmodEx.Misc.BufferUtils.newFloatBuffer;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.jouvieje.FmodEx.Channel;
import org.jouvieje.FmodEx.FmodEx;
import org.jouvieje.FmodEx.Init;
import org.jouvieje.FmodEx. Sound;
import org.jouvieje.FmodEx.System;
import org.jouvieje.FmodEx.Defines.FMOD_INITFLAGS;
import org.jouvieje.FmodEx.Defines.FMOD_MODE;
import org.jouvieje.FmodEx.Defines.INIT_MODES;
import org.jouvieje.FmodEx.Enumerations.FMOD_CHANNELINDEX;
import org.jouvieje.FmodEx.Enumerations.FMOD_DSP_FFT_WINDOW;
import org.jouvieje.FmodEx.Enumerations.FMOD_RESULT;
import org.jouvieje.FmodEx.Exceptions.InitException;
import org.jouvieje.FmodEx.Misc.BufferUtils;
import org.jouvieje.FmodEx.Structures.FMOD_CREATESOUNDEXINFO;

/ **
*
* @ author cuckoo
*
* /

public class CreateAudioSystem {

public static void main (String args []) {
new CreateAudioSystem (). createContents ();
}
public void createContents () {
initSystem ();

_soundFile = new File ("media \ \ In a Glade.mp3");
String soundFilename = _soundFile.getAbsolutePath ();
java.lang.System.out.println (" -> file name: "+ soundFilename);
int mode = FMOD_MODE.FMOD_2D | FMOD_MODE.FMOD_SOFTWARE | FMOD_MODE.FMOD_CREATESTREAM;
/ / mode | = FMOD_MODE.FMOD_HARDWARE;

/ / create a exinfo
FMOD_CREATESOUNDEXINFO exinfo = ; FMOD_CREATESOUNDEXINFO.create ();
/ / set this found `s length
/ / ; exinfo.setLength ((int) _soundFile.length ());
_audioSystem.createStream (soundFilename, mode, null, _sound);
ByteBuffer buffer = BufferUtils.newByteBuffer (1024);
_sound.getLength (buffer.asIntBuffer (), FMOD_TIMEUNIT_MS);
; java.lang.System.out.println ("-> sound length is" + buffer.getInt (0));
; java.lang.System.out.println ("-> start play");
startPlay ();
}
public void startPlay () {
_audioSystem.playSound (FMOD_CHANNELINDEX.FMOD_CHANNEL_FREE, _sound, true, _channel);
java.lang.System.out.println ("-> play");
; _channel.setPaused (false);
do
{
_audioSystem.update ();
/ / int ms = ; 0;
/ / boolean playing = false;

if (_channel! = null &&! _channel.isNull ())
{
Sound currentsound = new Sound ();
/ *
_result = _channel.isPlaying (_buffer);
if (( _result! = FMOD_OK) && (_result! = FMOD_ERR_INVALID_HANDLE) && (_result! = FMOD_ERR_CHANNEL_STOLEN))
java.lang . System.out.println ("-> error");
playing = _buffer.get (0)! = 0;
* /
_result = _channel.getPaused (_buffer);
if ((_result! = FMOD_OK) && (_result! = FMOD_ERR_INVALID_HANDLE) && ; (_result! = FMOD_ERR_CHANNEL_STOLEN))
java.lang.System.out.println ("-> error");
_paused = _buffer.get (0)! = 0;

/ * _result = _channel.getPosition (_buffer.asIntBuffer (), FMOD_TIMEUNIT_MS);
if ((_result ! = FMOD_OK) && (_result! = FMOD_ERR_INVALID_HANDLE) && (_result! = FMOD_ERR_CHANNEL_STOLEN))
java.lang.System . out.println ("-> error");
ms = _buffer.getInt (0); * /

_channel.getCurrentSound (currentsound);
FloatBuffer floatBuffer = newFloatBuffer (512);
_channel.getSpectrum (floatBuffer, floatBuffer.limit (), 0, FMOD_DSP_FFT_WINDOW.FMOD_DSP_FFT_WINDOW_TRIANGLE);
for ( int i = 0; i <floatBuffer.limit (); i + +) {
float value = floatBuffer.get (i );
if (value! = 0f) {
java.lang.System.out.println ("----------------" ; + value);
} else {
java.lang.System.out.println ("-------- 2 --------");
}
}
if (! currentsound.isNull ())
{
_result = currentsound.getLength (_buffer.asIntBuffer (), FMOD_TIMEUNIT_MS) ;
if ((_result! = FMOD_OK) && (_result! = FMOD_ERR_INVALID_HANDLE) && (_result! = FMOD_ERR_CHANNEL_STOLEN) )
java.lang.System.out.println ("-> error");
_songLength = _buffer.getInt (0);
}
_channel.getPosition (_buffer.asIntBuffer (), FMOD_TIMEUNIT_MS);
_currentLength = _buffer.getInt (0);
if (_currentLength> _previous) {
_previous = _currentLength;
} else {
if (_previous> 4) {
_paused = true;
}
}
}
/ / java.lang.System.out.println ("-> play location" + _currentLength);
/ / java.lang.System.out.println ("-> song length" + _songLength);
try {
/ / sleep 10 ms
Thread.sleep (10);
} catch (InterruptedException e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
} while (true);
}
public void initSystem () {
if (_audioSystem == ; null) {
/ **
* Load libraries
* /
try {
Init.loadLibraries (INIT_MODES.INIT_FMOD_EX );
} catch (InitException e) {
/ / TODO Auto-generated catch block
java.lang.System.out.println ( "-> load not success");
}
java.lang.System.out.println ("-> load libraries is end" ;);
/ *
* Create a system and initial it
* /
System audioSystem = new System ();
_result = FmodEx.System_Create (audioSystem);
IntBuffer intBuffer = BufferUtils.newIntBuffer (1);
audioSystem. getVersion (intBuffer);
java.lang.System.out.println ("version" + intBuffer.get (0));
audioSystem.init (_maxChannels, FMOD_INITFLAGS.FMOD_INIT_NORMAL, null);
_audioSystem = audioSystem;
}
}

private System _audioSystem = null;
private File ; _soundFile = null;
private int _maxChannels = 32;
private int _currentLength = 0;
private int _previous = 4;
private int ; _songLength = 0;
private boolean _paused = false ;
private Sound _sound = new Sound ();
private Channel _channel = new Channel ();
private FMOD_RESULT _result ; = null;
private ByteBuffer _buffer = BufferUtils.newByteBuffer (1024);

}
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.