Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.39 KB | None | 0 0
  1. package jaco.mp3.player;
  2.  
  3. import jaco.mp3.player.MP3PlayerTheme;
  4. import jaco.mp3.player.MP3PlayerThemeDefault;
  5. import jaco.mp3.resources.Decoder;
  6. import jaco.mp3.resources.Frame;
  7. import jaco.mp3.resources.Obuffer;
  8. import jaco.mp3.resources.SampleBuffer;
  9. import jaco.mp3.resources.SoundStream;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.io.ObjectInputStream;
  15. import java.io.PrintStream;
  16. import java.net.URL;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. import java.util.Random;
  20. import java.util.logging.Level;
  21. import java.util.logging.Logger;
  22. import javax.sound.sampled.AudioFormat;
  23. import javax.sound.sampled.AudioSystem;
  24. import javax.sound.sampled.BooleanControl;
  25. import javax.sound.sampled.Control;
  26. import javax.sound.sampled.DataLine;
  27. import javax.sound.sampled.FloatControl;
  28. import javax.sound.sampled.Line;
  29. import javax.sound.sampled.SourceDataLine;
  30. import javax.swing.JPanel;
  31.  
  32. public class MP3Player
  33. extends JPanel {
  34. private static final long serialVersionUID = 1L;
  35. private static final Logger LOGGER = Logger.getLogger(MP3Player.class.getName());
  36. private static final Random RANDOM = new Random();
  37. private List<Object> playlist = new ArrayList<Object>();
  38. private transient boolean isPaused = false;
  39. private transient boolean isStopped = true;
  40. private volatile transient int volume = 50;
  41. private volatile transient boolean shuffle = false;
  42. private volatile transient boolean repeat = true;
  43. private volatile transient Thread playingThread = null;
  44. private volatile transient int playingIndex = 0;
  45. private volatile transient SourceDataLine playingSource = null;
  46. private volatile transient int playingSourceVolume = 0;
  47.  
  48. public MP3Player() {
  49. this.init();
  50. }
  51.  
  52. public MP3Player(File file) {
  53. this.add(file);
  54. this.init();
  55. }
  56.  
  57. public MP3Player(File ... files) {
  58. for (File file : files) {
  59. this.add(file);
  60. }
  61. this.init();
  62. }
  63.  
  64. public MP3Player(URL url) {
  65. this.add(url);
  66. this.init();
  67. }
  68.  
  69. public MP3Player(URL ... urls) {
  70. for (URL url : urls) {
  71. this.add(url);
  72. }
  73. this.init();
  74. }
  75.  
  76. private void init() {
  77. new MP3PlayerThemeDefault().apply(this);
  78. }
  79.  
  80. /*
  81. * WARNING - Removed try catching itself - possible behaviour change.
  82. */
  83. public MP3Player add(File file, boolean recursively) {
  84. if (file.isFile()) {
  85. if (file.getName().endsWith(".mp3")) {
  86. MP3Player mP3Player = this;
  87. synchronized (mP3Player) {
  88. this.playlist.add(file);
  89. }
  90. }
  91. } else if (file.isDirectory()) {
  92. File[] files;
  93. for (File file2 : files = file.listFiles()) {
  94. if (!file2.isFile() && !recursively) continue;
  95. this.add(file2, recursively);
  96. }
  97. } else {
  98. throw new IllegalArgumentException("WTF is this? ( " + file + " )");
  99. }
  100. return this;
  101. }
  102.  
  103. public MP3Player add(File file) {
  104. this.add(file, true);
  105. return this;
  106. }
  107.  
  108. /*
  109. * WARNING - Removed try catching itself - possible behaviour change.
  110. */
  111. public MP3Player add(URL url) {
  112. MP3Player mP3Player = this;
  113. synchronized (mP3Player) {
  114. this.playlist.add(url);
  115. }
  116. return this;
  117. }
  118.  
  119. public int getPlaylist() {
  120. return this.playingIndex;
  121. }
  122.  
  123. public void setTheme(MP3PlayerTheme theme) {
  124. this.removeAll();
  125. theme.apply(this);
  126. this.revalidate();
  127. this.repaint();
  128. }
  129.  
  130. /*
  131. * WARNING - Removed try catching itself - possible behaviour change.
  132. */
  133. public void play() {
  134. System.out.println("MP3Player Made by Nero");
  135. MP3Player mP3Player = this;
  136. synchronized (mP3Player) {
  137. if (this.isPaused) {
  138. this.isPaused = false;
  139. this.notifyAll();
  140. return;
  141. }
  142. }
  143. this.stop();
  144. if (this.playlist.size() == 0) {
  145. return;
  146. }
  147. mP3Player = this;
  148. synchronized (mP3Player) {
  149. this.isStopped = false;
  150. }
  151. if (this.playingThread == null) {
  152. this.playingThread = new Thread(){
  153.  
  154. /*
  155. * WARNING - Removed try catching itself - possible behaviour change.
  156. */
  157. @Override
  158. public void run() {
  159. Object soundStream;
  160. boolean skipForwardAllowed;
  161. block51 : {
  162. InputStream inputStream = null;
  163. try {
  164. try {
  165. Object playlistObject;
  166. MP3Player mP3Player = MP3Player.this;
  167. synchronized (mP3Player) {
  168. playlistObject = MP3Player.this.playlist.get(MP3Player.this.playingIndex);
  169. }
  170. if (playlistObject instanceof File) {
  171. inputStream = new FileInputStream((File)playlistObject);
  172. } else if (playlistObject instanceof URL) {
  173. inputStream = ((URL)playlistObject).openStream();
  174. } else {
  175. throw new IOException("this is impossible; how come the play list contains this kind of object? :: " + playlistObject.getClass());
  176. }
  177. soundStream = new SoundStream(inputStream);
  178. Decoder decoder = new Decoder();
  179. do {
  180. MP3Player mP3Player2 = MP3Player.this;
  181. synchronized (mP3Player2) {
  182. if (MP3Player.this.isStopped) {
  183. break;
  184. }
  185. if (MP3Player.this.isPaused) {
  186. if (MP3Player.this.playingSource != null) {
  187. MP3Player.this.playingSource.flush();
  188. }
  189. MP3Player.access$7(MP3Player.this, MP3Player.this.volume);
  190. try {
  191. MP3Player.this.wait();
  192. }
  193. catch (InterruptedException e2) {
  194. LOGGER.log(Level.SEVERE, "wait() failed", e2);
  195. }
  196. continue;
  197. }
  198. }
  199. try {
  200. Frame frame = ((SoundStream)soundStream).readFrame();
  201. if (frame == null) break;
  202. if (MP3Player.this.playingSource == null) {
  203. int frequency = frame.frequency();
  204. int channels = frame.mode() == 3 ? 1 : 2;
  205. AudioFormat format = new AudioFormat(frequency, 16, channels, true, false);
  206. Line line = AudioSystem.getLine(new DataLine.Info(SourceDataLine.class, format));
  207. MP3Player.access$8(MP3Player.this, (SourceDataLine)line);
  208. MP3Player.this.playingSource.open(format);
  209. MP3Player.this.playingSource.start();
  210. MP3Player.access$7(MP3Player.this, 0);
  211. MP3Player.this.setVolume(MP3Player.this.playingSource, 0);
  212. }
  213. SampleBuffer output = (SampleBuffer)decoder.decodeFrame(frame, (SoundStream)soundStream);
  214. short[] buffer = output.getBuffer();
  215. int offs = 0;
  216. int len = output.getBufferLength();
  217. if (MP3Player.this.playingSourceVolume != MP3Player.this.volume) {
  218. if (MP3Player.this.playingSourceVolume > MP3Player.this.volume) {
  219. MP3Player mP3Player3 = MP3Player.this;
  220. MP3Player.access$7(mP3Player3, mP3Player3.playingSourceVolume - 10);
  221. if (MP3Player.this.playingSourceVolume < MP3Player.this.volume) {
  222. MP3Player.access$7(MP3Player.this, MP3Player.this.volume);
  223. }
  224. } else {
  225. MP3Player mP3Player4 = MP3Player.this;
  226. MP3Player.access$7(mP3Player4, mP3Player4.playingSourceVolume + 10);
  227. if (MP3Player.this.playingSourceVolume > MP3Player.this.volume) {
  228. MP3Player.access$7(MP3Player.this, MP3Player.this.volume);
  229. }
  230. }
  231. MP3Player.this.setVolume(MP3Player.this.playingSource, MP3Player.this.playingSourceVolume);
  232. }
  233. MP3Player.this.playingSource.write(MP3Player.this.toByteArray(buffer, offs, len), 0, len * 2);
  234. ((SoundStream)soundStream).closeFrame();
  235. }
  236. catch (Exception e3) {
  237. LOGGER.log(Level.WARNING, "unexpected problems while playing " + this.toString(), e3);
  238. break;
  239. }
  240. } while (true);
  241. if (MP3Player.this.playingSource == null) {
  242. LOGGER.log(Level.INFO, "source is null because first frame is null, so probably the file is not a mp3");
  243. } else {
  244. MP3Player e3 = MP3Player.this;
  245. synchronized (e3) {
  246. if (!MP3Player.this.isStopped) {
  247. MP3Player.this.playingSource.drain();
  248. } else {
  249. MP3Player.this.playingSource.flush();
  250. }
  251. }
  252. MP3Player.this.playingSource.stop();
  253. MP3Player.this.playingSource.close();
  254. MP3Player.access$8(MP3Player.this, null);
  255. }
  256. try {
  257. ((SoundStream)soundStream).close();
  258. }
  259. catch (Exception e4) {
  260. LOGGER.log(Level.WARNING, "error closing the sound stream", e4);
  261. }
  262. }
  263. catch (IOException e5) {
  264. LOGGER.log(Level.SEVERE, "unable to open the input stream", e5);
  265. if (inputStream != null) {
  266. try {
  267. inputStream.close();
  268. }
  269. catch (Exception e6) {
  270. LOGGER.log(Level.WARNING, "error closing the input stream", e6);
  271. }
  272. }
  273. break block51;
  274. }
  275. }
  276. catch (Throwable throwable) {
  277. if (inputStream != null) {
  278. try {
  279. inputStream.close();
  280. }
  281. catch (Exception e7) {
  282. LOGGER.log(Level.WARNING, "error closing the input stream", e7);
  283. }
  284. }
  285. throw throwable;
  286. }
  287. if (inputStream != null) {
  288. try {
  289. inputStream.close();
  290. }
  291. catch (Exception e8) {
  292. LOGGER.log(Level.WARNING, "error closing the input stream", e8);
  293. }
  294. }
  295. }
  296. soundStream = MP3Player.this;
  297. synchronized (soundStream) {
  298. skipForwardAllowed = !MP3Player.this.isStopped;
  299. MP3Player.access$12(MP3Player.this, false);
  300. MP3Player.access$13(MP3Player.this, true);
  301. }
  302. MP3Player.access$14(MP3Player.this, null);
  303. if (skipForwardAllowed) {
  304. MP3Player.this.skipForward();
  305. }
  306. }
  307. };
  308. this.playingThread.start();
  309. }
  310. }
  311.  
  312. /*
  313. * WARNING - Removed try catching itself - possible behaviour change.
  314. */
  315. public boolean isPlaying() {
  316. MP3Player mP3Player = this;
  317. synchronized (mP3Player) {
  318. return !this.isPaused && !this.isStopped;
  319. }
  320. }
  321.  
  322. /*
  323. * WARNING - Removed try catching itself - possible behaviour change.
  324. */
  325. public void pause() {
  326. if (!this.isPlaying()) {
  327. return;
  328. }
  329. MP3Player mP3Player = this;
  330. synchronized (mP3Player) {
  331. this.isPaused = true;
  332. this.notifyAll();
  333. }
  334. }
  335.  
  336. /*
  337. * WARNING - Removed try catching itself - possible behaviour change.
  338. */
  339. public boolean isPaused() {
  340. MP3Player mP3Player = this;
  341. synchronized (mP3Player) {
  342. return this.isPaused;
  343. }
  344. }
  345.  
  346. /*
  347. * WARNING - Removed try catching itself - possible behaviour change.
  348. */
  349. public void stop() {
  350. MP3Player mP3Player = this;
  351. synchronized (mP3Player) {
  352. this.isPaused = false;
  353. this.isStopped = true;
  354. this.notifyAll();
  355. }
  356. if (this.playingThread != null) {
  357. try {
  358. this.playingThread.join();
  359. }
  360. catch (InterruptedException e2) {
  361. LOGGER.log(Level.SEVERE, "join() failed", e2);
  362. }
  363. }
  364. }
  365.  
  366. /*
  367. * WARNING - Removed try catching itself - possible behaviour change.
  368. */
  369. public boolean isStopped() {
  370. MP3Player mP3Player = this;
  371. synchronized (mP3Player) {
  372. return this.isStopped;
  373. }
  374. }
  375.  
  376. public void skipForward() {
  377. this.skip(1);
  378. }
  379.  
  380. public void skipBackward() {
  381. this.skip(-1);
  382. }
  383.  
  384. private void skip(int items) {
  385. if (this.playlist.size() == 0) {
  386. return;
  387. }
  388. boolean playAllowed = this.isPlaying();
  389. if (this.shuffle) {
  390. this.playingIndex = RANDOM.nextInt(this.playlist.size());
  391. } else {
  392. this.playingIndex += items;
  393. if (this.playingIndex > this.playlist.size() - 1) {
  394. if (this.repeat) {
  395. this.playingIndex = 0;
  396. } else {
  397. this.playingIndex = this.playlist.size() - 1;
  398. playAllowed = false;
  399. }
  400. } else if (this.playingIndex < 0) {
  401. if (this.repeat) {
  402. this.playingIndex = this.playlist.size() - 1;
  403. } else {
  404. this.playingIndex = 0;
  405. playAllowed = false;
  406. }
  407. }
  408. }
  409. this.stop();
  410. if (playAllowed) {
  411. this.play();
  412. }
  413. }
  414.  
  415. public MP3Player setVolume(int volume) {
  416. if (volume < 0 || volume > 100) {
  417. throw new IllegalArgumentException("Wrong value for volume, must be in interval [0..100].");
  418. }
  419. this.volume = volume;
  420. return this;
  421. }
  422.  
  423. public MP3Player setVolume(float volume) {
  424. if (volume < 0.0f || volume > 100.0f) {
  425. throw new IllegalArgumentException("Wrong value for volume, must be in interval [0..100].");
  426. }
  427. this.volume = (int)volume;
  428. return this;
  429. }
  430.  
  431. public int getVolume() {
  432. return this.volume;
  433. }
  434.  
  435. public MP3Player setShuffle(boolean shuffle) {
  436. this.shuffle = shuffle;
  437. return this;
  438. }
  439.  
  440. public boolean isShuffle() {
  441. return this.shuffle;
  442. }
  443.  
  444. public MP3Player setRepeat(boolean repeat) {
  445. this.repeat = repeat;
  446. return this;
  447. }
  448.  
  449. public boolean isRepeat() {
  450. return this.repeat;
  451. }
  452.  
  453. private void setVolume(SourceDataLine source, int volume) {
  454. try {
  455. FloatControl gainControl = (FloatControl)source.getControl(FloatControl.Type.MASTER_GAIN);
  456. BooleanControl muteControl = (BooleanControl)source.getControl(BooleanControl.Type.MUTE);
  457. if (volume == 0) {
  458. muteControl.setValue(true);
  459. } else {
  460. muteControl.setValue(false);
  461. gainControl.setValue((float)(Math.log((double)volume / 100.0) / Math.log(10.0) * 20.0));
  462. }
  463. }
  464. catch (Exception e2) {
  465. LOGGER.log(Level.WARNING, "unable to set the volume to the provided source", e2);
  466. }
  467. }
  468.  
  469. private void setVolume(SourceDataLine source, float volume) {
  470. try {
  471. FloatControl gainControl = (FloatControl)source.getControl(FloatControl.Type.MASTER_GAIN);
  472. BooleanControl muteControl = (BooleanControl)source.getControl(BooleanControl.Type.MUTE);
  473. if (volume == 0.0f) {
  474. muteControl.setValue(true);
  475. } else {
  476. muteControl.setValue(false);
  477. gainControl.setValue((float)(Math.log((double)volume / 100.0) / Math.log(10.0) * 20.0));
  478. }
  479. }
  480. catch (Exception e2) {
  481. LOGGER.log(Level.WARNING, "unable to set the volume to the provided source", e2);
  482. }
  483. }
  484.  
  485. private void setVolume(SourceDataLine source, Double volume) {
  486. try {
  487. FloatControl gainControl = (FloatControl)source.getControl(FloatControl.Type.MASTER_GAIN);
  488. BooleanControl muteControl = (BooleanControl)source.getControl(BooleanControl.Type.MUTE);
  489. if (volume == 0.0) {
  490. muteControl.setValue(true);
  491. } else {
  492. muteControl.setValue(false);
  493. gainControl.setValue((float)(Math.log(volume / 100.0) / Math.log(10.0) * 20.0));
  494. }
  495. }
  496. catch (Exception e2) {
  497. LOGGER.log(Level.WARNING, "unable to set the volume to the provided source", e2);
  498. }
  499. }
  500.  
  501. public int getPosition() {
  502. int pos = 0;
  503. if (this.playingSource != null) {
  504. pos = (int)(this.playingSource.getMicrosecondPosition() / 1000L);
  505. }
  506. return pos;
  507. }
  508.  
  509. private byte[] toByteArray(short[] ss2, int offs, int len) {
  510. byte[] bb2 = new byte[len * 2];
  511. int idx = 0;
  512. while (len-- > 0) {
  513. short s2 = ss2[offs++];
  514. bb2[idx++] = (byte)s2;
  515. bb2[idx++] = (byte)(s2 >>> 8);
  516. }
  517. return bb2;
  518. }
  519.  
  520. private void readObject(ObjectInputStream objectInputStream) throws ClassNotFoundException, IOException {
  521. objectInputStream.defaultReadObject();
  522. }
  523.  
  524. static /* synthetic */ void access$7(MP3Player mP3Player, int n2) {
  525. mP3Player.playingSourceVolume = n2;
  526. }
  527.  
  528. static /* synthetic */ void access$8(MP3Player mP3Player, SourceDataLine sourceDataLine) {
  529. mP3Player.playingSource = sourceDataLine;
  530. }
  531.  
  532. static /* synthetic */ void access$12(MP3Player mP3Player, boolean bl2) {
  533. mP3Player.isPaused = bl2;
  534. }
  535.  
  536. static /* synthetic */ void access$13(MP3Player mP3Player, boolean bl2) {
  537. mP3Player.isStopped = bl2;
  538. }
  539.  
  540. static /* synthetic */ void access$14(MP3Player mP3Player, Thread thread) {
  541. mP3Player.playingThread = thread;
  542. }
  543.  
  544. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement