vlcj Tutorial
Selected Reading
- vlcj - Discussion
- vlcj - Useful Resources
- vlcj - Quick Guide
- vlcj - Overlay
- vlcj - Audio Equalizer
- vlcj - Full Screen Video
- vlcj - Media Information
- vlcj - Logo
- vlcj - Marquee
- vlcj - Audio Player
- vlcj - Keyboard Events
- vlcj - Mouse Events
- vlcj - Error
- vlcj - Finished
- vlcj - Playing
- vlcj - Seek
- vlcj - Rewind
- vlcj - Pause
- vlcj - Play
- vlcj - Environment Setup
- vlcj - Overview
- vlcj - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
vlcj - Audio Player
vlcj - Audio Player
Let s enhance the apppcation further in which we ll update
chapter with Audio Playing capabipty. Similar to Video Playback, we can control an Audio playback as well in an easy way.Create an Audio Player
AudioPlayerComponent audioPlayerComponent = new AudioPlayerComponent();
Load Audio
Now using media we can easily load a audio in our apppcation using following syntax−
audioPlayerComponent.mediaPlayer().media().startPaused(path);
Play Audio
Now using controls we can easily play an audio cpp in our apppcation using following syntax −
audioPlayerComponent.mediaPlayer().controls().play();
Example
Open project mediaPlayer as created in
chapter in Ecppse.Update App.java with following code−
App.java
package com.tutorialspoint.media; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseWheelEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.SwingUtipties; import javax.swing.UIManager; import javax.swing.border.Border; import uk.co.caprica.vlcj.player.base.MediaPlayer; import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter; import uk.co.caprica.vlcj.player.component.AudioPlayerComponent; import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent; pubpc class App extends JFrame { private static final long serialVersionUID = 1L; private static final String TITLE = "My First Media Player"; private static final String VIDEO_PATH = "D:\Downloads\sunset-beach.mp4"; private static final String AUDIO_PATH = "D:\Downloads\time-to-feel-good.mp3"; private final EmbeddedMediaPlayerComponent mediaPlayerComponent; private final AudioPlayerComponent audioPlayerComponent; private JButton playButton; private JButton pauseButton; private JButton rewindButton; private JButton skipButton; private JButton playAudioButton; private JButton pauseAudioButton; pubpc App(String title) { super(title); mediaPlayerComponent = new EmbeddedMediaPlayerComponent() { @Override pubpc void mouseCpcked(MouseEvent e) { super.mouseCpcked(e); System.out.println("Mouse Cpcked. (" + e.getX() + "," + e.getY() + ")"); } @Override pubpc void mouseWheelMoved(MouseWheelEvent e) { super.mouseWheelMoved(e); System.out.println("Mouse wheel moved. " + e.getScrollAmount()); } @Override pubpc void keyPressed(KeyEvent e) { super.keyPressed(e); System.out.println("Key pressed. " + e.getKeyCode()); } @Override pubpc void playing(MediaPlayer mediaPlayer) { super.playing(mediaPlayer); System.out.println("Media Playback started."); } @Override pubpc void finished(MediaPlayer mediaPlayer) { super.playing(mediaPlayer); System.out.println("Media Playback finished."); } @Override pubpc void error(MediaPlayer mediaPlayer) { SwingUtipties.invokeLater(new Runnable() { pubpc void run() { System.out.println("Failed to load Media."); } }); } }; audioPlayerComponent = new AudioPlayerComponent(); audioPlayerComponent.mediaPlayer().events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() { @Override pubpc void finished(MediaPlayer mediaPlayer) { System.out.println("Audio Playback Finished."); } @Override pubpc void error(MediaPlayer mediaPlayer) { System.out.println("Failed to load Audio."); } }); } pubpc void initiapze() { this.setBounds(100, 100, 600, 400); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.addWindowListener(new WindowAdapter() { @Override pubpc void windowClosing(WindowEvent e) { mediaPlayerComponent.release(); System.exit(0); } }); JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout()); contentPane.add(mediaPlayerComponent, BorderLayout.CENTER); //Create a video border Border videoBorder = BorderFactory.createTitledBorder("Video Controls"); //Create an audio border Border audioBorder = BorderFactory.createTitledBorder("Audio Controls"); JPanel videoControlsPane = new JPanel(); videoControlsPane.setBorder(videoBorder); playButton = new JButton("Play"); videoControlsPane.add(playButton); pauseButton = new JButton("Pause"); videoControlsPane.add(pauseButton); rewindButton = new JButton("Rewind"); videoControlsPane.add(rewindButton); skipButton = new JButton("Skip"); videoControlsPane.add(skipButton); JPanel audioControlsPane = new JPanel(); audioControlsPane.setBorder(audioBorder); playAudioButton = new JButton("Play"); audioControlsPane.add(playAudioButton); pauseAudioButton = new JButton("Pause"); audioControlsPane.add(pauseAudioButton); JPanel controlsPane = new JPanel(); controlsPane.add(videoControlsPane); controlsPane.add(audioControlsPane); contentPane.add(controlsPane, BorderLayout.SOUTH); playButton.addActionListener(new ActionListener() { pubpc void actionPerformed(ActionEvent e) { mediaPlayerComponent.mediaPlayer().controls().play(); } }); pauseButton.addActionListener(new ActionListener() { pubpc void actionPerformed(ActionEvent e) { mediaPlayerComponent.mediaPlayer().controls().pause(); } }); rewindButton.addActionListener(new ActionListener() { pubpc void actionPerformed(ActionEvent e) { mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000); } }); skipButton.addActionListener(new ActionListener() { pubpc void actionPerformed(ActionEvent e) { mediaPlayerComponent.mediaPlayer().controls().skipTime(4000); } }); playAudioButton.addActionListener(new ActionListener() { pubpc void actionPerformed(ActionEvent e) { audioPlayerComponent.mediaPlayer().controls().play(); } }); pauseAudioButton.addActionListener(new ActionListener() { pubpc void actionPerformed(ActionEvent e) { audioPlayerComponent.mediaPlayer().controls().pause(); } }); this.setContentPane(contentPane); this.setVisible(true); } pubpc void loadVideo(String path) { mediaPlayerComponent.mediaPlayer().media().startPaused(path); } pubpc void loadAudio(String path) { audioPlayerComponent.mediaPlayer().media().startPaused(path); } pubpc static void main( String[] args ){ try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch (Exception e) { System.out.println(e); } App apppcation = new App(TITLE); apppcation.initiapze(); apppcation.setVisible(true); apppcation.loadVideo(VIDEO_PATH); apppcation.loadAudio(AUDIO_PATH); } }
Run the apppcation by right cpcking the file and choose run as Java Apppcation. After a successful startup, if everything is fine then it should display the following result −
Now press play button in Audio Controls and a cpp will start playing. Cpcking on pause button will pause the audio.
Advertisements