Audio Player using MediaPlayer class in JMF - java

JMF provides a class MediaPlayer which can be used as a full-featured player. However, I can't find a way to play an audio file. Some snippets of the code used.
import javax.media.bean.playerbean.MediaPlayer;
// ....
MediaPlayer mp = new MediaPlayer();
mp.setMediaLocation(location of file); // <- syntax error!
mp.start();
But it doesn't work. Eclipse shows this error:
Syntax error on token "setMediaLocation", Identifier expected after this token
..on setMediaLocation() method.
Can someone tell me how to use MediaPlayer class to play audio file? and what is wrong in my coding?

You do not literally have ..setMediaLocation(location of file); in the source do you? I thought that was paraphrased.
In that case, the answer is to supply an argument to the setMediaLocation() method. The docs. specify a String or MediaLocator instance.
I avoid using String instances to represent file or URL based sources. If a method needs a File or URL - give it a File or URL. That leaves the best option as a MediaLocator(URL). Here is how it might go. First we need a URL - there are a number of different ways of getting one, depending on what the source of the URL is. E.G. the internet, a file on the local file-system, or an embedded resource in a Jar delivered with the app. The last two might be something like:
File based URL
File mediaFile = new File("user.mp3");
URL mediaURL = mediaFile.toURI().toURL();
// ...
Embedded resource URL
URL mediaURL = this.getClass().getResource("/path/to/our.mp3");
// ...
Once there is a mediaURL instance, we can create a locator and use it in the method.
Using the URL
MediaLocator mediaLocator = new MediaLocator(mediaURL);
MediaPlayer mp = new MediaPlayer();
mp.setMediaLocation(mediaLocator);
// ...
General tips
JMF is an abandoned API.
Since you seem interested only in audio, look to use Java Sound. It can play audio samples just fine, is part of core Java, and with a small part of the JMF, can play MP3 format (see link for details).
You seem new to the abandoned API being used, Eclipse and debugging. Perhaps you should focus on simpler things for the moment. Media handling is an advanced topic.

Related

How could I implement audio to my program in Replit (JavaFX) only via URL links?

I want to add audio to my JavaFX project in Replit. Is there a way to use a public accessible URL link that references an .mp3 file in order to play audio?
For example, the following allows me to include an image only using a URL without having to reference a file:
Image image= new Image("https://cdn.discordapp.com/attachments/952426047645843537/954119846046629928/play3.png");
Is something similar possible for audio files?
You can try this
Media sound = new Media("https://yourURL/file.mp3");
MediaPlayer mediaPlayer = new MediaPlayer(sound);
mediaPlayer.play();

how can get album art of song from url

want get album art of song from url and this is my try so far :
SongPath = "www.asd.com/music.mp3";
android.media.MediaMetadataRetriever mmr = new MediaMetadataRetriever();
try{
mmr.setDataSource(SongPath);
}catch(Exception e){}
byte [] data = mmr.getEmbeddedPicture();
if(data != null)
{
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bitmap);
}
else
{
imageView.setImageResource(R.drawable.jak);
}
but when run this code get this : call to get embedded picture failed
so i research about this and some people fix that with change this part
mmr.setDataSource(SongPath);
to this
mmr.setDataSource(SongPath,new HashMap<String, String>());
i do that but when run app image view show nothing and get this : SkImageDecoder::Factory returned null
Note :
the only way i could do that use FFmpegMediaMetadataRetriever library (its like MediaMetadataRetriever) and worked but problem is library seems slow .. its mean 4,5sec time need to fetch pic and when add this library the apk file from 1.8mb become to 24mb! and this is so huge!
so any one in the world know how can do that with good way ? if any one can please help
You can use Glide in a similar way you would use it for a File: bumptech/glide#699 if you can figure out how to do it remotely. The problem is that you still need to download the whole .mp3 file, which takes that 4-5 seconds I guess. I'm not exactly sure about the .mp3 format, but I think the album art may be at the end of the file. For this reason this approach is not suggested (similar to how it's a bad idea to load video thumbs from http). If you want to go this way anyway, then slap a .diskCacheStrategy(SOURCE) on the load to save the file first, and then write a custom decoder to use
The best approach is to have the album art served from a separate file if you host the mp3; or use a public service to retrieve it. See any album art downloader program for possible services.
One thing's for sure: without a protocol, nothing will work, prefix your www. with http:// or https:// as necessary.

VLCJ save mediaplayer capture

