I am working on an audio player and need to add pause()
and play()
features in it to connect with JButtons
. The problem is I am not able to import Media package as it says package does not exist. I cannot find anywhere it online to download the package. Same goes for AudioPlayer
class which gives bad class file error.
Asked
Active
Viewed 1.7k times
2
-
Can you post the full name of the package you're trying to use? Is this part of an external library / jar file? Are you using any build tools like ant, maven, or gradle? – JNYRanger Jun 07 '15 at 20:21
-
Next time use findJar: http://findjar.com/index.x?query=javax.media It is a very useful site for this kind of search. – Costis Aivalis Jun 10 '15 at 11:55
4 Answers
1
you need the JMF libraries , you can get them from there , for windows there is a typic installer :

AntJavaDev
- 1,204
- 1
- 18
- 24
-
i have it downloaded but still i am not able to use AudioPlayer class in my code. – Masu Jun 10 '15 at 11:38
-
which IDE are you using ? have you import the external jar in the project's build path?? – AntJavaDev Jun 10 '15 at 11:48
-
my teacher restricts me to use cmd for compiling while the code is written in notepad++. i didnt get second part of your question – Masu Jun 10 '15 at 11:51
-
-
well i dont remember for sure the command that youll have to use , but you ll have to define the jmf.jar path in order to be able to compile / run the app. There are some examples to compile / run a java project with external jars , check here http://stackoverflow.com/questions/6069702/java-command-line-with-external-jar – AntJavaDev Jun 10 '15 at 12:27
1
It is a late answer, but you can use the Maven dependency:
<!-- https://mvnrepository.com/artifact/javax.media/jmf -->
<dependency>
<groupId>javax.media</groupId>
<artifactId>jmf</artifactId>
<version>2.1.1e</version>
</dependency>

aristotll
- 8,694
- 6
- 33
- 53
0
Based on your question,
You can down load java.media
then use
import javax.media.*;
then you can declare like
Player audioplayer = Manager.createRealizedPlayer(file.toURI().toURL());
And
audioplayer.start();
and audioplayer.stop();
Here file
means where the source file saved.
NB: you can use JMF jar file
Try like this
try {
audioplayer = Manager.createRealizedPlayer(file.toURI().toURL());
} catch (IOException ex) {
Logger.getLogger(MY_MP3_PLAYER.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoPlayerException ex) {
Logger.getLogger(MY_MP3_PLAYER.class.getName()).log(Level.SEVERE, null, ex);
} catch (CannotRealizeException ex) {
Logger.getLogger(MY_MP3_PLAYER.class.getName()).log(Level.SEVERE, null, ex);
}
OR Try the sample code given below
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
public class Mp3Player {
public static void main(String[] args) throws IOException, NoPlayerException, CannotRealizeException {
// Source of song file
File f=new File("your path in which mp3 file is saved");
// Create a Player object that realizes the audio
final Player p=Manager.createRealizedPlayer(f.toURI().toURL());
// Start the music
p.start();
// Create a Scanner object for taking input from cmd
Scanner s=new Scanner(System.in);
// Read a line and store it in st
String st=s.nextLine();
// If user types 's', stop the audio
if(st.equals("s"))
{
p.stop();
}
}
}

Anptk
- 1,125
- 2
- 17
- 28
-
anywhere i can get AudioPlayer class? it provides predefined play() pause() methods. – Masu Jun 10 '15 at 10:00
-
go to the link in answer download JMF api. The class AudioPlayer has play() and stop () method. just use like in answer – Anptk Jun 10 '15 at 10:14
-
when i write that Manager.createRealizedPlayer() line.. it gives me error that javax.media.Player cannot be converted to Player – Masu Jun 10 '15 at 11:37
-
its working code, if its not working for you, check your jar file and path – Anptk Jun 10 '15 at 12:00
-
It compiles fine. It gives runtime error Exception in thread "main" javax.media.NoPlayerException and if i place a try-catch for the code.. it gives runtime error java.io.IOException: file not found – Masu Jun 10 '15 at 12:08
-
File f=new File("your path");edit with path where you saved your mp3 file – Anptk Jun 10 '15 at 12:12
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80172/discussion-between-anptk-and-masu). – Anptk Jun 10 '15 at 12:16
-1
Following four packages will solve your problem. They contains most of helpful methods to deal with audio player.
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.advanced.AdvancedPlayer;
You can use .stop(), start(), .play() etc. from above packages. Hope that will help.

VD007
- 267
- 2
- 15
-
thank you :) if you could tell me what is "jl" because i'll be having viva for this. would this let me make objects of Player, AudioPlayer? I also want a pause feature. would this allow me? – Masu Jun 07 '15 at 10:26
-
-
Weel, it is not included in the default SDK, download it from [JavaZoom](http://www.javazoom.net). – Mordechai Jun 07 '15 at 10:33
-
They are packages created to manage audio/video behavior on java. while, decoder, player etc. are folders inside the package. further down in the hierarchy, audiodevice, factoryregistry, advancedplayer, javalayerexception etc. are subfolders in those folders. Get the understanding of hierarchy in java packages. – VD007 Jun 07 '15 at 10:35
-
and for your error solution, try to import whole package instead of those particular folders. use import javazoom.jl.*; and remove all four imports from the code – VD007 Jun 07 '15 at 10:35
-
and yes, as suggested by MouseEvent, if import doesn't work then download the package from JavaZoom (link: http://www.javazoom.net/projects.html) – VD007 Jun 07 '15 at 10:39