Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (517)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (578)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Ultroman (posted 2012-04-19 13:27:24, viewed 118 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177  
private URL url = null, nextUrl = null;
   private AudioInputStream stream;
   private AudioInputStream decodedStream;
   private AudioFormat format;
   private AudioFormat decodedFormat;
   private volatile boolean stop;
   private boolean running, looping, nextIsLooping, ready;
   String filename = "";
   SourceDataLine line = null;

   public OggStreamer() {
      this.stop = true;
      this.running = true;
      this.url = null;
      this.ready = true;
   }

   public void run() {
      while(running){
         // If the line and all streams have been reset,
         // we are ready to play a new file.
         if(!ready && stream==null && decodedStream==null && line==null){
            System.out.println("Streamer is ready...");
            ready = true;
         }
         
         // If the streamer is stopped, ready, and it has a file queued to play,
         // we set the loop-variables to the ones for the queued file, and start playing.
         if(ready && stop && this.nextUrl!=null){
            stop = false;
            this.looping = this.nextIsLooping;
            this.url = this.nextUrl;
            this.nextUrl = null;
            System.out.println("Streamer is running...");
            
            // Debugging SOPs to tell if we're playing a looping song or not.
            if(looping)System.out.println("Looping song: "+this.url.getFile().substring(this.url.getFile().lastIndexOf("/")+1));
            else System.out.println("Playing song: "+this.url.getFile().substring(this.url.getFile().lastIndexOf("/")+1));
               
            try {
               // Get AudioInputStream from given file.
               this.stream = AudioSystem.getAudioInputStream(this.url);
               this.decodedStream = null;
               if (this.stream != null) {
                  this.format = this.stream.getFormat();
                  this.decodedFormat = new AudioFormat(
                        AudioFormat.Encoding.PCM_SIGNED,
                        this.format.getSampleRate(), 16,
                        this.format.getChannels(),
                        this.format.getChannels() * 2,
                        this.format.getSampleRate(), false);
                  // Get AudioInputStream that will be decoded by underlying VorbisSPI
                  this.decodedStream = AudioSystem.getAudioInputStream(
                        this.decodedFormat, this.stream);
               }else{
                  System.out.println("Stream is null");
               }
            } catch (Exception e) {
               System.out.println("Could not get or decode audiostream");
            }
            
            line = null;
            // Get a line-object with the correct format, and set the volume
            try {
               line = this.getSourceDataLine(this.decodedFormat);
               FloatControl volume = (FloatControl)line.getControl(FloatControl.Type.MASTER_GAIN);
               volume.setValue(1);
            } catch (LineUnavailableException lue) {
               System.out.println("Line is unavailable!");
            }
            
            // If we were successful getting a line-object, we start playing from the stream.
            if (line != null) {
               try {
                  byte[] data = new byte[4096];
                  // Start
                  line.start();
                  int nBytesRead = 0;
   
                  // While there is music to be played, and we haven't been ordered to stop,
                  // we keep feeding stream-data into the line.
                  while (nBytesRead != -1 && !stop) {
                     nBytesRead = this.decodedStream.read(data, 0, data.length);
                     if (nBytesRead != -1) {
                        line.write(data, 0, nBytesRead);
                     }
                  }
                  
                  // We've stopped prematurely or the music-stream is empty, so we set the ready-flag
                  // to false. When line and all streams are shut down, the main while-loop
                  // sets the ready-flag to true again, indicating we're ready for a new file.
                  ready=false;
                  
                  // If we haven't been ordered to stop prematurely, and the song we're playing
                  // is supposed to loop, we set the nextUrl to the current one.
                  // Then when the streamer is ready again, it will play the same song again.
                  if(!stop && looping) this.nextUrl = this.url;
                  
                  // We tell the streamer that playback, for whatever reason, has ended.
                  stop = true;
                  
                  // clean-up
                  line.stop();
                  line.close();
                  line=null;
                  if(this.decodedStream!=null) this.decodedStream.close();
                  if(this.stream!=null) this.stream.close();
                  stream=null;
                  decodedStream=null;
               } catch (IOException io) {
                  System.out.println("Line cannot start!");
               }
            }
         }
      }
      
      // clean-up before thread is shut down
      if(line.isRunning()){
         line.stop();
      }
      if(line.isOpen())
         line.close();
      line=null;
      try {
         this.decodedStream.close();
         this.stream.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
      this.decodedStream = null;
      this.stream = null;
      this.looping = false;
      this.url = null;
   }

   public void startMusic(String filenameString) {
      // If the streamer isn't stopped already, we make it stop.
      if(!stop)stop();
      
      // We set the new filename to be the new filenameString.
      // Only for debug-printing purposes.
      this.filename = filenameString;
      System.out.println("Starting song with: "+filenameString);
      
      // Put the new url into the queueing-variable, and tell it not to loop. 
      this.nextUrl = this.getClass().getResource(filenameString);
      this.nextIsLooping = false;
   }

   public void startLoop(String filenameString) {
      // If the streamer isn't stopped already, we make it stop.
      if(!stop)stop();
      
      // We set the new filename to be the new filenameString.
      // Only for debug-printing purposes.
      this.filename = filenameString;
      System.out.println("Starting loop with: "+filenameString);
      
      // Put the new url into the queueing-variable, and tell it to loop.
      this.nextUrl = this.getClass().getResource(filenameString);
      this.nextIsLooping = true;
   }

   public void stop() {
      System.out.println("Stopping streamer");
      this.stop = true;
   }
   
   private SourceDataLine getSourceDataLine(AudioFormat audioFormat)
         throws LineUnavailableException {
      SourceDataLine res = null;
      DataLine.Info info = new DataLine.Info(SourceDataLine.class,
            audioFormat);
      res = (SourceDataLine) AudioSystem.getLine(info);
      res.open(audioFormat);
      return res;
   }





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

DarkCart (12 views)
2014-10-31 21:44:48

DarkCart (15 views)
2014-10-31 21:43:57

TehJavaDev (40 views)
2014-10-27 03:28:38

TehJavaDev (30 views)
2014-10-27 03:27:51

DarkCart (44 views)
2014-10-26 19:37:11

Luminem (25 views)
2014-10-26 10:17:50

Luminem (30 views)
2014-10-26 10:14:04

theagentd (36 views)
2014-10-25 15:46:29

Longarmx (64 views)
2014-10-17 03:59:02

Norakomi (62 views)
2014-10-16 15:22:06
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!