I can not start the player in my code - java

I am trying to launch my my code and start the player. But I can not do that.
import javax.media.*;
import java.io.*;
public class MP3Player {
public static void main(String[] args) throws Exception {
File file = new File("c://player/trigger.mpg");
MediaLocator mrl = new MediaLocator(file.toURL());
Player player = Manager.createPlayer(mrl);
player.start();
}
}
[Edit by Philipp]
According to a comment by the original author, Netbeans prints the following error message:
Unable to handle format: MPEG, 160x120, FrameRate=30.0, Length=28800 Failed to realize:
com.sun.media.PlaybackEngine#131f71a Error: Unable to realize
com.sun.media.PlaybackEngine#131f71a BUILD SUCCESSFUL (total time: 1 second)
[/Edit by Philipp]

I don't know JMF player at all, but I assume the problem is that the code exits immediately after issuing the command, terminating any other threads...
I'd try inserting a Thread.sleep(1000); after player.start(); :
public class MP3Player {
public static void main(String[] args) throws Exception
{
File file = new File("c:/player/trigger.mpg");
MediaLocator mrl = new MediaLocator(file.toURL());
Player player = Manager.createPlayer(mrl);
player.start();
Thread.sleep(1000);
}
}
If now the first second of the MP3 is heard, this was the issue.
EDIT Also, someone pointed out problems with the slashes, the path should be correct too, but the slash is not missing, but there is rather one too much of it...
EDIT2 Ok, I misread mpg for mp3, and as the poster posted the error he got: the format of the video is not supported by JMF, you need a codec.
This might be of help: Tek-tips: Play MPEG-4 movie with JMF?

Unable to handle format: MPEG, 160x120, FrameRate=30.0
It is unable to play a video stream it founds. From the description and the name of your code, the file is expected to contain only audio streams of the compression format MP3 (MPEG-1 Audio Layer III). An .mpg extension may contains lot of different mpeg formats

Related

Issue using Sikuli Pattern on Mac OS

I am trying to perform a small calculation as part of my selenium learning with Java, Sikuli using Eclipse IDE.
My Code is as below:
package webelements.concepts;
import org.sikuli.script.FindFailed;
import org.sikuli.script.ImagePath;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
public class DeskTopIconEx {
public static void main(String[] args) throws FindFailed, InterruptedException {
// to Perform small calculation on Calculator app which is present on screen.
ImagePath.setBundlePath("/Users/murthyinguva/Desktop/Images");
Thread.sleep(4000);
Screen screenObj = new Screen();
Pattern btnCObj = new Pattern("//Users//murthyinguva//Desktop//Images//btnC");
Pattern btn9Obj = new Pattern("//Users//murthyinguva//Desktop//Images//btn9");
Pattern btnXObj = new Pattern("//Users//murthyinguva//Desktop//Images//btnX");
Pattern btn5Obj = new Pattern("//Users//murthyinguva//Desktop//Images//btn5");
Pattern btnEqualsObj = new Pattern("//Users//murthyinguva//Desktop//Images//btnEquals");
String paths = ImagePath.getBundlePath();
System.out.println("Image path given as :" + paths);
screenObj.click(btnCObj);
screenObj.click(btn9Obj);
screenObj.click(btnXObj);
screenObj.click(btn5Obj);
screenObj.click(btnEqualsObj);
}
}
Console output is:
`Image path given as :/Users/murthyinguva/Desktop/Images
Exception in thread "main" FindFailed: btnC.png: (90x88) in R[0,0 1440x900]#S(0)
Line 2226, in file Region.java
at org.sikuli.script.Region.wait(Region.java:2226)
at org.sikuli.script.Region.wait(Region.java:2244)
at org.sikuli.script.Region.getLocationFromTarget(Region.java:3298)
at org.sikuli.script.Region.click(Region.java:3916)
at org.sikuli.script.Region.click(Region.java:3892)
at webelements.concepts.DeskTopIconEx.main(DeskTopIconEx.java:25)
`
Help required:
I would like to know your advises why i am getting this error and unable to see that Sikuli is performing any mouse actions. I have given the permissions as per:
https://github.com/RaiMan/SikuliX1/wiki/Allow-SikuliX-actions-on-macOS
Because of this difficulty I am unable to progress much, Your help is highly appreciated. Thanks in advance.
Desktop screenshot
The forward slashes in the filenames have to be single.
But since you have used
setBundlePath()
before, the Patterns can be reduced to
String btnCObj = "btnC";
All the best. RaiMan from SikuliX.