I'm using VLCJ and I wish to save a video clip I'm playing using mediaPlayer.playMedia(); to my desktop. I know this can be accomplished by setting media options using a :sout string but I don't know which options to set and I'm having trouble understanding the example option strings on the web. Could someone help by explaining the following :sout option string?
String[] options = {":sout=#transcode{vcodec=mp4v,vb=4096,scale=1,acodec=mpga,ab=128,channels=2,samplerate=44100}:duplicate{dst=file{dst=" + fileName + "},dst=display}", ":input-slave=alsa://hw:0,0"};
I just need an options array that creates an mp4 video using the fileName destination. These options fail/error out for me.
Also, VLCJ seems to be dropping a ton of frames with this error
avcodec decoder error: more than 5 seconds of late video -> dropping frame
by using these options
String[] options = {":sout=#transcode{vcodec=mp1v,vb=2048,scale=1,acodec=mpga,ab=128,channels=2,samplerate=44100}:duplicate{dst=file{mux=mpeg1,dst=" + fileName + "},dst=display}", ":input-slave=dshow://hw:0,0" };
To save a raw stream, use media options similar to the following when you play the media:
String mrl = "your-streaming-mrl";
String[] opts = {"sout=#duplicate{dst=std{access=file,mux=raw,dst=output-file.ext}}"};
Clearly you replace "your-streaming-mrl" and "output-file.ext" with whatever is appropriate.
And then:
mediaPlayer.playMedia(mrl, opts);
You will need to wait (listen) for a media player "finished" event before your saved file is ready.
You may also need to explicitly invoke release() on the media player before your saved file is ready.

Where do i put file(.mp3) in order for it to be read?

I am usinf the JavaFx method of playing music files but it isn't working (sound not playing). I feel the problem lies in my files location. Where do I put the .mp3 in my java projects folder for it to be referenced with a simple string as so? Or is there another way to reference it? JavaFX Media takes a String parameter.
String test = "test.mp3";
Media x = new Media(test);
MediaPlayer mediaPlayer = new MediaPlayer(x);
mediaPlayer.play();
If you have read the JavaDoc of Media, then you know that you have to give a http, file or jar URI to the constructor you are using.
Actually from looking at the source code I wonder that you are not getting an IllegalArgumentException.
However, use Class.getResource(...).toURI().toString() to get the String you want to give to the Media constructor.
The combination of which Class object you call this on and what you give to the getResource() method depends on how you layout your files.
If you have your file besides your class, getClass().getResource("file.name") should work.
If you have your file in the root of your classpath, getClass().getResource("/file.name") should do.
You can give any other valid http, file or jar URI to the contructor too of course.

In Java how can I play an ogg file provided by an https web service such as cerevoice?

I am trying to program a classroom assistant (I work as a teacher) using Java who will give spoken instructions to students/ask them questions etc. I have managed to connect successfully to the cerevoice cloud to create an ogg file
e.g. "https://cerevoice.s3.amazonaws.com/Heather220501c8c2e94d4650f64f7d951bf76b08b0eb.ogg"
however when I try to play this ogg file from java I get an error that it could not be found or that it is an unsupported audio resource depending on the ogg player I use (i have tried EasyOGG and TinySound so far) - both the ogg players work successfully locally but I cannot get them to play a file directly from the website.
Examples how I have tried to reference it:
URL url = new URL("https://cerevoice.s3.amazonaws.com/Heather220501c8c2e94d4650f64f7d951bf76b08b0eb.ogg");
Music song = TinySound.loadMusic(url);
song.play(true);
OggClip ogg = new OggClip("https://cerevoice.s3.amazonaws.com/Heather220501c8c2e94d4650f64f7d951bf76b08b0eb.ogg");
ogg.loop();
Apologies for my ignorance I'd really appreciate any help anyone can give with playing this file! =)
Many thanks,
Darren
Edited to show using a literal String value to help clarity
Edit very ugly hack 1 solution: //opens a browser window plays file then closes window
String url = "https://cerevoice.s3.amazonaws.com/Heather220501c8c2e94d4650f64f7d951bf76b08b0eb.ogg"; //hard coded here for simplicity but URL dynamically retrieved from webservice
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
Thread.sleep(3000);
Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
Edit slightly less ugly hack 2 solution:
//creation of a temporary file to allow playing from java without creating a browser window
try{
Files.deleteIfExists(Paths.get("C:/cere/temp.ogg"));
try (InputStream in = URI.create(url.value).toURL().openStream()) {
Files.copy(in, Paths.get("C:/cere/temp.ogg"));
}catch(IOException e) {
}

Categories