TerrificTable55

Untitled

Dec 16th, 2022
901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. ```java
  2. package main.java.xyz.terrific.mp3_player;
  3.  
  4. import javazoom.jl.decoder.*;
  5. import javazoom.jl.player.AudioDevice;
  6.  
  7. import java.io.InputStream;
  8.  
  9. public class Player {
  10.     private int frame;
  11.     private Bitstream bitstream;
  12.     private Decoder decoder;
  13.     private JavaSoundAudioDevice audio;
  14.     private boolean closed;
  15.     private boolean complete;
  16.     private int lastPosition;
  17.  
  18.     public Player(InputStream stream) throws JavaLayerException {
  19.         this(stream, (JavaSoundAudioDevice)null);
  20.     }
  21.  
  22.     public Player(InputStream stream, JavaSoundAudioDevice device) throws JavaLayerException {
  23.         this.frame = 0;
  24.         this.closed = false;
  25.         this.complete = false;
  26.         this.lastPosition = 0;
  27.         this.bitstream = new Bitstream(stream);
  28.         this.decoder = new Decoder();
  29.         if (device != null) {
  30.             this.audio = device;
  31.         } else {
  32.             this.audio = new JavaSoundAudioDeviceFactory().createAudioDevice();
  33.         }
  34.  
  35.         this.audio.open(this.decoder);
  36.     }
  37.  
  38.     public void play() throws JavaLayerException {
  39.         this.play(Integer.MAX_VALUE);
  40.     }
  41.  
  42.     public boolean play(int frames) throws JavaLayerException {
  43.         boolean ret;
  44.         for(ret = true; frames-- > 0 && ret; ret = this.decodeFrame()) {
  45.         }
  46.  
  47.         if (!ret) {
  48.             AudioDevice out = this.audio;
  49.             if (out != null) {
  50.                 out.flush();
  51.                 synchronized(this) {
  52.                     this.complete = !this.closed;
  53.                     this.close();
  54.                 }
  55.             }
  56.         }
  57.  
  58.         return ret;
  59.     }
  60.  
  61.     public synchronized void close() {
  62.         AudioDevice out = this.audio;
  63.         if (out != null) {
  64.             this.closed = true;
  65.             this.audio = null;
  66.             out.close();
  67.             this.lastPosition = out.getPosition();
  68.  
  69.             try {
  70.                 this.bitstream.close();
  71.             } catch (BitstreamException var3) {
  72.             }
  73.         }
  74.  
  75.     }
  76.  
  77.     public synchronized boolean isComplete() {
  78.         return this.complete;
  79.     }
  80.  
  81.     public int getPosition() {
  82.         int position = this.lastPosition;
  83.         AudioDevice out = this.audio;
  84.         if (out != null) {
  85.             position = out.getPosition();
  86.         }
  87.  
  88.         return position;
  89.     }
  90.  
  91.     public boolean setGain(float newGain) {
  92.         JavaSoundAudioDevice jsAudio = (JavaSoundAudioDevice) audio;
  93.         try {
  94.             jsAudio.write(null, 0, 0);
  95.         } catch (JavaLayerException ex) {
  96.             ex.printStackTrace();
  97.         }
  98.         return jsAudio.setLineGain(newGain);
  99.     }
  100.  
  101.     protected boolean decodeFrame() throws JavaLayerException {
  102.         try {
  103.             AudioDevice out = this.audio;
  104.             if (out == null) {
  105.                 return false;
  106.             } else {
  107.                 Header h = this.bitstream.readFrame();
  108.                 if (h == null) {
  109.                     return false;
  110.                 } else {
  111.                     SampleBuffer output = (SampleBuffer)this.decoder.decodeFrame(h, this.bitstream);
  112.                     synchronized(this) {
  113.                         out = this.audio;
  114.                         if (out != null) {
  115.                             out.write(output.getBuffer(), 0, output.getBufferLength());
  116.                             // by dividing bufferlength playback speed can be increased, causes audio fragments tho
  117.                         }
  118.                     }
  119.  
  120.                     this.bitstream.closeFrame();
  121.                     return true;
  122.                 }
  123.             }
  124.         } catch (RuntimeException var7) {
  125.             throw new JavaLayerException("Exception decoding audio frame", var7);
  126.         }
  127.     }
  128. }
  129. ```
Advertisement
Add Comment
Please, Sign In to add comment