⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 player.java

📁 Eclipse高级编程3源码(书本源码)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		// Create layout data for toolbar
		GridData data = new GridData();
		data.horizontalIndent = 5;
		data.verticalAlignment = GridData.END;
		data.grabExcessHorizontalSpace = true;
		data.grabExcessVerticalSpace = true;
		toolbar.setLayoutData(data);
	}

	/**
	 * Convenience method for creating a toolbar button.
	 * 
	 * @param bar
	 *            the ToolBar instance
	 * @param style
	 *            the button type
	 * @param text
	 *            text for button label
	 * @param tip
	 *            tool tip
	 * @return ToolItem - the created toolbar button
	 */
	private ToolItem makeToolItem(ToolBar bar, int style, String text,
			String tip) {
		ToolItem item = new ToolItem(bar, style);
		if (style != SWT.SEPARATOR) {
			item.setText(text);
			item.setToolTipText(tip);
			// Add event processing for button clicks
			item.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					processButton(e);
				}
			});
		}
		return item;
	}

	/**
	 * This method processes all ToolItem events.
	 * 
	 * @param e
	 *            the event object
	 */
	private void processButton(SelectionEvent e) {
		Widget widget = e.widget;
		if (widget == playButton) {
			play();
		} else if (widget == stopButton) {
			stop();
		} else if (widget == pauseButton) {
			pause();
		} else if (widget == forwardButton) {
			forward();
		} else if (widget == backButton) {
			back();
			// The following buttons are of type CHECK.
			// We must retrieve their state to react correctly.
		} else if (widget == descriptionButton) {
			if (descriptionButton.getSelection())
				showDescription();
			else
				hideDescription();
		} else if (widget == playlistButton) {
			if (playlistButton.getSelection())
				showPlaylist();
			else
				hidePlaylist();
		}
	}

	/**
	 * Create status panel with total duration and operating mode.
	 * 
	 * @param parent
	 *            the containing Composite
	 */
	private void createStatusPanel(Composite parent) {
		// Create panel as a new Composite
		statusPanel = new Composite(parent, SWT.NONE);
		statusPanel.setBackground(parent.getBackground());
		// Use a vertical row layout for the panel
		RowLayout rowLayout = new RowLayout();
		rowLayout.type = SWT.VERTICAL;
		rowLayout.wrap = false;
		rowLayout.pack = false;
		statusPanel.setLayout(rowLayout);
		// Now create the widgets of the status panel
		lengthLabel = createStatusLabel(statusPanel, timeFormat(0));
		statusLabel = createStatusLabel(statusPanel, IDLE);
	}

	private Label createStatusLabel(Composite panel, String text) {
		Label label = new Label(panel, SWT.RIGHT);
		label.setBackground(panel.getBackground());
		label.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
		label.setText(text);
		return label;
	}

	/**
	 * Converts the duration into an appropriate display format.
	 * 
	 * @param sec
	 *            Seconds
	 * @return String mm:ss.s
	 */
	private static String timeFormat(double sec) {
		int sec10 = (int) (sec * 10);
		return twoDigitFormat(sec10 / 600) + ":"
				+ twoDigitFormat((sec10 / 10) % 60) + "." + (sec10 % 10);
	}

	/**
	 * Format an integer into a two-digit string with leading zeros.
	 * 
	 * @param n
	 *            the integer value
	 * @return String - the result string
	 */
	private static String twoDigitFormat(int n) {
		if (n < 10)
			return "0" + n;
		return String.valueOf(n);
	}

	/** * Update SWT-Thread ** */
	/**
	 * Updates SWT-Widgets, caused by events from other threads, are executed
	 * via this method. By performing the updates under the SWT-thread
	 * (display.asyncExec()) we avoid an SWT thread error.
	 */
	private void updateGUI() {
		display.asyncExec(new Runnable() {
			public void run() {
				if (!toplevelShell.isDisposed()) {
					// Update scale
					scale.setMaximum(maxPosition);
					scale.setSelection(currentPosition);
					// Update operation mode
					updateText(statusLabel, mediaState);
					// Update total length
					updateText(lengthLabel, timeFormat(lengthInSec));
					// Check if we have to start the next sound file
					if (state == EOM) {
						state = STOP;
						forward();
					}
				}
			}
		});
	}

	/**
	 * Updates the text of a label
	 * 
	 * @param c
	 *            the Label instance
	 * @param s
	 *            the new text
	 */
	private void updateText(Label c, String s) {
		// test against current content to avoid screen flicker
		if (!c.getText().equals(s))
			c.setText(s);
	}

	/** ** Button actions *** */
	/**
	 * Positions in the sound file when the scale is modified by the end user
	 * (only for .WAV files).
	 */
	private void seek() {
		try {
			double position = ((double) scale.getSelection())
					/ ((double) scale.getMaximum());
			soundPlayer.setSeek(position);
			updateGUI();
		} catch (IOException e) {
			System.out.println(e.toString());
		}
	}

	/**
	 * Stops playing.
	 */
	private void stop() {
		if (state != STOP) {
			soundPlayer.stopPlayback();
			state = STOP;
			mediaState = IDLE;
			lengthInSec = 0;
		}
		updateGUI();
	}

	/**
	 * Pauses the playing process.
	 */
	private void pause() {
		switch (state) {
		case PLAY:
			soundPlayer.pausePlayback();
			state = PAUSE;
			mediaState = PAUSED;
			break;
		case PAUSE:
			soundPlayer.resumePlayback();
			state = PLAY;
			mediaState = PLAYING;
		}
		updateGUI();
	}

	/**
	 * Starts playing.
	 */
	private void play() {
		if (state == PLAY)
			// Current play processes are stopped
			stop();
		try {
			switch (state) {
			case PAUSE:
				// If the playing process was paused, we resume it.
				soundPlayer.resumePlayback();
				break;
			case STOP:
				// Otherwise we start all over again.
				// Fetch name of background image and title
				currentImage = playlistModel.getFeature(IMAGEFILE);
				currentTitle = playlistModel.getFeature(TITLE);
				// Update description window
				updateDescription();
				// Fetch name of sound file
				String filename = playlistModel.getFeature(SOUNDFILE);
				// Enforce a redraw of the canvas
				canvas.redraw();
				// Do nothing if the sound file does not
				// exist any more.
				if (!doesFileExist(filename))
					return;
				// Otherwise configure the engine
				soundPlayer.setDataSource(openFile(filename));
				soundPlayer.startPlayback();
				soundPlayer.setGain(0.5f);
				soundPlayer.setPan(0.5f);
				// Fetch the total duration of the sound file
				lengthInSec = soundPlayer.getTotalLengthInSeconds();
				maxPosition = (int) lengthInSec;
				// If the sound format is not WAV we deactivate
				// disable the scale (no scrolling possible).
				boolean canSeek = ((soundPlayer.getAudioFileFormat() != null) && (soundPlayer
						.getAudioFileFormat().getType().toString()
						.startsWith("WAV")));
				scale.setEnabled(canSeek);
			}
			// Now set the operation modus and update the GUI.
			state = PLAY;
			mediaState = PLAYING;
			updateGUI();
		} catch (UnsupportedAudioFileException e) {
			System.out.println(e.toString());
		} catch (LineUnavailableException e) {
			System.out.println(e.toString());
		} catch (IOException e) {
			System.out.println(e.toString());
		}
	}

	/**
	 * If the playlist has a next element, we stop playing the current sound
	 * file and start again with the next.
	 */
	private void forward() {
		if (playlistModel.next()) {
			stop();
			play();
		}
	}

	/**
	 * If the playlist has a previous element, we stop playing the current sound
	 * file and start again with the previous.
	 */
	private void back() {
		if (playlistModel.previous()) {
			stop();
			play();
		}
	}

	/** ** Manage windows ** */
	/**
	 * Creates a new DescriptionViewer if it not yet exists. Supplies the
	 * DescriptionViewer with new text content.
	 */
	private void showDescription() {
		if (descriptionWindow == null) {
			// Create window
			descriptionWindow = new DescriptionWindow(toplevelShell,
					playlistModel);
			// Initialize window
			descriptionWindow.create();
			// Fetch Shell instance
			Shell shell = descriptionWindow.getShell();
			Rectangle bounds = toplevelShell.getBounds();
			// Position at the right hand side of the main window
			shell.setBounds(bounds.x + bounds.width - 5, bounds.y + 10, 320,
					240);
			// React to shell抯 close button
			shell.addShellListener(new ShellAdapter() {
				public void shellClosed(ShellEvent e) {
					// Update toolbar button
					hideDescription();
				}
			});
			// Open the window
			descriptionWindow.open();
		}
	}

	/**
	 * Closes the description window
	 */
	private void hideDescription() {
		// Reset the description toolbar button
		descriptionButton.setSelection(false);
		// Close window
		if (descriptionWindow != null) {
			descriptionWindow.close();
			descriptionWindow = null;
		}
	}

	/**
	 * Updates the window with new text
	 */
	private void updateDescription() {
		if (descriptionWindow != null)
			descriptionWindow.update();
	}

	/**
	 * Creates a new playlist window
	 */
	private void showPlaylist() {
		if (playlistWindow == null) {
			// Create new PlaylistWindow instance
			playlistWindow = new PlaylistWindow(toplevelShell, playlistModel);
			// Initialize the window to allow us to retrieve
			// the shell instance
			playlistWindow.create();
			Shell shell = playlistWindow.getShell();
			shell.addShellListener(new ShellAdapter() {
				public void shellClosed(ShellEvent e) {
					hidePlaylist();
				}
			});
			// Position the shell below the main window
			Rectangle bounds = toplevelShell.getBounds();
			shell.setBounds(bounds.x + bounds.width / 8, bounds.y
					+ bounds.height - 5, 400, 240);
			// Open the window.
			playlistWindow.open();
		}
	}

	/**
	 * Closes playlist window
	 */
	private void hidePlaylist() {
		// Reset playlist toolbar button.
		playlistButton.setSelection(false);
		// Close window
		if (playlistWindow != null) {
			playlistWindow.close();
			playlistWindow = null;
		}
	}

	/**
	 * @see javazoom.jlGui.BasicPlayerListener#updateMediaData(byte)
	 */
	public void updateMediaData(byte[] data) {
	}

	/**
	 * @see javazoom.jlGui.BasicPlayerListener#
	 *      updateMediaState(java.lang.String)
	 */
	public void updateMediaState(String newState) {
		// At file end set operation mode to IDLE
		if (newState.equals("EOM") && state != STOP) {
			this.state = EOM;
			mediaState = IDLE;
			// Update GUI
			updateGUI();
		}
	}

	/**
	 * @see javazoom.jlGui.BasicPlayerListener#updateCursor(int, int)
	 */
	public void updateCursor(int cursor, int total) {
		// Save maximum position and current position; update GUI
		maxPosition = total;
		currentPosition = cursor;
		updateGUI();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -