1
Hello everyone would like to know what’s wrong with my code. The Basicplayer library opens mp3 if you use File but do not open mp3 using Inputstream! That’s right?
public class BasicPlayerTest implements BasicPlayerListener {
private PrintStream out = null;
/**
* Entry point.
*
* @param args
* filename to play.
*/
public static void main(String[] args) {
BasicPlayerTest test = new BasicPlayerTest();
// test.play("src/musica/demo.mp3");
try {
// get mp3 path
String mp3 = "musica/demo.mp3";
InputStream in = BasicPlayerTest.class.getClassLoader()
.getResourceAsStream(mp3);
test.play(in);
} catch (Exception e) {
e.printStackTrace();
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(null, "ERROR!" + e,
"Simple Player Java", JOptionPane.ERROR_MESSAGE);
}
}
/**
* Contructor.
*/
public BasicPlayerTest() {
out = System.out;
}
// public void play(String filename){
public void play(InputStream filename) {
// Instantiate BasicPlayer.
BasicPlayer player = new BasicPlayer();
// BasicPlayer is a BasicController.
BasicController control = (BasicController) player;
// Register BasicPlayerTest to BasicPlayerListener events.
// It means that this object will be notified on BasicPlayer
// events such as : opened(...), progress(...), stateUpdated(...)
player.addBasicPlayerListener(this);
try {
// Open file, or URL or Stream (shoutcast, icecast) to play.
// control.open(new File(filename));
control.open(filename);
// control.open(new URL("http://yourshoutcastserver.com:8000"));
// Start playback in a thread.
control.play();
// If you want to pause/resume/pause the played file then
// write a Swing player and just call control.pause(),
// control.resume() or control.stop().
// Use control.seek(bytesToSkip) to seek file
// (i.e. fast forward and rewind). seek feature will
// work only if underlying JavaSound SPI implements
// skip(...). True for MP3SPI and SUN SPI's
// (WAVE, AU, AIFF).
// Set Volume (0 to 1.0).
control.setGain(0.85);
// Set Pan (-1.0 to 1.0).
control.setPan(0.0);
} catch (BasicPlayerException e) {
e.printStackTrace();
}
}
/**
* Open callback, stream is ready to play.
*
* properties map includes audio format dependant features such as bitrate,
* duration, frequency, channels, number of frames, vbr flag, ...
*
* @param stream
* could be File, URL or InputStream
* @param properties
* audio stream properties.
*/
public void opened(Object stream, Map properties) {
// Pay attention to properties. It's useful to get duration,
// bitrate, channels, even tag such as ID3v2.
display("opened : " + properties.toString());
}
/**
* Progress callback while playing.
*
* This method is called severals time per seconds while playing. properties
* map includes audio format features such as instant bitrate, microseconds
* position, current frame number, ...
*
* @param bytesread
* from encoded stream.
* @param microseconds
* elapsed (<b>reseted after a seek !</b>).
* @param pcmdata
* PCM samples.
* @param properties
* audio stream parameters.
*/
public void progress(int bytesread, long microseconds, byte[] pcmdata,
Map properties) {
// Pay attention to properties. It depends on underlying JavaSound SPI
// MP3SPI provides mp3.equalizer.
// display("progress : " + properties.toString());
}
/**
* Notification callback for basicplayer events such as opened, eom ...
*
* @param event
*/
public void stateUpdated(BasicPlayerEvent event) {
// Notification of BasicPlayer states (opened, playing, end of media,
// ...)
display("stateUpdated : " + event.toString());
}
/**
* A handle to the BasicPlayer, plugins may control the player through the
* controller (play, stop, ...)
*
* @param controller
* : a handle to the player
*/
public void setController(BasicController controller) {
display("setController : " + controller);
}
public void display(String msg) {
if (out != null)
out.println(msg);
}
}
Yes it works with Inputstream, an error I noticed is that a bar was missing
/
in theString mp3
should beString mp3 = "/musica/demo.mp3";
– ederwander
ederwander first thank you for your attention, for responding. It is true that the bar was missing yes in this code I posted, but I wanted the problem to be just this one. With the bar typed there also does not work! Error: May 03, 2016 12:48:08 pm javazoom.jlgui.basicplayer.Basicplayer open INFO: open(null) I’m using mp3 inside my application so I need to use Inputstream and not File when indicating the path understand? Look at her documentation there http://www.javazoom.net/jlgui/developerguide.html
– Marcos Marcos
Very strange does not work with Inputstream this API. Have you used this library before? With FILE works perfectly... Try using that with Inputstream...
– Marcos Marcos
I think it will be of great help if someone here can make a small example just to show working with Inputstream. Jlayer (jl1.0.1.jar) works perfectly with both File and Inputstream I’ve tried here. Now Basicplayer (basicplayer3.0.jar) has millions of examples on the internet just with File nothing with Inputstream!!! Anyone can help here?
– Marcos Marcos
Get! player.open(getClass(). getResource("music/vocals.mp3"));
– Marcos Marcos