JMF application-creating an audio player

Source: Internet
Author: User
Browse
In this section, we will perform the first exercise to create a simple audio player. This example introduces the manager class and the player interface, both of which are important parts for creating most JMF-based applications.
In this example, the function is to play a local audio file on the Character interface. We will learn the source code and understand the tasks of each line. After completing this section, you will have a JMF-based demo program that can play multiple audio files, including MP3, WAV, and AU.
The source code category after this exercise can be used to query the simpleaudioplayer. Java file.

Introduce necessary classes
Simpleaudioplayer includes some calls. In the first few lines of simpleaudioplayer, You need to introduce all necessary classes:
Import javax. Media .*;
Import java. Io. file;
Import java. Io. ioexception;
Import java.net. url;
Import java.net. malformedurlexception;

The javax. Media package is one of multiple packages defined by JMF. Javax. Media is a core package, including defining the manager class and player interface. In this section, we mainly learn the manager class and player interface, and the remaining javax. Media classes are placed in the following sections.
In addition to javax. Media declarations, the above code snippets introduce some input declarations for creating media players.

Player Interface
In the following code snippet, create a public class simpleaudioplayer and define a player variable:

public class SimpleAudioPlayer {
private Player audioPlayer = null;

The term Player sounds familiar, because it is based on our public audio or video Player. In fact, the example of this interface is like a real copy of them. Players reveals functional methods involved in a physical media player (such as a stereo speaker system or VCR. For example, a JMF media player can start and end a media stream. In this section, we will use the start and end features of Player.

Create a Player in a file
Using JMF to obtain a Player instance of a specific media file is very simple. The Manager class creates many special interface types in the same factory in JMF, including the Player interface. Therefore, the responsibility of the Manager class is to create a Player instance, as shown in the following example:

public SimpleAudioPlayer(URL url) throws IOException,
NoPlayerException,
CannotRealizeException {
audioPlayer = Manager.createRealizedPlayer(url);
}
public SimpleAudioPlayer(File file) throws IOException,
NoPlayerException,
CannotRealizeException {
this(file.toURL());
}

After reading the code in this section, you can note that the Manager class contains other methods for creating a Player instance. We will study some of them, such as the instantiation of DataSource or MediaLocator in the subsequent sections.

Player status
JMF defines different statuses that may exist for a large number of Player instances. As follows:
· Prefetched
· Prefetching
· Realized
· Realizing
· Started
· Unrealized

Use these statuses
Because media is often highly resource-intensive, many methods revealed by JMF objects are non-blocking and asynchronous notifications that allow a series of event listening status changes. For example, a Player must pass the Prefetched and Realized statuses before it can be started. Since these changes take some time to complete, the JMF media application can allocate a thread to initialize and create a Player instance, and then continue other operations. When Player is ready, it notifies the application that its status has changed.

In a simple program like ours, the type of versatility is not very important. For this reason, the Manager class also provides some useful methods for creating Realized player. Call a createRealizedPlayer () method to block the call thread until the player reaches the Realized state. To call a method for creating a player without blocking, we use a createPlayer () method in the Manager class. The following code creates

Realized player:
audioPlayer = Manager.createRealizedPlayer(url);

Start and Stop Player
Setting a Player instance to start or stop is like calling a simple authentication method of Player, as shown below:

public void play() {
audioPlayer.start();
}
public void stop() {
audioPlayer.stop();
audioPlayer.close();
}

Call the play () method in the SimpleAudioPlayer class to call the start () method of the Player instance. After calling this method, you can hear the sound file of the local speaker. Similarly, the stop () method stops the player and closes the Player Object.

For reading and playing local media files, disabling the Player instance to release all resources is a useful method. This is a simple example. Disabling Player is an acceptable way to terminate a session. However, in actual applications, you need to be careful to disable it before removing the Player. Once you have disabled the player, you must create a new Player instance (wait for its status to change) before playing a media file again ).

Create a SimpleAudioPlayer

Finally, this media playback application must contain a main () method that can be called by entering commands in the command prompt line. In this main () method, we will call the method for creating SimpleAudioPlayer:

File audioFile = new File(args[0]);
SimpleAudioPlayer player = new SimpleAudioPlayer(audioFile);

The only thing before playing an audio file is to call the method play () of the created audio player, as shown below:

player.play();

To stop and clear the audio player, the main () method should also be called as follows:

player.stop();

Compile and run SimpleAudioPlayer
Compile the sample program by entering javac SimpleAudioPlayer. java in the command prompt line. The created file SimpleAudioPlayer. class is in the current working directory.
Then, enter the following command in the command prompt line to run the example program:

java SimpleAudioPlayer audioFile

Replace audiofile with the audio file on your local machine. All relative file names are compared to the current working directory. You will see some flag information about the currently playing file. To terminate the playback, press Enter.
If compilation fails, make sure that the jmf jar file is correctly included in the classpath environment variable.

Complete source code I tested

Import javax. Media .*;
Import java. Io. file;
Import java. Io. ioexception;
Import java.net. url;
Import java.net. malformedurlexception;

Public class simpleaudioplayer {
 
Private player audioplayer = NULL;
 
Public simpleaudioplayer (URL)
Throws ioexception, noplayerexception, cannotrealizeexception {
 
Audioplayer = manager. createrealizedplayer (URL );
 
}
 
Public simpleaudioplayer (File file)
Throws IOException, NoPlayerException, CannotRealizeException {

This (file. toURL ());

}

Public void play (){

AudioPlayer. start ();

}
 
Public void stop (){

AudioPlayer. stop ();
AudioPlayer. close ();

}
Public static void main (String [] s ){

Try {
File audioFile = new File ("1.mp3 ");

SimpleAudioPlayer player = new SimpleAudioPlayer (audioFile );

Player. play ();

Thread. sleep (10000); // delay: 10 s

Player. stop ();
System. exit (0 );
}
Catch (Exception e ){
E. printStackTrace ();
System. exit (0 );
}
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.