📜  vlcj-音频播放器

📅  最后修改于: 2020-10-14 08:57:24             🧑  作者: Mango


让我们进一步增强应用程序,在其中我们将更新vlcj-具有音频播放功能的“处理键盘事件”一章。与视频播放类似,我们也可以通过简单的方式控制音频播放。

创建一个音频播放器

AudioPlayerComponent audioPlayerComponent = new AudioPlayerComponent();

加载音频

现在使用媒体,我们可以使用以下语法轻松在我们的应用程序中加载音频-

audioPlayerComponent.mediaPlayer().media().startPaused(path); 

播放音讯

现在使用控件,我们可以使用以下语法在我们的应用程序中轻松播放音频剪辑-

audioPlayerComponent.mediaPlayer().controls().play(); 

打开在Eclipse的“环境设置”一章中创建的项目mediaPlayer。

使用以下代码更新App.java-

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.SwingUtilities;
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;

public 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;

   public App(String title) {
      super(title);
      mediaPlayerComponent = new EmbeddedMediaPlayerComponent() {
         @Override
         public void mouseClicked(MouseEvent e) {
            super.mouseClicked(e);
            System.out.println("Mouse Clicked. (" + e.getX() + "," + e.getY() + ")");
         }
         @Override
         public void mouseWheelMoved(MouseWheelEvent e) {
            super.mouseWheelMoved(e);
            System.out.println("Mouse wheel moved. " + e.getScrollAmount());
         } 
         @Override
         public void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            System.out.println("Key pressed. " + e.getKeyCode());
         }
         @Override
         public void playing(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback started.");
         }
         @Override
         public void finished(MediaPlayer mediaPlayer) {
            super.playing(mediaPlayer);
            System.out.println("Media Playback finished.");
         }
         @Override
         public void error(MediaPlayer mediaPlayer) {
            SwingUtilities.invokeLater(new Runnable() {                
               public void run() {
                  System.out.println("Failed to load Media.");
               }                   
            });
         }
      };
      audioPlayerComponent = new AudioPlayerComponent();
      audioPlayerComponent.mediaPlayer().events().addMediaPlayerEventListener(new MediaPlayerEventAdapter() {
         @Override
         public void finished(MediaPlayer mediaPlayer) {
            System.out.println("Audio Playback Finished.");
         }
         @Override
         public void error(MediaPlayer mediaPlayer) {
            System.out.println("Failed to load Audio.");
         }
      });
   }
   public void initialize() {
      this.setBounds(100, 100, 600, 400);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      this.addWindowListener(new WindowAdapter() {
         @Override
         public 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() {
         public void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().play();
         }
      });  
      pauseButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      rewindButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(-14000);
         }
      });
      skipButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().controls().skipTime(4000);
         }
      });
      playAudioButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().play();
         }
      });
      pauseAudioButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            audioPlayerComponent.mediaPlayer().controls().pause();
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   public void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);       
   }
   public void loadAudio(String path) {
      audioPlayerComponent.mediaPlayer().media().startPaused(path);       
   }
   public static void main( String[] args ){
      try {            
         UIManager.setLookAndFeel(
         UIManager.getSystemLookAndFeelClassName());
      } 
      catch (Exception e) {
         System.out.println(e);
      }
      App application = new App(TITLE);
      application.initialize(); 
      application.setVisible(true);  
      application.loadVideo(VIDEO_PATH);
      application.loadAudio(AUDIO_PATH);
   }
}

通过右键单击文件来运行应用程序,然后选择以Java应用程序运行。成功启动后,如果一切正常,则应显示以下结果-

播放音讯

现在,按“音频控件”中的“播放”按钮,剪辑将开始播放。单击暂停按钮将暂停音频。