📄 mp3player.java
字号:
/*******************************************************************************
* Copyright (c) 2007 Siemens AG
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Ludwig Mittermeier - initial API and implementation
* Kai Toedter - some changes
*******************************************************************************/
package com.siemens.ct.mp3m.ui.player;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.advanced.AdvancedPlayer;
import javazoom.jl.player.advanced.PlaybackEvent;
import javazoom.jl.player.advanced.PlaybackListener;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.contexts.IContextActivation;
import org.eclipse.ui.contexts.IContextService;
import com.siemens.ct.mp3m.ui.player.IMp3PlayerStateListener.State;
public class Mp3Player extends PlaybackListener implements Runnable {
private static Mp3Player instance;
private String fileName;
private AdvancedPlayer player;
private Thread playerThread;
private State state;
private List<IMp3PlayerStateListener> listeners;
private int currentFrame;
private IContextActivation contextActivation;
private IContextService contextService;
private Mp3Player() {
state = State.STOPPED;
listeners = new ArrayList<IMp3PlayerStateListener>();
contextService = (IContextService) PlatformUI.getWorkbench().getService(IContextService.class);
}
public static Mp3Player getInstance() {
if (instance == null) {
instance = new Mp3Player();
}
return instance;
}
public synchronized void play(String fileName) throws FileNotFoundException {
this.fileName = fileName;
if (state == State.PLAYING) {
player.stop();
while (state != State.STOPPED) {
// wait until the player is in state STOPPED
}
}
// This is a trivial implementation just using another thread for every
// player. In a real application think about thread pooling.
playerThread = new Thread(this);
playerThread.start();
if (contextActivation == null) {
contextActivation = contextService
.activateContext("com.siemens.ct.mp3m.ui.player.context.stop");
}
}
public void pause() {
player.stop();
setState(State.PAUSED);
}
public void resume() {
if (state == State.PAUSED) {
playerThread = new Thread(this);
playerThread.start();
}
}
public void stop() {
if (state == State.PAUSED) {
player.close();
} else {
player.stop();
}
playerThread = null;
if (contextActivation != null) {
contextService.deactivateContext(contextActivation);
contextActivation = null;
}
}
public void run() {
try {
FileInputStream fis = new FileInputStream(fileName);
player = new AdvancedPlayer(fis);
player.setPlayBackListener(Mp3Player.this);
if (state == State.PAUSED) {
player.play(currentFrame, Integer.MAX_VALUE);
} else {
player.play();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JavaLayerException e) {
e.printStackTrace();
}
}
public void playbackFinished(PlaybackEvent evt) {
currentFrame = evt.getFrame();
if (state == State.PLAYING) {
setState(State.STOPPED);
}
}
public void playbackStarted(PlaybackEvent evt) {
setState(State.PLAYING);
}
public void addMp3PlayerStateListener(IMp3PlayerStateListener listener) {
listeners.add(listener);
}
public void removeMp3PlayerStateListener(IMp3PlayerStateListener listener) {
listeners.remove(listener);
}
public void setState(State state) {
this.state = state;
for(IMp3PlayerStateListener listener: listeners) {
listener.stateChanged(state);
}
}
/**
* @return the state
*/
public State getState() {
return state;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -