The constructor RandomAccessFile(File, String) is undefined;明明有这个构造函数啊

System.out.println(Thread.currentThread().getName()+"启动。。。");BufferedInputStreambis=n... System.out.println(Thread.currentThread().getName()+"启动。。。");
BufferedInputStream bis=null;
RandomAccessFile ras=null;
byte[] buf=new byte[BUFFER_SIZE];
URLConnection con=null;
try{
con=srcURL.openConnection();
con.setRequestProperty("Range", "byte="+start+"-"+end);
bis=new BufferedInputStream(con.getInputStream());
ras=new RandomAccessFile(destFile, "rw");

其中一段代码,最后一句,在Eclipse中一直报错:The constructor RandomAccessFile(File, String) is undefined;
其中destFile的声明:private final File destFile;构造函数public DownloadRunnable(int start,int end,URL srcURL,File destFile);
我再去看RandomAccessFile的源码,明明有RandomAccessFile(File file,String mode)这个构造函数啊,而且在Eclipse中alt+/也能看到这个构造函数,可是写出来就是报这个错,怎么回事?
还有啊,上面主程序有这句代码:File destFile=new File("d:\\java\\test\\国歌.map3");Eclipse也报错:The constructor File(String) is undefined;A源代码明明也有这个构造函数:File(String pathname)

通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。怎么他有报错了?
展开
 我来答
發呆碼字員
2014-10-27 · TA获得超过331个赞
知道小有建树答主
回答量:435
采纳率:0%
帮助的人:265万
展开全部
你是要播放音源档??
播放音乐的话最好是相关设定都要设定会比较好
以前也有播放音乐有时正常有时不正常,所以最好是可以建立一个完整的音源档使用!!

package wav;
import javax.sound.sampled.*;

import java.io.*;
import java.net.*;

/**
* 播放音乐(支援WAV, AIFF, AU) 2011/10/09
*
* 2012/12/08
* 1.增加播放结束时callback
* 2.修正bug: 无限次播放时,无法stop()
*
* @version 2
* @author Ray(吉他手)
*/
public class AudioPlayer{
private AudioInputStream currentSound;

private Clip clip;

private float gain;
private FloatControl gainControl;

//控制声道,-1.0f:只有左声道, 0.0f:双声道,1.0f右声道
private float pan;
private FloatControl panControl;

//控制静音 开/关
private boolean mute;
private BooleanControl muteControl;

//播放次数,小於等於0:无限次播放,大於0:播放次数
private int playCount;

private DataLine.Info dlInfo;
private Object loadReference;
private AudioFormat format;

//音乐播放完毕时,若有设定回call的对象,则会通知此对象
private AudioPlayerCallback callbackTartet;
private Object callbackObj ;
private boolean isPause;

public AudioPlayer(){
AudioPlayerInit();
}

public void AudioPlayerInit(){
currentSound = null;
clip = null;
gain = 0.5f;
gainControl = null;
pan = 0.0f;
panControl = null;
mute = false;
muteControl = null;
playCount = 0;
dlInfo = null;
isPause = false;
}

/**
* 设定要接收音乐播放完时事件的对象
* @param cb 接收callback的对象
* @param obj callback回来的物件
*/
public void setCallbackTartet(AudioPlayerCallback cb, Object obj){
callbackTartet = cb;
callbackObj = obj;
}

/**
* 设定播放次数,播放次数,小於等於0:无限次播放,大於0:播放次数
* @param c
*/
public void setPlayCount(int c){
if(c < -1){
c = -1;
}
playCount = c - 1;
}

/**
* 指定路径读取音档,回传true:播放成功,false:播放失败
* @param filePath
* @param obj 目前物件放置的package路径
*/
public boolean loadAudio(String filePath){
try {
loadAudio(new File(filePath));
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

/**
* 指定路径读取音档,使用目前物件放置的package当相对路径root,null时不使用物件路径为root
* @param filePath
* @param obj 目前物件放置的package路径
* @return 回传true:播放成功,false:播放失败
*/
public boolean loadAudio(String filePath, Object obj){
try {
if(obj != null){
loadAudio(obj.getClass().getResourceAsStream(filePath));
}else{
loadAudio(new File(filePath));
}
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

/**
* 从远端读取音档
*/
public void loadAudio(URL url) throws Exception{
loadReference = url;
currentSound = AudioSystem.getAudioInputStream(url);
finishLoadingAudio();
}

/**
* 读取本地端音档
* @param file
* @throws Exception
*/
public void loadAudio(File file) throws Exception{
loadReference = file;
currentSound = AudioSystem.getAudioInputStream(file);
finishLoadingAudio();
}

/**
* 从串流读取音档
* @param iStream
* @throws Exception
*/
public void loadAudio(InputStream iStream) throws Exception{
loadReference = iStream;

if (iStream instanceof AudioInputStream){
currentSound = (AudioInputStream)iStream;
} else {
currentSound = AudioSystem.getAudioInputStream(iStream);
}
finishLoadingAudio();
}

/**
* load完音档後,进行播放设定
*/
protected void finishLoadingAudio() throws Exception {
format = currentSound.getFormat();
dlInfo = new DataLine.Info(Clip.class, format, ((int) currentSound.getFrameLength() * format.getFrameSize()));
clip = (Clip) AudioSystem.getLine(dlInfo);
clip.open(currentSound);
clip.addLineListener(
new LineListener() {
public void update(LineEvent event) {
if (event.getType().equals(LineEvent.Type.STOP)){
if(!isPause){
if(callbackTartet != null){
callbackTartet.audioPlayEnd(callbackObj);
}
close();
}
}
}
}
);
}

/**
* 播放音档
*/
public void play(){
if(clip != null){
clip.setFramePosition(0);
clip.loop(playCount);
}
}

/**
* 恢复播放音档
*
*/
public void resume(){
isPause = false;

if(clip != null){
clip.setFramePosition(clip.getFramePosition());
clip.loop(playCount);
}

}

/**
* 暂停播放音档
*/
public void pause(){
isPause = true;
if(clip != null){
clip.stop();
}
}

/**
* 停止播放音档,且将音档播放位置移回开始处
*/
public void stop(){
if(clip != null){
clip.stop();
}
}

/**
* 设定音量
* @param dB 0~1,预设为0.5
*/
public void setVolume(float dB){
float tempB = floor_pow(dB,1);
//System.out.println("目前音量+"+tempB);
gain = tempB;
resetVolume();

}

/**
* @param min 要无条件舍去的数字
* @param Num 要舍去的位数
*
*/
private float floor_pow(float min, int Num){
float n = (float)Math.pow(10, Num);
float tmp_Num = ((int)(min*n))/n;
return tmp_Num ;
}

/**
* 重设音量
*/
protected void resetVolume(){
gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
//double gain = .5D; // number between 0 and 1 (loudest)
float dB = (float) (Math.log(gain) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
}

/**
* 设定声道,-1.0f:只有左声道, 0.0f:双声道,1.0f右声道
* @param p
*/
public void setPan(float p){
pan = p;
resetPan();
}

/**
* 重设单双道、双声道
*/
protected void resetPan(){
panControl = (FloatControl) clip.getControl(FloatControl.Type.PAN);
panControl.setValue(this.pan);
}

/**
* 设定静音状态,true:静音,false:不静音
* @param m
*/
public void setMute(boolean m){
mute = m;
resetMute();
}

/**
* 重设静音状态
*
*/
protected void resetMute(){
muteControl = (BooleanControl) clip.getControl(BooleanControl.Type.MUTE);
muteControl.setValue(mute);
}

/**
*
* @return
*/
public int getFramePosition(){
try {
return clip.getFramePosition();
} catch (Exception e) {
return -1;
}
}

/**
* 取得音档格式
* @return
*/
public AudioFormat getCurrentFormat(){
return format;
}

/**
* 取得音档的串流
* @return
*/
public AudioInputStream getAudioInputStream(){
try {
AudioInputStream aiStream;

if (loadReference == null){
return null;
} else if (loadReference instanceof URL) {
URL url = (URL)loadReference;
aiStream = AudioSystem.getAudioInputStream(url);
} else if (loadReference instanceof File) {
File file = (File)loadReference;
aiStream = AudioSystem.getAudioInputStream(file);
} else if (loadReference instanceof AudioInputStream){
AudioInputStream stream = (AudioInputStream)loadReference;
aiStream = AudioSystem.getAudioInputStream(stream.getFormat(), stream);
stream.reset();
} else {

InputStream inputStream = (InputStream)loadReference;
aiStream = AudioSystem.getAudioInputStream(inputStream);
}

return aiStream;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 目前音档是否已存在
* @return
*/
public boolean isAudioLoaded(){
return loadReference!= null;
}

/**
* 取得剪辑音档
* @return
*/
public Clip getClip() {
return clip;
}

/**
* 关闭音档
*/
public void close(){
try {
if (clip != null)
clip.close();
if (currentSound != null)
currentSound.close();
loadReference = null;
} catch (Exception e){
//System.out.println("unloadAudio: " + e);
e.printStackTrace();
}

currentSound = null;
clip = null;
gainControl = null;
panControl = null;
dlInfo = null;
loadReference = null;
muteControl = null;
callbackTartet = null;
callbackObj = null;
}

}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式