如何正确将路径传递给 MediaPlayer(错误 java.net.URISyntaxException)

huangapple go评论75阅读模式
英文:

How to correctly pass a path to a MediaPlayer (Error java.net.URISyntaxException)

问题

我想在按下按钮时播放一个mp3文件,所以我创建了一个按钮来创建一个音频播放器,这显然需要一个源。我尝试过通过路径(Path)或URI来传递源,但它不起作用,我总是会收到相同的错误(URISyntaxException)。
我像这样初始化我的按钮:

public class GUI
{
    private String localFolder=System.getProperty("user.dir");
    // 我正在获取我的应用程序执行的实际文件夹位置

    private void audioInitialize() {
        localFolder += File.separator + "src" + File.separator + "InUse" + File.separator;
        File saves = new File(localFolder);  // 需要路径用于其他用途
        AudioButton button0 = new AudioButton("SONG", saves.getPath() + File.separator + "song.mp3", true);
        buttons.add(button0);
        /*
        *
        *
        */
    }
}

这是我的AudioButton类:

public class AudioButton extends JButton implements ActionListener
{
    private String uri;
    private final boolean isCustom;

    public AudioButton(String label, String uri, boolean isCustom) {
        this.uri = uri;
        this.isCustom = isCustom;
        this.setText(label);
        this.setBackground(new Color(205, 205, 205));
        this.setBorderPainted(true);
        this.addActionListener(this);
    }

    public String getUri() {
        return this.uri;
    }

    public boolean isCustom() {
        return this.isCustom;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        AudioPlayer player = new AudioPlayer(this.uri);
        player.getPlayer().play();
    }
}

这是AudioPlayer:

public class AudioPlayer extends JPanel
{
    private final String uri;
    private Media media;
    private MediaPlayer mediaPlayer;

    public AudioPlayer(String uri) {
        @SuppressWarnings("unused")
        JFXPanel pane = new JFXPanel();
        this.uri = uri;
        media = new Media(uri);
        mediaPlayer = new MediaPlayer(media);
    }

    public MediaPlayer getPlayer() {
        return this.mediaPlayer;
    }
}

这是控制台输出:

Picked up _JAVA_OPTIONS: -Xmx512M
C:\Users\myUser\eclipse-workspace\Folder\src\InUse
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Users\myUser\eclipse-workspace\Folder\src\InUse\song.mp3
	at javafx.media/javafx.scene.media.Media.<init>(Media.java:385)
	at projectName.AudioPlayer.<init>(AudioPlayer.java:18)
	at projectName.AudioButton.actionPerformed(AudioButton.java:27)
	at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
	at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
	...
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Users\myUser\eclipse-workspace\Folder\src\InUse\song.mp3
	at java.base/java.net.URI$Parser.fail(URI.java:2912)
	...

如何解决这个问题呢?

英文:

I want to play a mp3 file when a button is pressed, so I created a button that create an audio player, which require, obviously, a source. I've tried to pass the source by a Path or a URI, but it doesn't work, I'll receive always the same error (URISyntaxException).
I initialize my buttons like this:

public class GUI
{
    private String localFolder=System.getProperty(&quot;user.dir&quot;);
    // I&#39;m taking the actual folder where my app is executed

