• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading an mp3 file

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Am looking for tips on executing an mp3 file using java.

My current application searches relevant directories and creates jbuttons for each song in the directory. What I am looking to do next is for when a user clicks on a button, the relevant mp3 file is played via the default media player (ie Windows media player).

Anyone have any tips on how this may be achieved using the Java 5 API?

Thanks in advance (apologies if this is impossible!)

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.awt.Desktop.open(File) should do the trick. Otherwise you can create a process for

This only works on Windows though.
 
And Green
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:java.awt.Desktop.open(File) should do the trick. Otherwise you can create a process for

This only works on Windows though.



Hello, thanks for that Rob, but I am unfamiliar with the package java.awt.Desktop. It is not part of the standard java 5 or 6 api's, is it a package I need to download from somewhere?

Regards.
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is part of the standard Java APIs, but not until Java 6.
http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html

If you're using Java 5.0 or before you have to find alternatives.
 
And Green
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:It is part of the standard Java APIs, but not until Java 6.
http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html

If you're using Java 5.0 or before you have to find alternatives.



Cheers for that. It looks promising. If I try to create an object of type Desktop ie :

Desktop mediaplayer = new Desktop();

and then call the open method on it, it gives me the error:

Desktop() has private access in java.awt.Desktop

and it points to the new keyword as the cause of the problem.

I've never come accross anything like this before, any ideas most appreciated!

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the javadocs of that class - the constructor isn't listed, because it's not public. You can obtain a Desktop object via

Desktop mediaplayer = Desktop.getDesktop()

Note that this will not work when run in an IDE; that's what the Desktop.isDesktopSupported() method guards against.
 
And Green
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Look at the javadocs of that class - the constructor isn't listed, because it's not public. You can obtain a Desktop object via

Desktop mediaplayer = Desktop.getDesktop()

Note that this will not work when run in an IDE; that's what the Desktop.isDesktopSupported() method guards against.



I get you, yes thanks.

I wondered if anyone has been having a simalar problem to this. I am getting a runtime error now. the program can open documents but not mp3's:

import java.io.*;
import java.awt.*;

public class music {

public static void main (String [] args)throws Exception {

File tester = new File("C:/etc/etc/asong.mp3");

Desktop mediaplayer = Desktop.getDesktop();

mediaplayer.open(tester);

}

}

Error on cmd line given as :

Failed to open file Invalid menu handle

at sun.awt.windows.WDEsktopPeer.ShellExecute(WDesktopPeer.java:59)
at sun.awt.windows.WDEsktopPeer.open(WDesktopPeer.java:36)
at java.awt.Desktop.open(desktop.jav:254)
at music.main(music.java:14)


Banging head against brick wall error!

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to make sure: is any program associated with MP3s? That is to say, if you double-click that file, does WMP open?
 
And Green
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:Just to make sure: is any program associated with MP3s? That is to say, if you double-click that file, does WMP open?



Yes, WMP opens when an mp3 or wma is clicked. The strange thing is, the above program works with mp4 video and this is opened with WMP, no problem
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic