JavaFX's Media Player not retrieving metadata - java

I'm creating a video player using JavaFX media player library. I need to retrieve the metadata and display in a window. I'm having problems retrieving the data though.
I've read 2 threads about this already and I understand that this is done asynchronously so I need a listener in order to get the metadata.
First Thread:Retrieving metadata from media files in JavaFX
Second Thread: https://stackoverflow.com/a/43144197/12787326
This code has been implemented like so below.
public void initialize(URL location, ResourceBundle resources) {
//Initialising path of the media file, replace this with your file path
String path = "Video.mp4";
//Instantiating Media class
Media media = new Media(new File(path).toURI().toString());
//Listener
media.getMetadata().addListener((MapChangeListener<String,Object>) change-> {
System.out.println(change);
});
//Instantiating MediaPlayer class
mediaPlayer = new MediaPlayer(media);
mv.setMediaPlayer(mediaPlayer);
mediaPlayer.setOnReady(() -> {
...
mediaPlayer.play();
});
}
I have tried a variety of different changes to the listener such as placing it in different areas of the code and trying to get the data from the mediaPlayer, which accesses the media itself. None of these seems to have fixed the problem.
I've tried print statements in the code trying to print off the contents of media.getMetadata but all I get is an empty list back.
I've tried creating a listener for media.getTracks() and that works as expected with updating each time the player switches its status. So that makes me wonder what is going wrong with media.getMetadata()
This is what the metadata of the video currently looks like so I assume I should be getting this infomarmation
I think the listener isn't seeing anything change, that's why print(change) is never reached. However, even if that is true, shouldn't me doing a system.out.print(media.getMetaData()) in the setOnAction code give me the data? Because when I try that all I get is an empty list.
Does anyone know how to fix this or tell me what I'm doing wrong?

Related

How do I retrieve video custom labels through the RestFB API using Java?

I'm creating a java application that pulls data from facebook videos using RestFB. How can I retrieve the custom labels from each video?
Although I'm able to pull the normal data from each video, there doesn't seem to be any RestFB function that gets custom labels.
I've tried creating my own function by copying the RestFB source code for getting the title and then changing it according to the data I need, but that doesn't seem to work.
The custom labels field is missing atm in RestFB, so you can wait until a new version is released or write a custom video type like this:
public class CustomLabelsVideo extends Video {
#Facebook("custom_labels")
private List<String> customLabels = new ArrayList<>();
public List<String> getCustomLabels() {
return customLabels;
}
public void setCustomLabels(List<String> customLabels) {
this.customLabels = customLabels;
}
}
You need to use this type in the fetchObject method, and don't forget to add the custom_labels string to the fields you fetch.

MediaBrowserService where to get the media files in onLoadChildren

I'm trying to make a MediaBrowserService. In the past when I want to display all the music files actually on the device, I would use a ContentResolver and load a cursor from the MediaStore.
I can't figure out where I am supposed to do this in a MediaBrowserService.
There is the method: onLoadChildren
The examples I find look like this:
#Override
public void onLoadChildren(
#NonNull final String parentMediaId,
#NonNull final Result<List<MediaBrowserCompat.MediaItem>> result) {
result.sendResult(MusicLibrary.getMediaItems());
}
My question is, do I use my ContentResolver cursor within this method to return a list of MediaItems? Or is there some other internal method to use to go through the device and get all the music files?
Only examples I see online are streaming services which do an HTTP call to get a song list, or they just hard code fake songs to display for testing. I'm trying to allow the service to browse music files that are on the device itself.
I also tried to do the exactly same thing in my app, hitting the same wall as you. Finally I figured out how to do it using the Android's new Paging library. You do have to use a cursor with a ContentResolver at the service side in order to fetch the media items on the device, but in order to not iterate all the cursor's records just to return the children in the onLoadChildren() method, I use the paging library to return only the chunks the user is interested in this moment at the client/UI side. You can read about it in a post I wrote, where I also demonstrate the concept with code samples.

Most efficient way to load a lot of images from URL Android

