音频播放器在JMF中使用MediaPlayer类 [英] Audio Player using MediaPlayer class in JMF
问题描述
JMF提供了一个类 MediaPlayer
,可用作全功能播放器。但是,我找不到播放音频文件的方法。一些代码片段使用。
import javax.media.bean.playerbean.MediaPlayer;
// ....
MediaPlayer mp = new MediaPlayer();
mp.setMediaLocation(文件的位置); //< - 语法错误!
mp.start();
但它不工作。 Eclipse显示此错误:
令牌setMediaLocation上的语法错误,此令牌后面的标识符
.. on setMediaLocation()
方法。
有人可以告诉我如何使用 MediaPlayer
类来播放音频文件?我的编码有什么问题?
你没有字面上有 .. setMediaLocation文件);
在源中?我认为这是改写的。
在这种情况下,答案是为 setMediaLocation()
方法。文件。指定一个 String
或 MediaLocator
实例。
我避免使用 String
实例来表示基于文件或URL的源。如果一个方法需要一个文件
或 URL
- 给它一个文件
或 URL
。这是一个最好的选择,作为一个 MediaLocator(URL)
。这是怎么回事首先,我们需要一个 URL
- 根据URL的来源,有多种不同的获取方法。例如。互联网,本地文件系统上的文件或与应用程序交付的Jar中的嵌入式资源。最后两个可能是这样的:
基于文件的URL
文件mediaFile = new File(user.mp3);
URL mediaURL = mediaFile.toURI()。toURL();
// ...
嵌入资源网址
URL mediaURL = this.getClass()。getResource(/ path / to / our.mp3);
// ...
一旦有一个 mediaURL
实例,我们可以创建一个定位器并在方法中使用它。
使用URL
MediaLocator mediaLocator = new MediaLocator(mediaURL);
MediaPlayer mp = new MediaPlayer();
mp.setMediaLocation(mediaLocator);
// ...
一般提示
$ ol
JMF provides a class MediaPlayer
which can be used as a full-featured player. However, I can't find a way to play an audio file. Some snippets of the code used.
import javax.media.bean.playerbean.MediaPlayer;
// ....
MediaPlayer mp = new MediaPlayer();
mp.setMediaLocation(location of file); // <- syntax error!
mp.start();
But it doesn't work. Eclipse shows this error:
Syntax error on token "setMediaLocation", Identifier expected after this token
..on setMediaLocation()
method.
Can someone tell me how to use MediaPlayer
class to play audio file? and what is wrong in my coding?
You do not literally have ..setMediaLocation(location of file);
in the source do you? I thought that was paraphrased.
In that case, the answer is to supply an argument to the setMediaLocation()
method. The docs. specify a String
or MediaLocator
instance.
I avoid using String
instances to represent file or URL based sources. If a method needs a File
or URL
- give it a File
or URL
. That leaves the best option as a MediaLocator(URL)
. Here is how it might go. First we need a URL
- there are a number of different ways of getting one, depending on what the source of the URL is. E.G. the internet, a file on the local file-system, or an embedded resource in a Jar delivered with the app. The last two might be something like:
File based URL
File mediaFile = new File("user.mp3");
URL mediaURL = mediaFile.toURI().toURL();
// ...
Embedded resource URL
URL mediaURL = this.getClass().getResource("/path/to/our.mp3");
// ...
Once there is a mediaURL
instance, we can create a locator and use it in the method.
Using the URL
MediaLocator mediaLocator = new MediaLocator(mediaURL);
MediaPlayer mp = new MediaPlayer();
mp.setMediaLocation(mediaLocator);
// ...
General tips
- JMF is an abandoned API.
- Since you seem interested only in audio, look to use Java Sound. It can play audio samples just fine, is part of core Java, and with a small part of the JMF, can play MP3 format (see link for details).
- You seem new to the abandoned API being used, Eclipse and debugging. Perhaps you should focus on simpler things for the moment. Media handling is an advanced topic.
这篇关于音频播放器在JMF中使用MediaPlayer类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!