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

📄 soundplay.java

📁 是用JAVA开发的播放器
💻 JAVA
字号:
package skullplay;
import javax.sound.sampled.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;
public class SoundPlay extends Thread
{
	private AudioInputStream fileis,soundis,mp3is;
	private AudioFormat fileaf,soundaf1,soundaf2,soundall;
	private SourceDataLine sourceline;
	private DataLine.Info info=null;
	private File file;
	private float sampleRate;
	private int stopsize,con=1,channels,filesize,datasize,frameposition,oldframe=0,dataposition;
	private byte[] soundbyte;
	private double kbs=0;
	private long secondposition,oldsecond=0;
	private boolean runtype=true;
	PlayEvent pe;
	PlayListener pl;
	private String playevent="END";
	PlayThread pt=new PlayThread();
	public double getKbps()//得到播放速率!
	{
		return kbs;
	}
	public int getDataPosition()//得到播入的当前位置!
	{
		return dataposition;
	}
	public void play(String filepath)//播放!(需要传入文件路径)
	{
		file=new File(filepath);
		this.start();
	}
	public void playback()
	{
		try
		{
			stopsize=soundis.read(soundbyte,0,102400);
			if(stopsize<102400)
			{
				con=2;
			}
		}
		catch (IOException eio7)
		{
			System.out.println("data->byte:I/O出现异常!\n"+eio7);
			System.exit(0);
		}
	}
	public void run()
	{
				try
		{
			fileis=AudioSystem.getAudioInputStream(file);//把文件数据放入声音流里去!
		}
		catch (UnsupportedAudioFileException eus)
		{
			System.out.println("file->audio:你输入的不是有效的音乐文件!\n"+eus);
			System.exit(0);
		}
		catch(IOException eio)
		{
			System.out.println("file->audio:I/O出现错误!\n"+eio);
			System.exit(0);
		}
		catch(Exception e)
		{
			System.out.println("file->audio:未知错误!\n"+e);
			System.exit(0);
		}
		finally
		{
		}
		fileaf=fileis.getFormat();//获得声音流的原始格式!
		sampleRate=fileaf.getSampleRate();
		channels=fileaf.getChannels();
		if(fileaf.isBigEndian())//建立要转换的格式,主要针对MP3!
		{
			soundaf2=new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,sampleRate,8,channels,2,sampleRate,true);
			soundaf1=new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,sampleRate,16,channels,4,sampleRate,true);
		}
		else
		{
			soundaf1=new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,sampleRate,16,channels,4,sampleRate,false);
			soundaf2=new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,sampleRate,8,channels,2,sampleRate,false);
		}
		if(AudioSystem.isConversionSupported(soundaf1,fileaf))//开始转换格式,
		{
			soundis=AudioSystem.getAudioInputStream(soundaf1,fileis);
			soundall=soundaf1;
			soundaf1=null;
			soundaf2=null;
			fileaf=null;
		}
		else
		{
			if(AudioSystem.isConversionSupported(soundaf2,fileaf))
			{
				soundis=AudioSystem.getAudioInputStream(soundaf2,fileis);
				soundall=soundaf2;
				soundaf1=null;
				soundaf2=null;
				fileaf=null;
			}
			else
			{
				System.out.println("音乐文件不能转换!");//WAV格式将不进行转换!
				soundis=fileis;
				soundall=fileaf;
				soundaf1=null;
				soundaf2=null;
				fileaf=null;
			}
		}
		info=new DataLine.Info(SourceDataLine.class,soundall);//建立音频线格式!
		try
		{
			sourceline=(SourceDataLine)AudioSystem.getLine(info);//按以上格式建立音频线!
		}
		catch (LineUnavailableException elua)
		{
			System.out.println("create line:内存得不到控制!\n"+elua);
			System.exit(0);
		}
		catch (SecurityException ese)
		{
			System.out.println("create line:安全得不到控制!\n"+ese);
			System.exit(0);
		}
		catch(IllegalArgumentException eia)
		{
			System.out.println("create line:音乐文件最终是不能转换!\n"+eia);
			System.exit(0);
		}
		catch(Exception e2)
		{
			System.out.println("create line:未知错误!\n"+e2);
			System.exit(0);
		}
		finally
		{
		}
		soundbyte=new byte[204800];
		try
		{
			datasize=soundis.read(soundbyte,0,102400);//预读204800bit的数据!
			datasize+=datasize%4;
			sourceline.open(soundall,30720);//通知音频线将要放入音频数据的格式,并设置缓存!
		}
		catch (IOException eio2)
		{
			System.out.println("data->byte:I/O出现异常!\n"+eio2);
			System.exit(0);
		}
		catch(LineUnavailableException elua1)
		{
			System.out.println("line open:存得不到控制!\n"+elua1);
			System.exit(0);
		}
		catch(IllegalArgumentException eia1)
		{
			System.out.println("line open:缓冲不能放入数据!\n"+eia1);
			System.exit(0);
		}
		catch(IllegalStateException eis)
		{
			System.out.println("line open:以被打开了!\n"+eis);
			System.exit(0);
		}
		catch(SecurityException es1)
		{
			System.out.println("line opne:数据不安全!\n"+es1);
			System.exit(0);
		}
		catch(Exception e1)
		{
			System.out.println("line open:未知错误!\n"+e1);
			System.exit(0);
		}
		finally
		{
		}
		if(sourceline.isOpen())
		{
			sourceline.start();//打开音频线
			try
			{
				if(runtype)
				{
					pt.start();
				}

				System.gc();
			}
			catch (IllegalThreadStateException eits)
			{
				System.out.println("thread start:线程已经开始!\n"+eits);
			}
			do
			{
				sourceline.write(soundbyte,0,datasize);//往缓存里写入数据!
				playback();//读完soundbyte的数据后,再到playback()方法里再进行读取数据!
			}while(con==1);
			pe=new PlayEvent(playevent);
			firePlayEvent(pe);
		}
		else
		{
			System.out.println("it can not open!");
			System.exit(0);
		}
	}
	public void firePlayEvent(PlayEvent pe)
	{
		pl.validatePlay(pe);
	}
	public void addPlayListener(PlayListener pl)
	{
		this.pl=pl;
	}
	public void end()
	{
		sourceline.close();
	}
	class PlayThread extends Thread
	{
		public PlayThread()
		{}
		public void run()
		{
			runtype=false;
		for (; ; )
		{
			sourceline.drain();
			System.gc();
			frameposition=sourceline.getFramePosition();
			secondposition=sourceline.getMicrosecondPosition();
			dataposition=frameposition*4;
			if((secondposition-oldsecond)/1000000==0)
			{
			}
			else
			{
				kbs=(((frameposition-oldframe)*4)/1024)/((secondposition-oldsecond)/1000000);
				oldframe=frameposition;
				oldsecond=secondposition;
			}
			try
			{
				this.sleep(1000);
			}
			catch (InterruptedException eipt)
			{
				System.out.println("sleep:不能转为睡眠!\n"+eipt);
			}
		}
		}
	};
};

⌨️ 快捷键说明

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