media metadata observable map is null in java fx media - javafx-2

Hi guys I tried to play a song and print its metadata using java fx 2.1 api using the following code
Media media = new Media(UIandControls.class.getResource("/assets/testData/mom.mp3").toExternalForm());
MediaPlayer mediaPlayer = new MediaPlayer(media);
ObservableMap list=media.getMetadata();
System.out.print(list);
mediaPlayer.play();
for some reason getMetadata() is empty observable Map.
Output looks like this
{}
plz help me.. Thank you.

Need to call mediaPlayer.play() before retrieving metadata.

I had troubles with this at first but I found that using this worked.
mediaPlayer.getMedia().getMetadata().addListener(new MapChangeListener<String, Object>(){
#Override
public void onChanged(Change<? extends String, ?> change) {
if(change.wasAdded()){
if(change.getKey().equals("artist"))
setArtist(change.getValueAdded().toString());
else if(change.getKey().equals("title"))
setTitle(change.getValueAdded().toString());
else if(change.getKey().equals("year"))
setYear(change.getValueAdded().toString());
}
}
});
Also I read that the media data won't always be there right after initialization, therefore calling the mediaPlayer.setOnReady() method can make sure that the data is there.
Example:
mediaPlayer.setOnReady(new Runnable(){
#Override
public void run() {
setTrackLength(mediaPlayer.getMedia().getDuration().toMinutes());
mediaPlayer.currentTimeProperty().addListener(progressChangeListener);
}
});

Related

Background Task UWP - Pass Object with Data

I need to pass an object with some information to consume in my Background Task. I try to search in web but not found any solution. It is possible?
One workarround is save information what I need to pass in isolated storage in my MainProjet and in my BackgroundTask project consume information saved before. But this solution is not beautiful to use.
Someone help me?
Thanks in advance
You can use SendMessageToBackground method
var message = new ValueSet();
message.Add("key",value);
BackgroundMediaPlayer.SendMessageToBackground(message);
In background task listen to this method
public void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayer_MessageReceivedFromForeground;
}
private void BackgroundMediaPlayer_MessageReceivedFromForeground(object sender, MediaPlayerDataReceivedEventArgs e)
{
foreach (string key in e.Data.Keys)
{
switch (key.ToLower())
{
}
}
}

Implementation of the MvxBindableCollectionViewSource

I'm new to Mvvmcross framework and currently exploring the iOS part of it (ohh and also new to iOS development to draw a beautiful picture of my current situation ^^). I'm using the vNext version.
I've found references to implementation of UICollectionViewController (MvxTouchCollectionViewController and MvxBindableCollectionViewSource), but these classes seem to be only a skeleton for a future implementation (abstract class, missing a kind of MvxSimpleBindableCollectionViewSource at least). I haven't found a sample using this feature.
I've also found a blog post from Stuart which lets presume he's working on this part (Work In Progress - MvvmCross lists sample).
Does anybody already play with this part and know about an implementation or usage example?
I've took a look to the 10 first minutes of the xaminar mentioned by Stuart in its article and seems pretty interesting, a good starting point for me.
I've used the collection view controller in several customer apps, but don't think I've published any open source samples that use it.
In essence, the use of the collectionview is very similar to the use of the tableview and cell - which is shown in detail in: http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html
In vNext, a sample controller might look like:
public class MyCollectionView : BaseCollectionView<MyCollectionViewModel>
{
private bool _needToCallViewDidLoadManually;
public HubView (MvxShowViewModelRequest request)
: base(request, new UICollectionViewFlowLayout (){
ItemSize= new System.Drawing.SizeF (100, 100),
MinimumInteritemSpacing = 20.0f,
SectionInset = new UIEdgeInsets (10,50,20,50),
ScrollDirection = UICollectionViewScrollDirection.Vertical,
})
{
if (_needToCallViewDidLoadManually) {
ViewDidLoad();
}
}
public override void ViewDidLoad ()
{
if (ShowRequest == null) {
_needToCallViewDidLoadManually = true;
return;
}
base.ViewDidLoad ();
_needToCallViewDidLoadManually = false;
var source = new CollectionViewSource(CollectionView);
this.AddBindings(
new Dictionary<object, string>()
{
{ source, "ItemsSource TheItems" }
});
CollectionView.Source = source;
CollectionView.ReloadData();
}
public class CollectionViewSource : MvxBindableCollectionViewSource
{
public CollectionViewSource (UICollectionView collectionView)
: base(collectionView, MyViewCell.Identifier)
{
collectionView.RegisterNibForCell(UINib.FromName(MyViewCell.Identifier, NSBundle.MainBundle), MyViewCell.Identifier);
}
}
}
If you are starting development now, then you might also benefit from considering the v3 branch which is just entering Beta.

Key Input in javaFx 2.0

I am currently working on a javaFX 2.0 game and I want to know how to make it so when I type a specific key something will happen. Thank You for taking your time for reading this.
Make your root node focused and then write your logic in the setOnKeyPressed method.
Sample code:
root.setFocusTraversable(true);
root.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent kEvent) {
if (kEvent.getCode() == KeyCode.SPACE) {
System.out.println("Space Bar pressed");
}
}
});

