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

📄 mp3ecoding.java

📁 一款Java播放器的源代码
💻 JAVA
字号:
import javax.swing.*; 
import java.io.*; 
import javax.sound.sampled.*; 
import javax.sound.midi.*; 
import java.awt.*; 
import java.awt.event.*; 
import javax.sound.sampled.AudioFormat.Encoding; 
import java.util.*; 
//高效设计:如果要设计一个要在其它类作为线程的类,最好把此类设计为Runnable型 
//不要在此类中调用线程,最好把此类作为一个Runnable接口使用 
public class Mp3Ecoding implements Runnable 
{ 
String filename; 
AudioInputStream stream; 
SourceDataLine playLine; 
AudioFormat baseFormat; 
long currentByte = 0; 
boolean isPause; 
int numThread = 0; 
int numBytesRead = -1; 
int bufferSize; 
private FileInputStream in; 
public Mp3Ecoding() 
{ 

//currentByte = bufferSize; 
} 
private void getFormat(String filename) 
{ 

try 
{ 
//in = new FileInputStream(new File(filename)); 
//AudioFileFormat af = mt.getAudioFileFormat(in); 
stream = AudioSystem.getAudioInputStream(new File(filename)); 
} 
catch(Exception e) 
{ 
e.printStackTrace(); 
} 
baseFormat = stream.getFormat(); 
int firstExtendnameIndex = filename.lastIndexOf("."); 
String extendStr = filename.substring(firstExtendnameIndex+1); 
if(!extendStr.equalsIgnoreCase("wav")) 
{ 
System.out.println(String.valueOf(extendStr.equalsIgnoreCase("wav"))); 
baseFormat = decodingMusic(baseFormat); 
} 



} 
private AudioFormat decodingMusic(AudioFormat format) 
{ 
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
format.getSampleRate(), 
format.getSampleSizeInBits(), 
format.getChannels(), 
format.getFrameSize(), 
format.getFrameRate(), 
format.isBigEndian()); 
return decodedFormat; 


} 
private SourceDataLine getLine(AudioFormat f) 
{ 
RandomAccessFile source = null; 
try{ 
source = new RandomAccessFile(filename,"r"); 
}catch(Exception e){} 
SourceDataLine line = null; 
DataLine.Info info = new DataLine.Info(SourceDataLine.class,f); 
bufferSize = baseFormat.getFrameSize() * 
Math.round(baseFormat.getSampleRate() / 10); 
byte[] buffer = new byte[bufferSize]; 
try 
{ 
line = (SourceDataLine)AudioSystem.getLine(info); 
line.open(f, bufferSize); 
} 
catch (LineUnavailableException ex) 
{ 
ex.printStackTrace(); 
} 
line.start(); 

// copy data to the line 
try { 

//do{ 
if((numBytesRead == -1)&&(MusicFrame.getIsPlay() == true)) 

{ 
currentByte = buffer.length; 
numBytesRead = 0; 
} 
source.seek(currentByte); 

while ((numBytesRead != -1)&&(MusicFrame.getIsPause()!=true)) { 
numBytesRead = source.read(buffer, 0, buffer.length); 
currentByte += numBytesRead; 
if (numBytesRead != -1) { 
line.write(buffer, 0, numBytesRead); 
} 
} 
if(MusicFrame.getIsStop() == true) currentByte = buffer.length; 
//}while((MusicFrame.getIsCycle() == true)&&(numBytesRead == -1)); 
} 
catch (Exception ex) { 
ex.printStackTrace(); 
} 

return line; 
} 

public void run() 
{ 

play(); 
} 
public synchronized void play() 
{ 
setFilename(filename); 
getFormat(filename); 

playLine = getLine(baseFormat); 
//playLine.drain();//write会自动调用drain方法 
if((MusicFrame.getIsCycle() == true)&&(MusicFrame.getIsPlay() == true)) 
{ 
Random f = new Random(); 
int index = f.nextInt(MusicFrame.getNameList().size()); 

filename = (String)MusicFrame.getNameList().getElementAt(index); 

play(); 
} 
playLine.close(); 


//numThread = 0; 
} 
public void setFilename(String name) 
{ 
if(name == null) 
{ 
name = MusicFrame.getFilename(); 
} 
filename = name; 
} 



}

⌨️ 快捷键说明

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