Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

97
LINES

< > BotCompany Repo | #1003617 // Play Music With Slider

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 683K of libraries. Click here for Pure Java version (1721L/12K/40K).

!752

static S musicID = "#1001093";

static int songDuration, seekValue;
static long songBytes;
static int position;

static new JSlider seeker;
static new JLabel text;

p-awt {
  substanceLAF("Moderate");
  
  //File mp3 = loadLibrary(musicID); // deh polish music
  
  // OK, .wav works.
  //File mp3 = new File("/media/CE009E97009E865D__/stuff/Musik/mp3/recorded/say something.wav");
  
  // Let's try OGG. (Works with fixing of classpath.)
  File mp3 = new File("/media/CE009E97009E865D__/stuff/Musik/UFDD - The Greetings Part.ogg");
  
  // Let's try MP3. Works now too!
  //File mp3 = new File("/media/CE009E97009E865D__/stuff/Musik/10. Odyssey - If you're looking for a way out.mp3");
  
  // Fixing the classpath for Java Sound (MP3 + OGG)
  fixContextClassLoader();
  
  JFrame frame = showFrame(centerAndEast(seeker, text));
  frame.pack();
  setFrameWidth(frame, 500);
  
  BasicPlayer player = makeBasicMP3Player();
  
  player.addBasicPlayerListener(new BasicPlayerListener {
    public void opened(Object stream, Map properties) {
      for (Object key : properties.keySet())
        print("opened: " + key + "=" + properties.get(key));

      final Number bytes = (Number) properties.get("audio.length.bytes");

      // Try to find duration of song
      long microseconds = 0;
      O duration = properties.get("duration");
      
      if (duration instanceof Number)
        microseconds = ((Number) duration).longValue();
      else pcall {
        long frames = asLong(properties.get("audio.length.frames"));
        long fps = asLong(properties.get("audio.framerate.fps"));
        microseconds = frames*1000000/fps;
      }
      
      print("Song Duration: " + microseconds);
      final long _microseconds = microseconds;
      
      awt {
        songDuration = (int) (_microseconds / 1000);
        seeker.setMaximum(songDuration);
        seeker.setEnabled(true);
        updateText();
        songBytes = bytes.longValue();
      }
    }

    public void progress(int bytesread, final long microseconds, byte[] pcmdata, Map properties) {
      //System.out.println("pcmdata: "+ pcmdata[0] + " " + pcmdata[1]);
      awt {
        //print("Progress: ms=" + microseconds);
        if (!seeker.getValueIsAdjusting()) {
          int ms = (int) (microseconds / 1000) + seekValue;
          seeker.setValue(ms);
          position = ms;
          updateText();
        }
      }
    }

    public void stateUpdated(BasicPlayerEvent event) {
      awt {
        updateStateStuff();
      }
    }

    public void setController(BasicController controller) {}
  });
    
  player.open(mp3);
  player.play();
}

svoid updateStateStuff {
}

svoid updateText {
  text.setText(formatMinuteAndSeconds(position/1000) + " / " + formatMinuteAndSeconds(songDuration/1000));
}

Author comment

Began life as a copy of #1003615

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003617
Snippet name: Play Music With Slider
Eternal ID of this version: #1003617/1
Text MD5: a46005ea568cae300eefc46d3d13502b
Transpilation MD5: 4b3aef7d62aa0992f7b174d2b4c3f17e
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-07-24 19:56:25
Source code size: 2886 bytes / 97 lines
Pitched / IR pitched: No / No
Views / Downloads: 663 / 804
Referenced in: #1003634 - Music Player v2