Помогите решить проблему.
Надо в приложении проигрывать mp3.
на данный момент wav работает но надо mp3.
Код
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
83.
84.
85.
86.
87.
88.
89.
90.
91.
92.
93.
94.
95.
96.
97.
98.
99.
100.
101.
102.
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
145.
146.
147.
148.
149.
150.
151.
152.
153.
154.
155.
156.
157.
158.
159.
160.
161.
162.
163.
164.
165.
166.
167.
168.
169.
170.
import javax.media.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
/**
* User: dima
* Date: 07.07.2005
* Time: 16:07:21
*/
public class Main extends JFrame implements ControllerListener {
Main main;
public Player player = null ;
Component visualComponent = null ;
Component controlComponent = null ;
Component progressBar = null ;
boolean firstTime = true;
long CachingSize = 0L;
int controlPanelHeight = 0 ;
int videoWidth = 0 ;
int videoHeight = 0 ;
JPanel panel = new JPanel( new FlowLayout());
JPanel panel2 = new JPanel( new FlowLayout());
public static void main(String[] args){
new Main();
}
public Main() {
this .setTitle("MinorAmp");
this .setSize( 300 , 400 );
this .setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
main = this ;
addComponents();
this .setVisible(true);
}
public void startPlay(){
player.realize();
}
public void addComponents() {
Container cp = this .getContentPane();
cp.setLayout( new FlowLayout());
cp.add(panel);
cp.add(panel2);
JButton button = new JButton("Start");
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(panel);
MediaLocator mrl = null ;
URL url = null ;
String mediaFile = "file://"+chooser.getSelectedFile().getAbsolutePath();
try {
url = new URL(mediaFile);
mediaFile = url.toExternalForm();
} catch (MalformedURLException mue) {
System.out.println(mue.toString());
}
System.out.println(url);
try {
// Create a media locator from the file name
if ((mrl = new MediaLocator(mediaFile)) == null ) Fatal("Can't build URL for " + mediaFile);
try {
player = Manager.createPlayer(mrl);
} catch (NoPlayerException e) {
System.out.println(e);
Fatal("Could not create player for " + mrl);
}
// Add ourselves as a listener for a player's events
player.addControllerListener(main);
} catch (MalformedURLException e) {
Fatal("Invalid media file URL!");
} catch (IOException e) {
Fatal("IO exception creating player for " + mrl);
}
startPlay();
}
});
panel2.add(button);
}
public void controllerUpdate(ControllerEvent event) {
// If we're getting messages from a dead player,
// just leave
if (player == null)
return;
// When the player is Realized, get the visual
// and control components and add them to the Applet
if (event instanceof RealizeCompleteEvent) {
if (progressBar != null) {
panel.remove(progressBar);
progressBar = null;
}
int width = 320;
int height = 0;
if (controlComponent == null) {
if ((controlComponent = player.getControlPanelComponent()) != null) {
controlPanelHeight = controlComponent.getPreferredSize().height;
panel.add(controlComponent);
height += controlPanelHeight;
}
}
if (visualComponent == null) {
if ((visualComponent = player.getVisualComponent()) != null) {
panel.add(visualComponent);
Dimension videoSize = visualComponent.getPreferredSize();
videoWidth = videoSize.width;
videoHeight = videoSize.height;
width = videoWidth;
height += videoHeight;
visualComponent.setBounds(0, 0, videoWidth, videoHeight);
}
}
panel.setBounds(0, 0, width, height);
if (controlComponent != null) {
controlComponent.setBounds(0, videoHeight, width, controlPanelHeight);
controlComponent.invalidate();
}
} else if (event instanceof CachingControlEvent) {
if (player.getState() > Controller.Realizing)
return;
// Put a progress bar up when downloading starts,
// take it down when downloading ends.
CachingControlEvent e = (CachingControlEvent) event;
CachingControl cc = e.getCachingControl();
// Add the bar if not already there ...
if (progressBar == null) {
if ((progressBar = cc.getControlComponent()) != null) {
panel.add(progressBar);
panel.setSize(progressBar.getPreferredSize());
validate();
}
}
} else if (event instanceof EndOfMediaEvent) {
// We've reached the end of the media; rewind and
// start over
player.setMediaTime(new Time(0));
player.start();
} else if (event instanceof ControllerErrorEvent) {
// Tell TypicalPlayerApplet.start() to call it a day
player = null;
Fatal(((ControllerErrorEvent) event).getMessage());
} else if (event instanceof ControllerClosedEvent) {
panel.removeAll();
}
}
void Fatal(String s) {
// Applications will make various choices about what
// to do here. We print a message
System.err.println("FATAL ERROR: " + s);
throw new Error(s); // Invoke the uncaught exception
// handler System.exit() is another
// choice.
}
}
Взято с сайта java.
выдаёт при загрузке mp3
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
file://D:/Musik/01.mp3
Error: Unable to realize com.sun.media.amovie.AMController@10efd7c
FATAL ERROR: Failed to realize
Exception in thread "JMF thread: SendEventQueue: com.sun.media.content.audio.mpeg.Handler" java.lang.Error: Failed to realize
at com.minor.projects.mediaplayer.mpv01.Main.Fatal(Main.java: 169 )
at com.minor.projects.mediaplayer.mpv01.Main.controllerUpdate(Main.java: 158 )
at com.sun.media.BasicController.dispatchEvent(BasicController.java: 1254 )
at com.sun.media.SendEventQueue.processEvent(BasicController.java: 1286 )
at com.sun.media.util.ThreadedEventQueue.dispatchEvents(ThreadedEventQueue.java: 65 )
at com.sun.media.util.ThreadedEventQueue.run(ThreadedEventQueue.java: 92 )
Error value: 800c000d
Я просто никогда не работал JMF седни токо начал.
Надо срочно, выручайте.