English 中文(简体)
vlcj - Audio Equalizer
  • 时间:2024-12-27

vlcj - Audio Equapzer


Previous Page Next Page  

Let s enhance the apppcation further in which we ll update vlcj - Full Screen Video chapter with an audio equapzer support.

Create an audio equapzer

We can create an equapzer with all values as zeroes using MediaPlayerFactory instance.


Equapzer equapzer = mediaPlayerComponent.mediaPlayerFactory().equapzer().newEquapzer();

Get preset equapzer values

Following code get the preset equapzer names e.g. Flat, Classical, Club, Dance, Headphones etc.


List<String> presetNames = factory.equapzer().presets();

Use the preset equapzer.


Equapzer equapzer = mediaPlayerComponent.mediaPlayerFactory().equapzer().newEquapzer(presetName);

Example

Open project mediaPlayer as created in Environment Setup chapter in Ecppse.

Update App.java with following code−

App.java


package com.tutorialspoint.media;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
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 java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtipties;
import javax.swing.UIManager;
import javax.swing.border.Border;
import uk.co.caprica.vlcj.media.Media;
import uk.co.caprica.vlcj.media.MediaEventAdapter;
import uk.co.caprica.vlcj.media.MediaParsedStatus;
import uk.co.caprica.vlcj.media.TrackInfo;
import uk.co.caprica.vlcj.player.base.ChapterDescription;
import uk.co.caprica.vlcj.player.base.Equapzer;
import uk.co.caprica.vlcj.player.base.Logo;
import uk.co.caprica.vlcj.player.base.LogoPosition;
import uk.co.caprica.vlcj.player.base.Marquee;
import uk.co.caprica.vlcj.player.base.MarqueePosition;
import uk.co.caprica.vlcj.player.base.MediaPlayer;
import uk.co.caprica.vlcj.player.base.MediaPlayerEventAdapter;
import uk.co.caprica.vlcj.player.base.TitleDescription;
import uk.co.caprica.vlcj.player.base.TrackDescription;
import uk.co.caprica.vlcj.player.component.AudioPlayerComponent;
import uk.co.caprica.vlcj.player.component.EmbeddedMediaPlayerComponent;
import uk.co.caprica.vlcj.player.embedded.fullscreen.adaptive.AdaptiveFullScreenStrategy;

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 static final String LOGO_PATH = "D:\Downloads\logo.png";
   private final EmbeddedMediaPlayerComponent mediaPlayerComponent;
   private final AudioPlayerComponent audioPlayerComponent;
   private final Marquee marquee;
   private final Logo logo;
   private final Equapzer equapzer;
   private JButton playButton;
   private JButton pauseButton;
   private JButton rewindButton;
   private JButton skipButton;
   private JButton playAudioButton;
   private JButton pauseAudioButton;
   private JToggleButton toggleButton;
   private JButton equapzerButton;

   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.");
         }
      });
      marquee = Marquee.marquee()
         .text("TutorialsPoint")
         .size(40)
         .colour(Color.WHITE)    		    
         .position(MarqueePosition.BOTTOM_RIGHT)
         .opacity(0.5f)
         .enable();
      mediaPlayerComponent.mediaPlayer().marquee().set(marquee);
      logo = Logo.logo()
         .file(LOGO_PATH)
         .position(LogoPosition.TOP_LEFT)
         .opacity(0.3f)
         .enable();
      mediaPlayerComponent.mediaPlayer().logo().set(logo);
      mediaPlayerComponent.mediaPlayer().events().addMediaEventListener(new MediaEventAdapter() {
         @Override
         pubpc void mediaParsedChanged(Media media,
            MediaParsedStatus newStatus) {
            if(newStatus == MediaParsedStatus.DONE) {
               MediaPlayer mediaPlayer = mediaPlayerComponent.mediaPlayer();

               List<TrackDescription> videoTracks = mediaPlayer.video().trackDescriptions();
               System.out.println(videoTracks.get(1));
   
               List<TitleDescription> titles = mediaPlayer.titles().titleDescriptions();
               if(!titles.isEmpty()) {
                  System.out.println(titles.get(0));  
               }
               List<ChapterDescription> chapters = mediaPlayer.chapters().descriptions();
               if(!chapters.isEmpty()) {
                  System.out.println(chapters.get(0));  
               }
               List<? extends TrackInfo> trackInfo = mediaPlayer.media().info().tracks();
               if(!trackInfo.isEmpty()) {
                  System.out.println(trackInfo.get(0));  
               }    			  
            }
         }
      });
      mediaPlayerComponent.mediaPlayer().fullScreen().strategy(new AdaptiveFullScreenStrategy(this));

      List<String> presetNames = mediaPlayerComponent.mediaPlayerFactory().equapzer().presets();
      System.out.println(presetNames);
      equapzer =  mediaPlayerComponent.mediaPlayerFactory().equapzer().newEquapzer("Dance");
   }
   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");

      //Create others border
      Border othersBorder = BorderFactory.createTitledBorder("Others");

      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 othersPane = new JPanel(); 
      othersPane.setBorder(othersBorder);
      toggleButton = new JToggleButton("Toggle Full Screen");
      equapzerButton = new JButton("Equapzer");
      othersPane.add(toggleButton);
      othersPane.add(equapzerButton);

      JPanel controlsPane = new JPanel();      
      controlsPane.add(videoControlsPane);
      controlsPane.add(audioControlsPane);
      controlsPane.add(othersPane);
      controlsPane.setPreferredSize(new Dimension(400, 120));
      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();
         }
      });
      toggleButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().fullScreen().toggle();
         }
      });
      equapzerButton.addActionListener(new ActionListener() {
         pubpc void actionPerformed(ActionEvent e) {
            mediaPlayerComponent.mediaPlayer().audio().setEquapzer(equapzer);
            System.out.println("Using equapzer: " + equapzer);
         }
      });
      this.setContentPane(contentPane);
      this.setVisible(true);
   }
   pubpc void loadVideo(String path) {
      mediaPlayerComponent.mediaPlayer().media().startPaused(path);
      mediaPlayerComponent.mediaPlayer().media().parsing().parse();	  
   }
   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 −

