This is the uiBehaviour() procedure we left behind (MainView class).
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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | private void uiBehaviour() { //File chooser fc.setMultiSelectionEnabled(true); fc.setFileFilter(new FileFilter() { @Override public String getDescription() { return "only supported audio files (mp3, wav)"; } @Override public boolean accept(File f) { if(f.isDirectory()) return true; if(f.getName().endsWith(".mp3")) return true; if(f.getName().endsWith(".wav")) return true; return false; } }); btnAdd.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { int returnVal = fc.showOpenDialog(btnAdd); if(returnVal == JFileChooser.APPROVE_OPTION){ File[] files = fc.getSelectedFiles(); for(File f : files){ player.addSong(f.getAbsolutePath()); songList.addElement(f.getName()); log("Added file " + f.getName() + " to playlist"); } } else{ log("No file selected"); } } }); //Song List jSongList.setModel(songList); jSongList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); jSongList.setLayoutOrientation(JList.VERTICAL); //Event that triggers at double click jSongList.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt){ JList list = (JList)evt.getSource(); if(evt.getClickCount() == 2){ log("Double click detected, moving to selected item."); int index = list.locationToIndex(evt.getPoint()); player.setIndexSong(index); try { player.play(); } catch (BasicPlayerException ev) { ev.printStackTrace(); } } } }); //Btn Delete btnDel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //Executed Outside UI Thread BackgroundExecutor.get().execute(new Runnable() { @Override public void run() { int[] indexes = jSongList.getSelectedIndices(); int removed = 0; for(int i : indexes) { log("Removed Song ("+(i-removed)+")" + songList.get(i-removed)); player.removeSong(i-removed); songList.remove(i-removed); removed++; } } }); } }); //Play Btn btnPlay.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { try { tooglePlay(); } catch (BasicPlayerException e1) { e1.printStackTrace(); } } }); //Next and Previous btns btnNext.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { try { player.nextSong(); //seekbar.resetLastSeek(); } catch (BasicPlayerException e) { log("Error calling the next song"); e.printStackTrace(); } } }); btnPrev.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { try { player.prvSong(); //seekbar.resetLastSeek(); } catch (BasicPlayerException e) { log("Error calling the previous song"); e.printStackTrace(); } } }); //Player related behaviour player.addBasicPlayerListener(new BasicPlayerListener() { @Override public void stateUpdated(BasicPlayerEvent event) { if(event.getCode() == BasicPlayerEvent.EOM) { //seekbar.resetLastSeek(); try { player.nextSong(); } catch (BasicPlayerException e) { e.printStackTrace(); } log("EOM event catched, calling next song."); } if(event.getCode() == BasicPlayerEvent.PAUSED){ //btnPlay.setText(">"); btnPlay.setIcon(playIcon); } if(event.getCode() == BasicPlayerEvent.RESUMED){ //btnPlay.setText("||"); btnPlay.setIcon(pauseIcon); } } @Override public void setController(BasicController arg0) {} @Override public void progress(int bytesread, long microseconds, byte[] pcmdata, Map properties) { //we don't want to use microseconds directly because it gets resetted on seeking seekbar.updateSeekBar(player.getProgressMicroseconds(), currentAudioDurationSec); if(wff != null) wff.updateWave(pcmdata); if(fdf != null) fdf.updateWave(pcmdata); } @Override public void opened(Object arg0, Map arg1) { //btnPlay.setText("||"); btnPlay.setIcon(pauseIcon); jSongList.setSelectedIndex(player.getIndexSong()); lblplaying.setText("Now Playing: " + songList.get(player.getIndexSong())); currentAudioDurationSec = player.getAudioDurationSeconds(); } }); //Timers Executor / Every 1 Second timersExec.scheduleAtFixedRate(new Runnable() { @Override public void run() { updateTimers(); //updatePlayingText(); } }, 0, 1, TimeUnit.SECONDS); titleExec.scheduleAtFixedRate(new Runnable() { @Override public void run() { updatePlayingText(); } }, 0, 1, TimeUnit.SECONDS); //Btn Waveform btnShWf.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { SwingUtilities.invokeLater(new Runnable(){ @Override public void run() { wff = new WaveformFrame(); wff.setVisible(true); } }); } }); //Btn that show freq diagram btnShDi.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { fdf = new FreqDiagFrame(); fdf.setVisible(true); } }); //Open status window frame btnShSt.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { //stf = new StatusFrame(); stf.setVisible(true); } }); } /** * Used by the Play/Pause button */ private void tooglePlay() throws BasicPlayerException { if(songList.size() == 0) return; if(!player.isPaused()){ player.pause(); //btnPlay.setText(">"); btnPlay.setIcon(playIcon); } else{player.play();} } |
It's a lot of code so let's analyze it in order:
- File chooser: It's a swing component that can be used to create an interface to select one or multiple files. I've set a file filter to let it see only files ending with .mp3 or .wav.
- btnAdd will use the File chooser to get the songs the user selected, adding it to the playlist.
- Song list: contains visual list of the playlist, I've added a behavior so that when a double click occurs it gets the index clicked and plays the selected song.
- Btn Delete: delete the selected songs from the playlist
- Play Btn: a simple play/pause toogle button.
- Btn Next/ Btn Prev: move to next or previous song.
- Player related behaviour: this is where all the fun things happens. We add a new event listener to the basic player, this way we can have some things done every time a certain event happens regarding the player.
- We use progress to update the seekbar
- We use opened to detect if a new song start playing ( a new audio file has been opened by the BasicPlayer class...)
- With state update we detect if the streaming has been paused, stopped, resumed, etc.
- Other: something regarding additional features
There are some concept that we already explained in the previous posts, the code shouldn't be that hard but for problems or anything just let me know in the comments.
Nessun commento:
Posta un commento