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

📄 srshell.java

📁 接上一个程序 播放器可以实现波形的显示 还可以改变采样点个数和算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.sound.sampled.*;
import java.io.*;
import javax.swing.filechooser.*;

public class SRshell extends JFrame
{
	//声明声音录制播放的逻辑类
	private SRLogic srl;
	//声明声音录制时的格式选择面板
	private FormatControls fc;
	
	final int bufSize = 16384;
	
	//声明声音输入流、声音格式类以及用来播放声音的Clip类
	private AudioInputStream wavStream,wavSave;
	private AudioFormat audioFormat;
	private Clip clip,recClip;
	
	private int[] audioData;
	
	Object currentSound;
	
	//声明声音播放面板上的一系列功能按钮、标签等
	private JPanel pControl,pWavemap,pInfo,pTool,pMain,pSamplingGrap;
	private SamplingGraph wavMap;
	private JButton bPlay,bPause,bStop,bRecorder,bForward,bBack;
	private JButton bCreate,bLoad,bSave,bAnalyze,bZoomIn,bZoomOut,bSet,bExit;
	private JSlider sGain,sSeek;
	
	private JLabel lName,lSize,lDuration,lSample1,lSample2,lTrack,lFormat;
	private JLabel wavName,wavSize,wavDuration,wavSample1,wavSample2,wavTrack,wavFormat;
	
