Javafx 简明教程
JavaFX - Handling Media
通常来说,媒体是指各种通信方式,如音频、视频以及可以是单向或双向的其他形式。它以音乐、电影、新闻、视频播客等形式出现。
In general, media refers to various means of communication such as audio, video, and other forms that can be both one-way or two-way. It is used in the form of music, movies, news, vlogs and so forth.
由于媒体使用已经成为我们生活中必不可少的一部分,JavaFX 创建者开发了一组 Java API,这可以实现 JavaFX 应用程序中对媒体的使用。
Since media usage became an essential part of our lives, JavaFX creators developed a set of Java APIs that enables the use of media within JavaFX applications.
Handling Media in JavaFX
为了处理媒体内容,JavaFX 提供了 javafx.scene.media 包,使开发者能够在支持媒体播放的平台上将媒体播放器嵌入到桌面窗口或网页中。此包包含用于组合使用以处理媒体内容的下列类:
To handle media, JavaFX provides javafx.scene.media package that allows developers to embed a media player within the desktop window or a web page on supported platforms for media playback. This package contains the following classes that are used in combination to handle media content −
-
Media − It contains pieces of information like source, resolution, and metadata of a specified media resource.
-
MediaPlayer − It provides the controls for playing media.
-
MediaView − It is a node object used to provide support for animation and other effects to the media resource.
上述类并非独立存在的,它们总被组合使用来创建一个内嵌媒体播放器。控制媒体播放所需的所有属性和方法均在 MediaPlayer 类中提供。更具体地说, play() 、 stop() 和 pause() 方法用于控制媒体播放。要调节音量级别,我们使用 VOLUME 变量。音量级别的范围从 0 到 1.0(最大值)。
The classes mentioned above are not independent, they are always used in the combination to create an embedded media player. All attributes and methods needed to control media playback are made available in MediaPlayer class. More specifically, the play(), stop(), and pause() methods are used to control media playback. To adjust the volume level, we use VOLUME variable. The range of volume level is from 0 to 1.0 (the maximum value).
请记住,如果没有 MediaView 类,我们无法看到媒体播放器播放的媒体。
Remember, without the MediaView class, we can’t view the media being played by media player.
还有一些用于处理以下事件的其他附加方法:
There are some other additional methods that are used to handle the following events −
-
Buffers data
-
Whenever any errors of the MediaErrorEvent class occur.
-
When media stops because media player has not received data fast enough to continue playing.
-
When the media player reaches the end of media.
Supported format of Media in JavaFX
JavaFX 在支持 JavaFX 的设备上支持以下媒体格式:
The following media formats are supported in JavaFX −
S.No |
Media & Format |
1 |
Audio MP3, AIFF , WAV, and MPEG-4 |
2 |
Video FLV containing VP6 video and MP3 audio, and MPEG-4 |
Features provided by JavaFX media
JavaFX 媒体在支持 JavaFX 的设备上提供了以下功能:
The following features are provided by JavaFX media on devices that support JavaFX −
-
It supports multiple playback functions, such as Play, Pause, Stop, Volume, and Mute.
-
It allows navigation through audio/video in a forward or backward direction as needed.
-
It provides support for HTTP and File protocols.
-
Progressive download
-
It also supports HTTP live streaming.
Handling Media Reference
以下是用于在 JavaFX 中处理媒体的类和方法。
Following are the classes and methods that are used for handling media in JavaFX.
Media
JavaFx 中的 Media 类就像音频视频文件的容器。它包含指定媒体资源的一些信息,例如来源、分辨率和元数据。下面列出了一些 Media 类的函数:
The Media class in the JavaFx is like a container for audio video files. It contains pieces of information like source, resolution, and metadata of a specified media resource. Some of the methods of the 'Media' class listed below are −
Sr.No. |
Methods & Description |
1 |
getDuration()Retrieve the duration in seconds of the media. |
2 |
getWidth()Retrieve the width in pixels of the media. |
3 |
getHeight()Retrieve the height in pixels of the media. |
4 |
getSource()Retrieve the source URI of the media. |
5 |
getError()Return any error encountered in the media. |
MediaPlayer
MediaPlayer 类控制音频和视频文件,并允许诸如播放、暂停和停止等功能,使媒体控制变得容易。下面列出了一些 MediaPlayer 类的函数:
The MediaPlayer class controls audio and video files and allows for functions like play, pause, and stop, making media control easy. Some of the methods of the 'MediaPlayer' class listed below are −
Sr.No. |
Methods & Description |
1 |
getStatus()Retrieves the current player status. |
2 |
getVolume()Retrieves the audio playback volume. |
3 |
setRate()Sets the playback rate to the supplied value. |
4 |
isAutoPlay()Retrieves the autoPlay property value. |
5 |
getBlance()Retrieves the audio balance. |
6 |
setMute()Sets the value of muteProperty(). |
7 |
getStartTime()Retrieves the start time. |
8 |
getStopTime()Retrieves the stop time. |
9 |
getTotalDuration()Retrieves the total playback duration including all cycles (repetitions). |
10 |
stop()Stops playing the media. |
MediaView
JavaFX 中的 MediaView 类是一个用于显示由 MediaPlayer 播放的视频或媒体的特殊类。下面列出了一些 MediaView 类的函数:
The MediaView class in JavaFX is a special class for displaying videos or media played by a MediaPlayer. Some of the methods of the 'MediaView' class listed below are −
Sr.No. |
Methods & Description |
1 |
getX()Retrieves the x coordinate of the MediaView origin. |
2 |
getY()Retrieves the y coordinate of the MediaView origin. |
3 |
setSmooth()Sets whether to smooth the media when scaling. |
4 |
getViewPort()Retrieves the rectangular viewport into the media frame. |
5 |
isPreserveRatio()Sets whether to preserve the media aspect ratio when scaling. |
6 |
getFitHeight()Retrieves the height of the bounding box of the resized media. |
7 |
getFitWidth()Retrieves the width of the bounding box of the resized media. |