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).

1  
!752
2  
3  
static S musicID = "#1001093";
4  
5  
static int songDuration, seekValue;
6  
static long songBytes;
7  
static int position;
8  
9  
static new JSlider seeker;
10  
static new JLabel text;
11  
12  
p-awt {
13  
  substanceLAF("Moderate");
14  
  
15  
  //File mp3 = loadLibrary(musicID); // deh polish music
16  
  
17  
  // OK, .wav works.
18  
  //File mp3 = new File("/media/CE009E97009E865D__/stuff/Musik/mp3/recorded/say something.wav");
19  
  
20  
  // Let's try OGG. (Works with fixing of classpath.)
21  
  File mp3 = new File("/media/CE009E97009E865D__/stuff/Musik/UFDD - The Greetings Part.ogg");
22  
  
23  
  // Let's try MP3. Works now too!
24  
  //File mp3 = new File("/media/CE009E97009E865D__/stuff/Musik/10. Odyssey - If you're looking for a way out.mp3");
25  
  
26  
  // Fixing the classpath for Java Sound (MP3 + OGG)
27  
  fixContextClassLoader();
28  
  
29  
  JFrame frame = showFrame(centerAndEast(seeker, text));
30  
  frame.pack();
31  
  setFrameWidth(frame, 500);
32  
  
33  
  BasicPlayer player = makeBasicMP3Player();
34  
  
35  
  player.addBasicPlayerListener(new BasicPlayerListener {
36  
    public void opened(Object stream, Map properties) {
37  
      for (Object key : properties.keySet())
38  
        print("opened: " + key + "=" + properties.get(key));
39  
40  
      final Number bytes = (Number) properties.get("audio.length.bytes");
41  
42  
      // Try to find duration of song
43  
      long microseconds = 0;
44  
      O duration = properties.get("duration");
45  
      
46  
      if (duration instanceof Number)
47  
        microseconds = ((Number) duration).longValue();
48  
      else pcall {
49  
        long frames = asLong(properties.get("audio.length.frames"));
50  
        long fps = asLong(properties.get("audio.framerate.fps"));
51  
        microseconds = frames*1000000/fps;
52  
      }
53  
      
54  
      print("Song Duration: " + microseconds);
55  
      final long _microseconds = microseconds;
56  
      
57  
      awt {
58  
        songDuration = (int) (_microseconds / 1000);
59  
        seeker.setMaximum(songDuration);
60  
        seeker.setEnabled(true);
61  
        updateText();
62  
        songBytes = bytes.longValue();
63  
      }
64  
    }
65  
66  
    public void progress(int bytesread, final long microseconds, byte[] pcmdata, Map properties) {
67  
      //System.out.println("pcmdata: "+ pcmdata[0] + " " + pcmdata[1]);
68  
      awt {
69  
        //print("Progress: ms=" + microseconds);
70  
        if (!seeker.getValueIsAdjusting()) {
71  
          int ms = (int) (microseconds / 1000) + seekValue;
72  
          seeker.setValue(ms);
73  
          position = ms;
74  
          updateText();
75  
        }
76  
      }
77  
    }
78  
79  
    public void stateUpdated(BasicPlayerEvent event) {
80  
      awt {
81  
        updateStateStuff();
82  
      }
83  
    }
84  
85  
    public void setController(BasicController controller) {}
86  
  });
87  
    
88  
  player.open(mp3);
89  
  player.play();
90  
}
91  
92  
svoid updateStateStuff {
93  
}
94  
95  
svoid updateText {
96  
  text.setText(formatMinuteAndSeconds(position/1000) + " / " + formatMinuteAndSeconds(songDuration/1000));
97  
}

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: 662 / 803
Referenced in: [show references]