JavaFX audio doesn't seem to be playing

I am fairly new to JavaFX and recently wanted to play audio with an MP3 file rather than WAV. From what I can tell, I am doing things correctly and I don't get any errors, but I also don't hear any sound.
I will post the parts of my code that matter below. If I'm missing something please let me know. Thanks.
try {
URL sound = getClass().getResource("/resources/origin.mp3");
Media hit = new Media(sound.toExternalForm());
musicPlayer = new MediaPlayer(hit);
musicPlayer.setVolume(1.0);
}
catch(Exception e) {
System.out.println("whoops: " + e);
}
checkMusic();
Check Music Method:
public void checkMusic() {
if(music)
musicPlayer.setAutoPlay(true);
else
musicPlayer.stop();
}
I also tried just musicPlayer.play(); as well.
EDIT
And yes, I am sure the code within the if statement runs, I have checked it with println, and they print out. The music boolean is just a controller for settings in the program/game.
instead of
Media hit = new Media(sound.toExternalForm());
try this:
final Media media = new Media(sound.toString());

JavaFX MediaPlayer highly inaccurate seeking

I'm using 320 kbps roughly 1 hour long MP3 files. The project I'm working on would seek in a collection of music inside an MP3 file so that it can shuffle the songs. I would give the timestamps to the program and it would seek to the song. It would work if JavaFX's seek method wasn't highly inaccurate.
After using MediaPlayer.seek(duration) The MediaPlayer.getCurrentTime() returns the duration we seeked to as expected. However if we listen to the mp3 file(either without seeking or in an external mp3 player) we realize that the time reported and reality is very different, sometimes even seconds.
For example MediaPlayer.seek(Duration.millis(2000)) results seeking to 0 seconds. A 2 second failure rate is not acceptable.
With WAV it seems to work. Though it does not with MP3.
The two workarounds I think so far are possible:
Writing an MP3 Decoder and Player which doesn't have the bug
Using uncompressed WAV files
Does anyone know anything better?
If anyone needs the source code there isn't much more in it:
public static void main(String[] args) {
MediaPlayer player = null;
JFXPanel fxPanel = new JFXPanel(); //To initialize JavaFX
try {
String url = new File("E:\\Music\\test.mp3").toURI().toURL().toExternalForm();
player = new MediaPlayer(new Media(url));
System.out.println("File loaded!");
} catch (MalformedURLException e) {
//e.printStackTrace();
System.out.println("Error with filename!");
System.exit(0);
}
player.play();
System.out.println("Playing!");
while (true)
{
Scanner reader = new Scanner(System.in);
String string = reader.nextLine();
if (string.equals("Exit")) System.exit(0);
else if (string.equals("Seek"))
{
player.seek(Duration.millis(2000)); //this seeks to the beggining of the file
player.pause();
try {
Thread.sleep(100); //player.getCurrentTime() doesn't update immediately
} catch (InterruptedException e) { }
System.out.println("Time: " + player.getCurrentTime().toMillis() + " / " + player.getTotalDuration().toMillis());
player.play();
}
}
}
I would recommend using the javazoom library. It is an open source java library that already has this stuff written without errors(At least none that I found).
Source
http://www.javazoom.net/index.shtml
Place your call to the seek method off the UI thread or your UI will hang.
new Thread(() ->player.seek(Duration.millis(2000))).start();

jaudiotagger java - MP3AudioHeader can not be resolved