Lwuit touch screen strange behaviour

I am making an application using LWUIT.
There is a form
There is a list embedded on the form.
The list has 5 elements.
Initially, when I first load the app, if I choose the 1st element, 2nd gets chosen; when I choose the second the 3rd gets chose and and so on (Weird!)
I am not able to click any button on the screen either
next what I do is, shift to a different from using arrow keys (of the keyboard... I am running the app on a simulator btw)
Then I come back to the first form and now everything works as expected(no weird behaviour).
What could be the issue?
I am using Sun Java Micro Edition SDK 3.0 (default touch screen for testing)
My code is:
List dummy = new List();
dummy.addItem("wewerwer");
dummy.addItem("wewerdswer");
dummy.addItem("wewqweerwer");
dummy.addItem("dscxwewerwer");
dummy.addItem("jhgwewerwer");
mainListForm.setLayout(new BorderLayout());
mainListForm.addComponent(BorderLayout.CENTER,dummy);
mainListForm.show();
What could possible be going wrong here?
UPDATE 1
I think there is a bug here. I have attached the complete code below along with the screen shot
import javax.microedition.midlet.*;
import com.sun.lwuit.*;
import com.sun.lwuit.events.*;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
public class Demo extends MIDlet implements ActionListener {
private Form mForm;
List abc;
public void startApp() {
Display.init(this);
try {
Resources r = Resources.open("/Test.res");
UIManager.getInstance().setThemeProps(r.getTheme(
r.getThemeResourceNames()[0])
);
} catch (Exception e){
System.out.println(e.toString());
}
if (mForm == null) {
Button click = new Button("Press me!");
click.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
System.out.println("I have been pressed");
}
});
abc = new List();
abc.addItem("Str1");
abc.addItem("Str2");
abc.addItem("Str3");
abc.addItem("Str4");
abc.addItem("Str5");
abc.addItem("Str6");
Form f = new Form("Hello, LWUIT!");
abc.addActionListener(this);
f.addComponent(abc);
Command exitCommand = new Command("Exit");
f.addCommand(exitCommand);
f.addCommandListener(this);
f.addComponent(click);
f.show();
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void actionPerformed(ActionEvent ae) {
System.out.println(abc.getSelectedIndex());
}
}
So now when I click on 'Str1' of the list Str2 gets selected and so on.
IDE: Netbeans
Emulator: Default Touch screen phone
On the action event set the list to active again after the event by invoking setHandlesInput(true)
OK....so this is how you resolve it.
After the form is displayed remove the list from the form and again add it to the form and then repaint the form.
Earlier Code
1) form.addComponenet(BorderLayout.center,list);
2) form.show();
Word Around for the problem
1)form.addComponenet(BorderLayout.center,list);
2)form.show();
3)form.setScrollable(false);
I know its kind of strange, but this way the list index selection works smooth for touch screen phones.

J2ME Uncaught Exception

I plan to start my first lesson in j2me, and I download a simple book and I try my first program.
When I take a second step to add commands, I face an error message which is:
uncaught exception java/lang/noclassdeffounderror: readfile.
So, would you please help me to understand this message? and how to solve it?
Please find my code below.
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ReadFile extends MIDlet implements CommandListener
{
private Form form1;
private Command Ok, Quit;
private Display display;
private TextField text1;
public void startApp()
{
form1 = new Form( "TA_Pog" );
Ok = new Command("Ok", Command.OK, 1);
Quit = new Command("Quit", Command.EXIT, 2);
form1.setCommandListener(this);
form1.addCommand(Ok);
form1.addCommand(Quit);
text1 = new TextField("Put Your Name :","His Name : " , 32, TextField.URL );
form1.append(text1);
display = Display.getDisplay(this);
display.setCurrent(form1);
}
public void commandAction(Command c , Displayable d)
{
if (c == Ok)
{
Alert a = new Alert("Alert","This Alert from Ok Button", null, AlertType.ALARM);
a.setTimeout (3000);
display.setCurrent(a,this.form1);
}
else
{
this.notifyDestroyed();
}
}
public void pauseApp() {}
public void destroyApp( boolean bool ) {}
}
Note: the code above is taken exactly from a book.
Thanks in advance
Besr regards
uncaught exception java/lang/noclassdeffounderror: readfile.
I somehow doubt the message is exactly as you describe it. Does it look more like below?
uncaught exception java/lang/NoClassDefFoundError: ReadFile
Please keep in mind in Java it matters much whether you use lower or upper case letters. As long as you don't pay attention to stuff like that, you are likely be getting a lot of problems like that.
Now, take a closer look at your class name:
public class ReadFile //...
The exception you are getting most likely says that Java machine can't find the class you try to use. There is something wrong with your build/compilation.
I run your code. It's running good. I think you have to clean and build your project. Firstly go to project properties and then go to Application Descriptor and click on Midlet tab, and select your midlet and press ok then clean build, run it.

Resources