	private void audioInitialize()	{
		localFolder+=File.pathSeparator+&quot;src&quot;+File.pathSeparator+&quot;InUse&quot;+File.pathSeparator;
		File saves=new File(localFolder);  // need the path for other use
		AudioButton button0=new AudioButton(&quot;SONG&quot;, saves.getPath()+File.pathSeparator+&quot;song.mp3&quot;, true);
		buttons.add(button0);
        /*
        *
        *
        */
}

This is my AudioButton class:

public class AudioButton extends JButton implements ActionListener
{
	private String uri;
	private final boolean isCustom;
	public AudioButton(String label, String uri, boolean isCustom)	{
		this.uri=uri;
		this.isCustom=isCustom;
		this.setText(label);
		this.setBackground(new Color(205, 205, 205));
		this.setBorderPainted(true);
		this.addActionListener(this);
	}
	public String getUri()	{return this.uri;}
	public boolean isCustom()	{return this.isCustom;}
	@Override
	public void actionPerformed(ActionEvent e)
	{
		AudioPlayer player=new AudioPlayer(this.uri);
		player.getPlayer().play();
	}
}

There is AudioPlayer:

public class AudioPlayer extends JPanel
{
	private final String uri;
	private Media media;
	private MediaPlayer mediaPlayer;
	public AudioPlayer(String uri)	{
		@SuppressWarnings(&quot;unused&quot;)
		JFXPanel pane=new JFXPanel();
		this.uri=uri;
		media=new Media(uri);
		mediaPlayer=new MediaPlayer(media);
	}
	public MediaPlayer getPlayer()	{return this.mediaPlayer;
}

And this is the console output:

Picked up _JAVA_OPTIONS: -Xmx512M
C:\Users\myUser\eclipse-workspace\Folder\src\InUse
Exception in thread &quot;AWT-EventQueue-0&quot; java.lang.IllegalArgumentException: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Users\myUser\eclipse-workspace\Folder\src\InUse\song.mp3
	at javafx.media/javafx.scene.media.Media.&lt;init&gt;(Media.java:385)
	at projectName.AudioPlayer.&lt;init&gt;(AudioPlayer.java:18)
	at projectName.AudioButton.actionPerformed(AudioButton.java:27)
	at java.desktop/javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1967)
	at java.desktop/javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2308)
	at java.desktop/javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:405)
	at java.desktop/javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:262)
	at java.desktop/javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:269)
	at java.desktop/java.awt.Component.processMouseEvent(Component.java:6578)
	at java.desktop/javax.swing.JComponent.processMouseEvent(JComponent.java:3343)
	at java.desktop/java.awt.Component.processEvent(Component.java:6343)
	at java.desktop/java.awt.Container.processEvent(Container.java:2259)
	at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4961)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2317)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4793)
	at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
	at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4539)
	at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4480)
	at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2303)
	at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2758)
	at java.desktop/java.awt.Component.dispatchEvent(Component.java:4793)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:766)
	at java.desktop/java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:717)
	at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:711)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:89)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:99)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:739)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:737)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:89)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:736)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:199)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
Caused by: java.net.URISyntaxException: Illegal character in opaque part at index 2: C:\Users\myUser\eclipse-workspace\Folder\src\InUse\song.mp3
	at java.base/java.net.URI$Parser.fail(URI.java:2912)
	at java.base/java.net.URI$Parser.checkChars(URI.java:3083)
	at java.base/java.net.URI$Parser.parse(URI.java:3119)
	at java.base/java.net.URI.&lt;init&gt;(URI.java:598)
	at javafx.media/javafx.scene.media.Media.&lt;init&gt;(Media.java:383)
	... 38 more

How can I resolve this problem?

答案1

得分: 0

已找到解决方法:在AudioPlayer中,创建Media对象时,应将文件URI作为字符串传递给构造函数(很可能是这样,否则,根据错误信息我可以理解会出现解析问题):

 public AudioPlayer(String uri)  {
        @SuppressWarnings("unused")
        JFXPanel pane=new JFXPanel();
        this.uri=uri;
        media=new Media(new File(uri).toUri().toString()); // 将新文件的URI作为字符串提供
        mediaPlayer=new MediaPlayer(media);
    }
英文:

Figured out how to solve the problem: in AudioPlayer, when creating the Media object you should give the constructor a file uri as a string (probably, otherwise, it create parsing problems as I could understand from errors):

 public AudioPlayer(String uri)  {
        @SuppressWarnings(&quot;unused&quot;)
        JFXPanel pane=new JFXPanel();
        this.uri=uri;
        media=new Media(new File(uri).toUri().toString()); // giving the uri of a new file as a string
        mediaPlayer=new MediaPlayer(media);
    }

huangapple
  • 本文由 发表于 2020年4月10日 22:42:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/61142681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定