I am trying to use jaudiotagger for retriving id3 tags from mp3 file for my recent side project but I have run into this problem..I can't run the program because I get this error saying "MP3AudioHeader can not be resolved"..but I have imported everything that is needed as you can see...any suggestions would be helpful and here is the code I copied from the website
import org.jaudiotagger.*;
public class mainClass
{
public static void main(String[] args)
{
// the file we are going to read
File oSourceFile = new File("/Users/tushar_chutani/Music/iTunes/iTunes Media/Music/Fun_/We Are Young (feat. Janelle Monáe) - Single/01 We Are Young (feat. Janelle Monáe).mp3");
MP3File f = (Mp3File)AudioFileIO.read(oSourceFile);
MP3AudioHeader audioHeader = f.getAudioHeader();
audioHeader.getTrackLength();
audioHeader.getSampleRateAsNumber();
mp3AudioHeader.getChannels();
mp3AudioHeader.isVariableBitRate();
}
Your code is correct ,the mistake is that ,its not (Mp3File) its (MP3File) 'p' should be in capital letter. so correct is:-
MP3File f = (MP3File)AudioFileIO.read(oSourceFile);

Start and stop sounds in Java

Similar to this question, I would like to play a WAV file in a Java application - however, I would also like the ability to pause, resume, and restart the sound. I'm guessing I can restart by pausing and then just creating a new sound, but how would I pause and resume in the first place?
Note that my sound is ~15minutes and 152.8mb. If there is a way to do this with an MP3 file (same length, 20.8mb) that would be even better.
For playing WAV files, see the answers to this question:
Problem with Javas Audio Clips on frequent playback of beep sounds
For playing MP3s, you can use JLayer which is a fairly small jar (100k I think, maybe smaller) that you can bundle with your application.
Here's a fairly decent example of how to use it:
MP3.java (from How to play an MP3 file in Java)
/*************************************************************************
* Compilation: javac -classpath .:jl1.0.jar MP3.java (OS X)
* javac -classpath .;jl1.0.jar MP3.java (Windows)
* Execution: java -classpath .:jl1.0.jar MP3 filename.mp3 (OS X / Linux)
* java -classpath .;jl1.0.jar MP3 filename.mp3 (Windows)
*
* Plays an MP3 file using the JLayer MP3 library.
*
* Reference: http://www.javazoom.net/javalayer/sources.html
*
*
* To execute, get the file jl1.0.jar from the website above or from
*
* http://www.cs.princeton.edu/introcs/24inout/jl1.0.jar
*
* and put it in your working directory with this file MP3.java.
*
*************************************************************************/
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
public class MP3 {
private String filename;
private Player player;
// constructor that takes the name of an MP3 file
public MP3(String filename) {
this.filename = filename;
}
public void close() { if (player != null) player.close(); }
// play the MP3 file to the sound card
public void play() {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
player = new Player(bis);
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
// run in new thread to play in background
new Thread() {
public void run() {
try { player.play(); }
catch (Exception e) { System.out.println(e); }
}
}.start();
}
// test client
public static void main(String[] args) {
String filename = args[0];
MP3 mp3 = new MP3(filename);
mp3.play();
// do whatever computation you like, while music plays
int N = 4000;
double sum = 0.0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sum += Math.sin(i + j);
}
}
System.out.println(sum);
// when the computation is done, stop playing it
mp3.close();
// play from the beginning
mp3 = new MP3(filename);
mp3.play();
}
}
Similar to this question, I would like to play a WAV file in a Java application - however, I would also like the ability to pause, resume, and restart the sound. I'm guessing I can restart by pausing and then just creating a new sound, but how would I pause and resume in the first place?
The javax.sound.sampled.Clip would be ideal for this, except for the fact that most implementations of Clip will not load more than 2 seconds of stereo, 16 bit, 44.1KHz sound! For that reason I developed BigClip. BigClip can handle sounds that are as big as the available memory.
Note that my sound is ~15minutes and 152.8mb. If there is a way to do this with an MP3 file (same length, 20.8mb) that would be even better.
Sure thing. As mentioned in the JavaSound tag info page..
MP3 decoding support
The Java Sound API does not support many formats of sampled sound internally. In a 1.6.0_24 Oracle JRE getAudioFileTypes() will generally return {WAVE, AU, AIFF}. An MP3 decoder at least, is close by. The mp3plugin.jar of the Java Media Framework supports decoding MP3s.
I currently use BigClip & the mp3plugin.jar Jar in the DukeBox player. Given 1024Meg of memory, it can easily load both the 17:12 of the 1812 Overture, & 15:38 of Bolero (the two longest tracks in my favorites play list). I mention 'both' since it will load the next track while playing the current one.
As an aside, beware of looking at code that mentions the sun.audio packages (mentioned in both linked threads). This package and/or it's classes might be moved or removed in the next release (at Oracle's discretion) & have not been necessary since Java 1.3.

Categories