無聊中想一想用java調用下聽音樂的api。晚上不少文章用的比較老大方法了,都是用原生的代碼寫,並且不支持mp3格式,BasicPlayer第三方包提供了很好的api調用,簡單的3行代碼就能夠調用mp3了。若是喜歡本身動手的,能夠去BasicPlayer官網下載源代碼,裏面包含所須要的jar包和測試類。html
先介紹下BasicPlayer。官方是這樣介紹的:java
BasicPlayer層是jlGui的簡單播放器API。這些類被設計用於須要簡單功能(播放,中止,暫停,恢復,尋找)播放音頻文件或流的任何應用程序。它是JavaSound API的高級API。redis
官網地址:http://www.javazoom.net/jlgui/api.html api
官方源代碼中包含包有:jsp
1:basicplayer3.0.jaride
2:commons-logging-api.jar測試
3:jl1.0.jarui
4:jogg-0.0.7.jarthis
5:jorbis-0.0.15.jarspa
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 }