• java基于BasicPlayer调用 播放音乐


    无聊中想想用java调用下听音乐的api。晚上很多文章用的比较老大方法了,都是用原生的代码写,而且不支持mp3格式,BasicPlayer第三方包提供了很好的api调用,简单的3行代码就可以调用mp3了。如果喜欢自己动手的,可以去BasicPlayer官网下载源代码,里面包含所需要的jar包和测试类。

    先介紹下BasicPlayer。官方是这样介绍的:

    BasicPlayer层是jlGui的简单播放器API。这些类被设计用于需要简单功能(播放,停止,暂停,恢复,寻找)播放音频文件或流的任何应用程序。它是JavaSound API的高级API。

    官网地址:http://www.javazoom.net/jlgui/api.html 

    官方源代码中包含包有:

    1:basicplayer3.0.jar

    2:commons-logging-api.jar

    3:jl1.0.jar

    4:jogg-0.0.7.jar

    5:jorbis-0.0.15.jar

    6:jspeex0.9.7.jar

    7:mp3spi1.9.4.jar (注意!这个版本如果出现了java.io.IOException: Resetting to invalid mark       这个错误,需要把版本更新到mp3spi1.9.5.jar就解决了)

    8:tritonus_share.jar

    vorbisspi1.0.2.jar

    在测试前要确保网卡和音响或耳机正常,不然会提示一些莫名的错误哦。

    下面贴出我测试成功的代码。如下

      1 package fff;
      2 
      3 /*
      4  * BasicPlayerTest.
      5  * 
      6  * JavaZOOM : jlgui@javazoom.net
      7  *            http://www.javazoom.net
      8  *
      9  *-----------------------------------------------------------------------
     10  *   This program is free software; you can redistribute it and/or modify
     11  *   it under the terms of the GNU Library General Public License as published
     12  *   by the Free Software Foundation; either version 2 of the License, or
     13  *   (at your option) any later version.
     14  *
     15  *   This program is distributed in the hope that it will be useful,
     16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18  *   GNU Library General Public License for more details.
     19  *
     20  *   You should have received a copy of the GNU Library General Public
     21  *   License along with this program; if not, write to the Free Software
     22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
     23  *----------------------------------------------------------------------
     24  */
     25 
     26 import java.io.File;
     27 import java.io.PrintStream;
     28 import java.util.Map;
     29 
     30 import javazoom.jlgui.basicplayer.BasicController;
     31 import javazoom.jlgui.basicplayer.BasicPlayer;
     32 import javazoom.jlgui.basicplayer.BasicPlayerEvent;
     33 import javazoom.jlgui.basicplayer.BasicPlayerException;
     34 import javazoom.jlgui.basicplayer.BasicPlayerListener;
     35 
     36 /**
     37  * This class implements a simple player based on BasicPlayer. BasicPlayer is a
     38  * threaded class providing most features of a music player. BasicPlayer works
     39  * with underlying JavaSound SPIs to support multiple audio formats. Basically
     40  * JavaSound supports WAV, AU, AIFF audio formats. Add MP3 SPI (from JavaZOOM)
     41  * and Vorbis SPI( from JavaZOOM) in your CLASSPATH to play MP3 and Ogg Vorbis
     42  * file.
     43  */
     44 public class BasicPlayerTest implements BasicPlayerListener {
     45     private PrintStream out = null;
     46 
     47     /**
     48      * Entry point.
     49      * 
     50      * @param args
     51      *            filename to play.
     52      * @throws Exception 
     53      */
     54     public static void main(String[] args) throws Exception {
     55         BasicPlayerTest test = new BasicPlayerTest();
     56         test.play("D:/xxx.mp3");
     57 
     58 //        //简单测试代码
     59 //        BasicPlayer player = new BasicPlayer();
     60 //        player.open(new File("D:/xxx.mp3"));
     61 //        player.play();
     62         
     63     }
     64 
     65     /**
     66      * Contructor.
     67      */
     68     public BasicPlayerTest() {
     69         out = System.out;
     70     }
     71 
     72     public void play(String filename) {
     73         // Instantiate BasicPlayer.
     74         BasicPlayer player = new BasicPlayer();
     75         // BasicPlayer is a BasicController.
     76         BasicController control = (BasicController) player;
     77         // Register BasicPlayerTest to BasicPlayerListener events.
     78         // It means that this object will be notified on BasicPlayer
     79         // events such as : opened(...), progress(...), stateUpdated(...)
     80         player.addBasicPlayerListener(this);
     81 
     82         try {
     83             // Open file, or URL or Stream (shoutcast) to play.
     84             control.open(new File(filename));
     85             // control.open(new URL("http://yourshoutcastserver.com:8000"));
     86 
     87             // Start playback in a thread.
     88             control.play();
     89 
     90             // Set Volume (0 to 1.0).
     91             // setGain should be called after control.play().
     92             control.setGain(0.85);
     93 
     94             // Set Pan (-1.0 to 1.0).
     95             // setPan should be called after control.play().
     96             control.setPan(0.0);
     97 
     98             // If you want to pause/resume/pause the played file then
     99             // write a Swing player and just call control.pause(),
    100             // control.resume() or control.stop().
    101             // Use control.seek(bytesToSkip) to seek file
    102             // (i.e. fast forward and rewind). seek feature will
    103             // work only if underlying JavaSound SPI implements
    104             // skip(...). True for MP3SPI (JavaZOOM) and SUN SPI's
    105             // (WAVE, AU, AIFF).
    106 
    107         } catch (BasicPlayerException e) {
    108             e.printStackTrace();
    109         }
    110     }
    111 
    112     /**
    113      * Open callback, stream is ready to play.
    114      * 
    115      * properties map includes audio format dependant features such as bitrate,
    116      * duration, frequency, channels, number of frames, vbr flag, id3v2/id3v1
    117      * (for MP3 only), comments (for Ogg Vorbis), ...
    118      * 
    119      * @param stream
    120      *            could be File, URL or InputStream
    121      * @param properties
    122      *            audio stream properties.
    123      */
    124     public void opened(Object stream, Map properties) {
    125         // Pay attention to properties. It's useful to get duration,
    126         // bitrate, channels, even tag such as ID3v2.
    127         display("opened : " + properties.toString());
    128     }
    129 
    130     /**
    131      * Progress callback while playing.
    132      * 
    133      * This method is called severals time per seconds while playing. properties
    134      * map includes audio format features such as instant bitrate, microseconds
    135      * position, current frame number, ...
    136      * 
    137      * @param bytesread
    138      *            from encoded stream.
    139      * @param microseconds
    140      *            elapsed (<b>reseted after a seek !</b>).
    141      * @param pcmdata
    142      *            PCM samples.
    143      * @param properties
    144      *            audio stream parameters.
    145      */
    146     public void progress(int bytesread, long microseconds, byte[] pcmdata,
    147             Map properties) {
    148         // Pay attention to properties. It depends on underlying JavaSound SPI
    149         // MP3SPI provides mp3.equalizer.
    150         display("progress : " + properties.toString());
    151     }
    152 
    153     /**
    154      * Notification callback for basicplayer events such as opened, eom ...
    155      * 
    156      * @param event
    157      */
    158     public void stateUpdated(BasicPlayerEvent event) {
    159         // Notification of BasicPlayer states (opened, playing, end of media,
    160         // ...)
    161         display("stateUpdated : " + event.toString());
    162         if (event.getCode() == BasicPlayerEvent.STOPPED) {
    163             System.exit(0);
    164         }
    165     }
    166 
    167     /**
    168      * A handle to the BasicPlayer, plugins may control the player through the
    169      * controller (play, stop, ...)
    170      * 
    171      * @param controller
    172      *            : a handle to the player
    173      */
    174     public void setController(BasicController controller) {
    175         display("setController : " + controller);
    176     }
    177 
    178     public void display(String msg) {
    179         if (out != null)
    180             out.println(msg);
    181     }
    182 
    183 }
  • 相关阅读:
    2020年12月18日
    2020年12月17日
    2020年12月16日
    2020年12月15日
    2020年12月14日
    排序算法总结
    2020微软笔试题
    子串、子序列问题
    剑指offer题解(完结)
    ONOS 2.2安装
  • 原文地址:https://www.cnblogs.com/wulm/p/6930807.html
Copyright © 2020-2023  润新知