Audio.java
上传用户:cjy_led
上传日期:2021-02-20
资源大小:2k
文件大小:8k
- package View;
- import java.awt.FlowLayout;
- import java.awt.GridLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import javax.media.Manager;
- import javax.media.NoPlayerException;
- import javax.media.Player;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JPanel;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import java.awt.BorderLayout;
- import java.awt.Component;
- import java.awt.FileDialog;
- import java.awt.TextArea;
- import javax.media.ControllerEvent;
- import javax.media.ControllerListener;
- import javax.media.EndOfMediaEvent;
- import javax.media.RealizeCompleteEvent;
- public class Audio implements ActionListener{
- JFrame jframeAudio= new JFrame("归档查看系统");//整体面板
- JTextArea message= new JTextArea(2,35); //显示播放信息
- JTextField filePath = new JTextField(15); //文件路径框
-
- JTextField jtNumAudio = new JTextField(10); //音频号
- JTextField jtName= new JTextField(10); //题名
- JTextField jtNumFiles= new JTextField(10); //档案号
- JTextField jtAuthor= new JTextField(10); //摄影者
- JTextField jtDate = new JTextField(10); //音频的日期
- JTextField jtPlace = new JTextField(10); //拍摄地点
- // JTextArea jtDescription= new JTextArea(2,43); //音频的描述
- TextArea jtDescription = new TextArea(null,2,65,TextArea.SCROLLBARS_VERTICAL_ONLY);
- JPanel jpanelControl= new JPanel(new GridLayout(2,1));
-
- File selectedFile;
- private FileDialog fileDialog;
- private String filePosition;
- private String fileName;
- /*
- * This object is from JMF(javax.media.Player)
- */
- private Player player;
- /*
- * 播放音乐的控制器
- * 实现了音量调节和调整进度等
- * 由JMF实现
- */
- private Component control;
-
- /**
- * 初始化面板界面
- */
- public Audio(){
- jframeAudio.setLayout(new BorderLayout());
- /*
- * 将整体规划为3个面板
- */
- JPanel jpanelNorth = new JPanel(new FlowLayout());
- JPanel jpanelCenter = new JPanel(new FlowLayout());
- JPanel jpanelSouth = new JPanel(new GridLayout(3,1));
- jframeAudio.add(jpanelNorth, BorderLayout.NORTH);
- jframeAudio.add(jpanelCenter, BorderLayout.CENTER);
- jframeAudio.add(jpanelSouth, BorderLayout.SOUTH);
-
- /*
- * 第一个面板有打开文件夹 选择查看器功能
- */
- // 文件选择设计
- JButton jbFindFile = new JButton("打开");
- jbFindFile.addActionListener(this);
- JLabel jlNull = new JLabel(" ");
- // 软件选择面板设计
- JButton picture = new JButton("图片查看器"); // 选择图片查看器软件
- JButton audio = new JButton("音频查看器"); // 选择音频查看器软件
- JButton video = new JButton("视频查看器"); // 选择视频查看器软件
- picture.addActionListener(this); // 给按钮添加事件响应
- audio.addActionListener(this);
- video.addActionListener(this);
- jpanelNorth.add(filePath);
- jpanelNorth.add(jbFindFile);
- jpanelNorth.add(jlNull);
- jpanelNorth.add(picture);
- jpanelNorth.add(audio);
- jpanelNorth.add(video);
- /*
- * 第二个面板是显示音频播放信息以及控制播放用的
- */
- jpanelCenter.add(jpanelControl,BorderLayout.CENTER);
- jpanelControl.add(message,BorderLayout.CENTER);
- message.setText(" Welcome to use the audio player!");
-
- /*
- * 第三个面板是工具面板 还有添加信息面板
- */
- JPanel jpMessage1= new JPanel(new GridLayout(2,1));
- JPanel jpMessage1_1= new JPanel(new FlowLayout());
- JPanel jpMessage1_2= new JPanel(new FlowLayout());
- JPanel jpMessage2= new JPanel(new FlowLayout());
- JPanel jpSubmit = new JPanel(new FlowLayout());
- jpMessage1.add(jpMessage1_1);
- jpMessage1.add(jpMessage1_2);
- jpanelSouth.add(jpMessage1);
- jpanelSouth.add(jpMessage2);
- jpanelSouth.add(jpSubmit);
- JLabel jlNumAudio= new JLabel("音频号:");
- JLabel jlName= new JLabel("题名: ");
- JLabel jlNumFiles= new JLabel("档案号: ");
- JLabel jlAuthor= new JLabel("摄影者:");
- JLabel jlDate= new JLabel("摄影时间:");
- JLabel jlPlace= new JLabel("拍摄地点:");
- JLabel jlDescription= new JLabel("说明: ");
-
- JButton jbSubmit = new JButton("提交");
- JButton jbReset = new JButton("清空");
-
- //第一个 信息面板
- jpMessage1_1.add(jlNumAudio);
- jpMessage1_1.add(jtNumAudio);
- jpMessage1_1.add(jlName);
- jpMessage1_1.add(jtName);
- jpMessage1_1.add(jlNumFiles);
- jpMessage1_1.add(jtNumFiles);
- jpMessage1_2.add(jlAuthor);
- jpMessage1_2.add(jtAuthor);
- jpMessage1_2.add(jlDate);
- jpMessage1_2.add(jtDate);
- jpMessage1_2.add(jlPlace);
- jpMessage1_2.add(jtPlace);
- //第二个 信息面板
- jpMessage2.add(jlDescription);
- jpMessage2.add(jtDescription);
- //工具面板
- jpSubmit.add(jbSubmit);
- jpSubmit.add(jbReset);
-
- jframeAudio.setSize(670,370);
- jframeAudio.setLocation(150, 100); // 窗口起始位置
- jframeAudio.setVisible(true);
- jframeAudio.setResizable(false);
- jframeAudio.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
-
- //事件的响应
- @Override
- public void actionPerformed(ActionEvent ae) {
- String s= ae.getActionCommand();
- if("图片查看器".equals(s)){
- new Picture();
- jframeAudio.setVisible(false);
- }else if("音频查看器".equals(s)){
- JOptionPane.showMessageDialog(null, "已经是音频播放页面了!");
- }else if("视频查看器".equals(s)){
- new Video();
- jframeAudio.setVisible(false);
- }else if("打开".equals(s)){
- fileDialog = new FileDialog(fileDialog);
- fileDialog.setVisible(true);
- if (fileDialog.getFile() != null) {
- filePosition = fileDialog.getDirectory() + fileDialog.getFile();// 原因请见同目录下的FileDialogDemo.java文件
- filePath.setText(filePosition);
- fileName = fileDialog.getFile();
- selectedFile = new File(filePosition);
- try {
- createPlayer();
- } catch (NoPlayerException e) {
- e.printStackTrace();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }else if("提交".equals(s)){
- submit();
- }else if("清空".equals(s)){
- reset();
- }
- }
-
- /*
- * 开始播放
- */
- private void createPlayer() throws NoPlayerException, MalformedURLException, IOException{
- if(player!=null){
- stopPlayer();
- }
- try{
- player = Manager.createPlayer(selectedFile.toURL());
- player.addControllerListener(new ControllerHand());
- player.start();
- message.setText("正在播放:"+ fileName);
- }catch(Exception e){
- JOptionPane.showMessageDialog(null, "本播放器不支持您要播放的格式!", "出错了!",
- JOptionPane.ERROR_MESSAGE);
- }
-
- }
-
- /*
- * 停止播放
- */
- private void stopPlayer(){
- player.stop();
- player.close();
- jpanelControl.remove(control);
- message.setText(" Welcome to use the audio player!");
- }
-
- private void reset(){
- jtNumAudio.setText("");
- jtName.setText("");
- jtNumFiles.setText("");
- jtAuthor.setText("");
- jtDate.setText("");
- jtPlace.setText("");
- jtDescription.setText("");
- }
-
- private void submit(){
-
- }
-
-
-
- public static void main(String[] args) {
- new Audio();
- }
- private class ControllerHand implements ControllerListener {
- public void controllerUpdate(ControllerEvent e) {
- if (e instanceof RealizeCompleteEvent) {
- Component visual = player.getVisualComponent();
- control = player.getControlPanelComponent();
- if (control != null) {
- jpanelControl.add(control,BorderLayout.SOUTH);
- }
- jpanelControl.doLayout();
- return;
- }
- if (e instanceof EndOfMediaEvent) {
- stopPlayer();
- try {
- createPlayer();
- } catch (NoPlayerException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (MalformedURLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (IOException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- }
- }
-
- }