I'm making this netflix style app in which images are loaded into different categories. Let's say Dog videos (has 15 images), Cat videos (15 images), etc... All the images are loaded from a URL, it kind of takes a while for all to load. I was wondering if there was anything I could do to speed up the process? Or maybe show an empty container then fill it as the images load (that would be cool).
This is what I have done:
I have multiple async calls in one Activity, (1 async call per category)
JSONTask1 dogTask = new JSONTask1();
JSONTask2 catTask = new JSONTask2();
JSONTask3 pigTask = new JSONTask3();
JSONTask4 horseTask = new JSONTask4();
dogTask.execute("");
catTask.execute("");
pigTask.execute("");
horseTask.execute("");
I have all of those in a row in my actual code. Thanks.
I would use the "proxy pattern". Basically, you need to create a class that contains the minimal informations required for the display. In which, you have a preview image.
When ever you load everything you start by showing the preview content, ie : a loading gif for everypicture with the title of the movie or whatever. and basically the proxy would have a "loadImage" method that would make an ajax call or async call and the photos would load one by one. Plus, to make the loading easier, make sure the photos are not oversized.
You can see Picasso answers , in picasso i suggest you this way :
Picasso.with(getApplicationContext()).load("your url").placeholder(R.drawable.your_place_holder).error(R.drawable.showing_when_error_occured)
.into(imageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
}
});
Also another suggestion from me : convert your thumb images to base64 format in backend, then firstly retrieve your thumbs and show them. Then start an async task and change images when successfull.
Like whatsapp. In whatsapp you have thumb images they have so low resolution and super fast. When you click image if you have internet connection they load actual thumb images, and click again they load larger image.
Picasso website :http://square.github.io/picasso/
Load them asynchronously with Picasso, you can even show a placeholder image until the real one is loaded

JAVA gwt - saving info after refreshing page

At the moment , im working with java gwt and i stopped studdenly because one problem occured. I want that my information (for example string) will save after refresh button is clicked.
// user enters something in TextArea textArea1 object
Window.addWindowClosingHandler(new Window.ClosingHandler() {
public void onWindowClosing(Window.ClosingEvent closingEvent) {
//maybe there is a function or what
pleaseSaveInfomation(textArea1);
}
});
I tried this , but i know how to implement it correctly to my source code:
https://stackoverflow.com/a/14220746/5010218
The last(worst) chance is to store data from textArea in file.txt , after refreshing i could read info from file and thats all. But maybe GWT has a specific handler/method/class or what to handle this.
Thats for your opinion and help.
I had the same problem. You can easily overcome it with this.
import com.google.gwt.storage.client.Storage;
private Storage stockStore = null;
stockStore = Storage.getLocalStorageIfSupported();
Please read documentation
http://www.gwtproject.org/doc/latest/DevGuideHtml5Storage.html#UsingStorage
When a browser close a window (because of a refresh, or the user has closed the window, changed the url, etc), a script is not allowed to prevent this action. It's not specific to GWT.
However, you can suggest to the browser-agent to show a confirmation to the user. You can do this with the message property of the closing event.
In GWT:
Window.addWindowClosingHandler(new Window.ClosingHandler() {
public void onWindowClosing(Window.ClosingEvent closingEvent) {
closingEvent.setMessage("Confirm ?");
}
});
You shouldn't rely on this event to store your data, as a lot of condition can prevent you to do this. You should maybe periodically store a draft to the local-storage or to the server.
You probably want to store your data in sessionStorage. In GWT, this the Storage class.

Play audio stream with JavaFX

I'm trying to make simple audio player with JavaFX Mediaplayer component. All local files are fine but i want also implement internet radio.
Code:
public static void main(String[] args) throws URISyntaxException {
new JFXPanel(); //init jfx library
String u = "http://91.121.164.186:8050";
Media m=null;
try {
m = new Media(u);
} catch (MalformedURLException e) {
e.printStackTrace();
}
MediaPlayer player = new MediaPlayer(m);
System.out.println("play");
player.play();
player.setVolume(new Double(1));
}
When I run it like this there is no errors but there is no audio. What's wrong ? What are other posibilities to play radio stream in Java ?
In your current example I can see two errors,
You are trying to run a JAVAFX component on a non-Javafx thread, which will result in error. Try running your program inside the start method. Please go through How to use JavaFX MediaPlayer correctly?
The URL you are trying to access must be a Media Compoenent
Try going through this extremely great example on Javafx Media
http://docs.oracle.com/javafx/2/media/EmbeddedMediaPlayer.zip
N.B. The example has lot more data than your require, but its a great example !
"http://91.121.164.186:8050" is a website, (HTML document), not a audio file. You need to download an audio file, something that the player knows what to do with.

Categories