java 소리 재생 AudioStream.getData()이상

4248 단어 JavaSUN.netHTMLBlog
더 읽 기
  최근 에 작은 애플 리 케 이 션 을 쓰 고 자바 가 소 리 를 재생 하 는 라 이브 러 리 를 사 용 했 습 니 다.인터넷 에 많은 예 가 있어 서 하 나 를 찾 았 습 니 다.
 
 
code from http://dracularking.iteye.com/blog/738917 
 
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;

public class MusicPlay {
	private AudioStream as; //        
	ContinuousAudioDataStream cas;//       

	//     
	public MusicPlay(URL url) {
		try {
			//              
			as = new AudioStream(url.openStream());
		} catch(FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch(IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	//        
	public void start() {
		if(as == null) {
			System.out.println("AudioStream object is not created!");
			return;
		} else {
			AudioPlayer.player.start(as);
		}
	}

	//        
	public void stop() {
		if(as == null) {
			System.out.println("AudioStream object is not created!");
			return;
		} else {
			AudioPlayer.player.stop(as);
		}
	}

	//        
	public void continuousStart() {
		// Create AudioData source.
		AudioData data = null;
		try {
			data = as.getData();
		} catch(IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// Create ContinuousAudioDataStream.
		cas = new ContinuousAudioDataStream(data);

		// Play audio.
		AudioPlayer.player.start(cas);
	}

	//        
	public void continuousStop() {
		if(cas != null) {
			AudioPlayer.player.stop(cas);
		}
	}

}

 
불 행 히 도 사용 중 순환 재생 을 사용 할 때 Continuous AudioDataStream 클래스 를 만 들 때 data=as.getData()라 는 문장 에 이상 java.io.IOException:could not create AudioData object 가 나 타 났 습 니 다.
 
인터넷 에서 많은 자 료 를 찾 아 보 았 지만 해결 되 지 않 았 습 니 다.오디 오 파일 형식 이 틀 렸 다 고 합 니 다.라 이브 러 리 에 문제 가 있다 고 합 니 다.그런데 해결 되 지 않 았 습 니 다.나중에 AudioStream 류 소스 파일 의 getData 함 수 를 살 펴 본 결과 getData()는 오디 오 파일 의 길이 에 제한 이 있 고 1M 를 초과 할 수 없 으 며 원인 을 찾 았 습 니 다.Nd 라 는 원인 으로 인 한 이상 은 정말 비극 적 입 니 다.인터넷 에서 이런 상황 을 전혀 찾 을 수 없어 요.자바 독 에서 도 설명 이 없어 요...
 
http://www.docjar.com/html/api/sun/audio/AudioStream.java.html
  102       public AudioData getData() throws IOException {
  103           int length = getLength();
  104   
  105           //limit the memory to 1M, so too large au file won't load
  106           if (length < 1024*1024) {
  107               byte [] buffer = new byte[length];
  108               try {
  109                   ais.read(buffer, 0, length);
  110               } catch (IOException ex) {
  111                   throw new IOException("Could not create AudioData Object");
  112               }
  113               return new AudioData(format, buffer);
  114           }
  115   
  116           /*              acis.setData();
  117   
  118                           if (acis.stream instanceof ByteArrayInputStream) {
  119                           Format[] format = acis.getFormat();
  120                           byte[] bytes = acis.getBytes();
  121                           if (bytes == null)
  122                           throw new IOException("could not create AudioData object: no data received");
  123                           return new AudioData((AudioFormat)format[0], bytes);
  124                           }
  125           */
  126   
  127           throw new IOException("could not create AudioData object");
  128       }

 또한 소 리 를 재생 하 는 코드 에 문제 가 있 으 므 로 링크 를 참고 해 야 합 니 다.http://www.java2s.com/Code/JavaAPI/sun.audio/newAudioStreamInputStreamarg0.htm

좋은 웹페이지 즐겨찾기