	public SRshell()
	{
		/*以下整个public SRshell()方法都是在生成SoundRecorder程序的外壳
		 *包括界面,按钮,标签,对按钮的监听器等等,不再一一注明
		 */		
		fc=new FormatControls(this);
		srl=new SRLogic(this);
		
		sSeek=new JSlider();
		sSeek.setValue(0);
		sSeek.setMajorTickSpacing(10);
		sSeek.setMinorTickSpacing(5);
		sSeek.setPaintTicks(true);
		
		sGain=new JSlider(JSlider.VERTICAL);
		sGain.setPaintTicks(true);
		sGain.setToolTipText("音量控制");
		sGain.setValue(50);
		sGain.setMajorTickSpacing(10);
		sGain.setMinorTickSpacing(5);
		sGain.setPaintTicks(true);
		
		bBack=new JButton("快退");
		bPlay=new JButton("播放");
		bPause=new JButton("暂停");
		bForward=new JButton("快进");
		bStop=new JButton("停止");
		bRecorder=new JButton("录音");
		
		bBack.setToolTipText("后退5秒");
		bForward.setToolTipText("前进5秒");
		
		bStop.setEnabled(false);
		bPause.setEnabled(false);
		
		
		bCreate=new JButton("新建");
		bLoad=new JButton("载入");
		bSave=new JButton("保存");
		bAnalyze=new JButton("分析");
		bZoomIn=new JButton("放大");
		bZoomOut=new JButton("缩小");
		bSet=new JButton("设置");
		bExit=new JButton("退出");
		
		lName=new JLabel("  名    称:");
		lSize=new JLabel("  大    小:");
		lDuration=new JLabel("  长    度:");
		lSample1=new JLabel("  采样率:");
		lSample2=new JLabel("  采样值:");
		lTrack=new JLabel("  声    道:");
		lFormat=new JLabel("  格    式:");
		
		wavName=new JLabel("未知",JLabel.CENTER);
		wavSize=new JLabel("未知",JLabel.CENTER);
		wavDuration=new JLabel("未知",JLabel.CENTER);
		wavSample1=new JLabel("未知",JLabel.CENTER);
		wavSample2=new JLabel("未知",JLabel.CENTER);
		wavTrack=new JLabel("未知",JLabel.CENTER);
		wavFormat=new JLabel("未知",JLabel.CENTER);
		
		pControl=new JPanel();
		pWavemap=new JPanel();
		pInfo=new JPanel();
		pTool=new JPanel();
		pMain=new JPanel();
		pSamplingGrap=new JPanel();
		
		wavMap=new SamplingGraph();
		
		pControl.setBorder(BorderFactory.createTitledBorder("播放控制"));
		
		pMain.setBorder(BorderFactory.createTitledBorder("波形显示面板"));
		
		pWavemap.setBorder(BorderFactory.createEtchedBorder());
		
		pInfo.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"信息栏"),
			BorderFactory.createEtchedBorder()
		));
		
		pTool.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"工具栏"),
			BorderFactory.createEtchedBorder()
		));
		
		pSamplingGrap.setLayout(new BorderLayout());
		pSamplingGrap.add(new 
			JScrollPane(wavMap,JScrollPane.VERTICAL_SCROLLBAR_NEVER,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS),
			BorderLayout.CENTER);
		
		pControl.add(bBack);
		pControl.add(bPlay);
		pControl.add(bPause);
		pControl.add(bForward);
		pControl.add(bStop);
		pControl.add(bRecorder);
		
		pTool.add(bCreate);
		pTool.add(bLoad);
		pTool.add(bSave);
		pTool.add(bAnalyze);
		pTool.add(bZoomIn);
		pTool.add(bZoomOut);
		pTool.add(bSet);
		pTool.add(bExit);
		pTool.setPreferredSize(new Dimension(100,400));
		
		pInfo.setLayout(new GridLayout(0,1));
		pInfo.add(lName);
		pInfo.add(wavName);
		pInfo.add(lSize);
		pInfo.add(wavSize);
		pInfo.add(lDuration);
		pInfo.add(wavDuration);
		pInfo.add(lSample1);
		pInfo.add(wavSample1);
		pInfo.add(lSample2);
		pInfo.add(wavSample2);
		pInfo.add(lTrack);
		pInfo.add(wavTrack);
		pInfo.add(lFormat);
		pInfo.add(wavFormat);
		pInfo.setPreferredSize(new Dimension(150,400));

		
		
		pWavemap.setLayout(new BorderLayout());
		pWavemap.add(sSeek,BorderLayout.SOUTH);
		pWavemap.add(pSamplingGrap,BorderLayout.CENTER);
		
		//加入按钮动作监听
		
		sGain.addChangeListener(
			new ChangeListener()
			{
				public void stateChanged(ChangeEvent e)
				{
					srl.setGain();
				} 
			}
		);
		
		sSeek.addChangeListener(
		new ChangeListener()
			{
				public void stateChanged(ChangeEvent e)
				{
					srl.setSeek();
				}
			}
		);
		
		
		bPlay.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.play();
				}
			}
		);
		
		bPause.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.pause();
				}
			}
		);
		
		bStop.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.stop();
				}
			}
		);
		
		bRecorder.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.rec();
				}
			}
		);
		
		bForward.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.forward();
				}
			}
		);
		
		bBack.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.back();
				}
			}
		);
		
		bCreate.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.create();
				}
			}
		);
		
		bLoad.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.load();
				}
			}
		);
		
		bSave.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.save();
				}
			}
		);
		
		bAnalyze.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.analyze();
				}
			}
		);
		
		bZoomIn.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.zoom("in");
				}
			}
		);
		
		bZoomOut.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.zoom("out");
				}
			}
		);
		
		bSet.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.set();
				}
			}
		);
		
		bExit.addActionListener(
			new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					srl.exit();
				}
			}
		);
		
		pMain.setLayout(new BorderLayout());
		pMain.add(pWavemap,BorderLayout.CENTER);
		pMain.add(pControl,BorderLayout.SOUTH);
		pMain.add(sGain,BorderLayout.EAST);
		
		getContentPane().add(pMain,BorderLayout.CENTER);	
		getContentPane().add(pTool,BorderLayout.WEST);
		getContentPane().add(pInfo,BorderLayout.EAST);
		
		setTitle("SoundRecorder V0.1");
		setSize(700,400);
		setVisible(true);
		
		//窗口打开后在屏幕中央显示
 		int ScreenWidth=getToolkit().getScreenSize().width;
		int ScreenHeight=getToolkit().getScreenSize().height;
		setLocation((ScreenWidth-getWidth())/2,(ScreenHeight-getHeight())/2);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	//用于生成波形图
	public void createWave(AudioInputStream wavStream)
	{
		wavMap.getAudioInputStream(wavStream);
		wavMap.createWaveForm(null);
		audioData=wavMap.getAudioData();
	}
	
	//用来判断是否处于Play状态,调整播放按钮的使能状态
	public void isPlay(boolean bl)
	{
		if(bl)
		{
			bPlay.setEnabled(false);
			bRecorder.setEnabled(false);
			bStop.setEnabled(true);
			bPause.setEnabled(true);
		}
		else
		{
			bPlay.setEnabled(true);
			bRecorder.setEnabled(true);
			bStop.setEnabled(false);
			bPause.setEnabled(false);
		}
	}
	
	//用来判断是否处于Rec状态,调整播放按钮的使能状态
	public void isRec(boolean bl)
	{
		if(bl)
		{
			bPlay.setEnabled(false);
			bStop.setEnabled(true);
			bRecorder.setEnabled(false);
			bForward.setEnabled(false);
			bBack.setEnabled(false);
		}
		else
		{
			bPlay.setEnabled(true);
			bRecorder.setEnabled(true);
			bForward.setEnabled(true);
			bBack.setEnabled(true);
			bStop.setEnabled(false);
		}
	}
	
	//用来判断是否处于暂停状态,调整播放按钮的使能状态
	public void isPause(boolean bl)
	{
		if(bl)
		{
			bPlay.setEnabled(true);
			bPause.setEnabled(false);
			bStop.setEnabled(true);
		}
		else
		{
			bPause.setEnabled(true);	
		}
	}
	
	//设置最开始播放声音时sGain和sSeek两个滑动杆的初始位置
	public void startWave()
	{
	    	sGain.setValue(50);
	    	sSeek.setValue(0);
	}
	
	//放大波形图
	public void zoomIn()
	{
		wavMap.incSize();
		pSamplingGrap.repaint();
	}
	
	//缩小波形图
	public void zoomOut()
	{
		wavMap.decSize();
		pSamplingGrap.repaint();
	}
	
	//取得声音文件的文件名并在面板上显示	
	public void setWavName(String str)
	{
		wavName.setText(str);
	}
	
	//取得声音文件的路径并在面板上显示
	public void setWavPath(String str)
	{
		wavName.setToolTipText(str);
	}
	
	//取得声音文件的播放时间并在面板上显示
	public void setWavDuration(String str)
	{
		wavDuration.setText(str);
	}
	
	//取得声音文件的大小并在面板上显示
	public void setWavSize(String str)
	{
		wavSize.setText(str);
	}
	
	//取得声音文件的采样频率
	public void setWavSample1(String str)
	{
		wavSample1.setText(str);
	}
	
	//取得声音文件的采样值
	public void setWavSample2(String str)
	{
		wavSample2.setText(str);
	}
	
	//取得声音文件的声道设置
	public void setWavTrack(String str)
	{
		wavTrack.setText(str);
	}
	
	//去的声音文件的格式
	public void setWavFormat(String str)
	{
		wavFormat.setText(str);
	}
	
	//主运行函数,程序从这里开始运行
	public static void main(String args[])
	{		
		setLookAndFeel();
		SRshell sr=new SRshell();
		setLookAndFeel();
	}
	
	//一个静态方法,使程序外壳的外观和本地操作系统程序的外观一致
	public static void setLookAndFeel()
	{
	     try 
	     {
	         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	         Font font=new Font("圆体",Font.PLAIN,12);
	         String names[]={"Label","CheckBox","PopupMenu","TextPane","Component",
	                        "MenuItem","CheckBoxMenuItem","JRadioButtonMenuItem",
	                        "ComboBox","Button","Tree","ScrollPane","TabbedPane",
	                        "EditorPane","TitledBorder","Menu","TextArea","OptionPane",
	                        "MenuBar","ToolBar","ToggleButton","ToolTip","ProgressBar",
	                        "TableHeader","Panel","List","ColorChooser","PasswordField",
	                        "TextField","Table","Label","Viewport","RadioButtonMenuItem",
	                        "RadioButton"};
	         for(int i=0;i<names.length;i++)UIManager.put(names[i]+".font",font);
	         UIManager.put("Label.foreground",Color.black);
	         UIManager.put("Border.foreground",Color.black);
	         UIManager.put("TitledBorder.titleColor",Color.black);
	     }
	     catch (Exception ex) {}
	}
	

	//定义声音播放的逻辑类
	class SRLogic implements LineListener
	{
		//获得系统路径
		private String path=(System.getProperty("user.dir"));
		
		//声明录音类,用来录音
		private Capture capture=new Capture();
		private SRshell srs;
		
		private boolean isRec=false,isPlay=false;
		

⌨️ 快捷键说明

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