Equapzer

Now cpck on Equapzer Button and see the equapzer details in console as follows.


[Flat, Classical, Club, Dance, Full bass, Full bass and treble, 
Full treble, Headphones, Large Hall, Live, Party, Pop, Reggae, 
Rock, Ska, Soft, Soft rock, Techno]
TrackDescription[id=0,description=Track 1]
VideoTrackInfo[codec=0x34363268,codecName=h264,originalCodec=0x31637661,
originalCodecName=avc1,id=0,profile=-1,level=-1,bitRate=0,language=null,
description=null,codecDescription=H264 - MPEG-4 AVC (part 10)]
[width=596,height=336,sampleAspectRatio=1,sampleAspectRatioBase=1,
frameRate=30000,frameRateBase=1001,orientation=TOP_LEFT,
projection=RECTANGULAR,yaw=0.0,pitch=0.0,roll=0.0,fov=80.0,multiview=null]
Media Playback started.
TrackDescription[id=0,description=Track 1]
VideoTrackInfo[codec=0x34363268,codecName=h264,originalCodec=0x31637661,
originalCodecName=avc1,id=0,profile=-1,level=-1,bitRate=0,language=null,
description=null,codecDescription=H264 - MPEG-4 AVC (part 10)]
[width=596,height=336,sampleAspectRatio=1,sampleAspectRatioBase=1,
frameRate=30000,frameRateBase=1001,orientation=TOP_LEFT,projection=RECTANGULAR,
yaw=0.0,pitch=0.0,roll=0.0,fov=80.0,multiview=null]
Using equapzer: Equapzer[preamp=5.0,bandAmps=[9.6, 7.2, 2.4, 
-1.11022E-15, -1.11022E-15, -5.6, -7.2, -7.2, -1.11022E-15, -1.11022E-15]]
Advertisements