2011-10-06 108 views
1

我的代碼有問題,每次運行項目時都會拋出這個問題。現在我已經縮小了它的範圍,當我調用play()方法時播放聲音,但當我調用playL()方法時播放聲音。無法創建AudioData對象?

package net.chrypthic.Ball; 
import sun.audio.*; 
import java.io.*; 

public class SoundManager { 

AudioPlayer ap = AudioPlayer.player; 
AudioStream as; 
ContinuousAudioDataStream loop = null; 
public SoundManager(String music) 
{ 
    try 
    { 
     InputStream input = new FileInputStream("./"+music); 
     as = new AudioStream(input); 
     AudioData ad = as.getData(); 
     loop = new ContinuousAudioDataStream(ad); 
    }catch(Exception e) 
    { 
     System.out.println(e.getMessage()); 
    } 
} 
public void play() 
{ 
    ap.start(as); 
} 
public void stop() 
{ 
    ap.start(as); 
} 
public void playL() 
{ 
    ap.start(loop); 
} 
public void stopL() 
{ 
    ap.start(loop); 
} 
} 

爲什麼?我將sound/gsong1b.wav傳遞給它,它的大小爲6.2MB,長度爲2分鐘,比特率爲16000Hz。 我聽說聲音必須小於4MB大,但是它扮演,而當我環.... 任何幫助,將不勝感激僅錯誤。

+0

有沒有人可以解決這個問題? – chrypthic

回答

2

您使用(AudioPlayer,語音串流)這些類,即使他們是來自官方的Java JDK,其實都是保留類,這意味着甲骨文公司(和Sun之前)保留更改它們,恕不另行通知。您應該使用官方聲音API代替:

import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.Clip; 

//... 

public static void main(String[] args) throws Throwable { 
     Clip clip = AudioSystem.getClip(); 
     AudioInputStream inputStream = AudioSystem.getAudioInputStream(SoundManager.class.getResourceAsStream("C://temp/my.mp3")); 
     clip.open(inputStream); 
     clip.start(); 
    } 
+0

感謝您的快速回復,現在它拋出javax.sound.sampled.LineUnavailableException:無法分配剪輯數據:請求的緩衝區太大。有任何想法嗎? – chrypthic

+0

我是否必須更改緩衝區大小,以及如果我該怎麼做? – chrypthic

+0

這對我來說也是一種困惑,我很害怕。這可能是因爲你的聲卡驅動程序,嘗試更新這些驅動程序,或者它可能是你的聲音文件,就Java而言並不好。如果是後者,我擔心你自己,嘗試使用Google搜索一下... –

0

請嘗試使用HeadspaceMixer代替。 javax.sound不是一個完整的實現。

+0

這是否播放.wav文件? – chrypthic

0

使用您聲音按鈕的IDE Goto作用進行metthod。 用於連續播放.wav文件。我使用下面的代碼,它使用線程正常工作。確保你輸入了下面的v。導入sun.audio。 ;並導入java.io.;

 Thread sound; 
     sound = new Thread(){ 

      public void run(){ 

      AudioPlayer MGP=AudioPlayer.player; 
      AudioStream BGM; 
      AudioData MD; 
      ContinuousAudioDataStream loop=null; 
    for(;;){ 

     try{ 
     BGM=new AudioStream(new FileInputStream("C:\\Users\\HAMMED\\01FATIHA   (New).wav"));//enter the sound directory and name here 
     AudioPlayer.player.start(BGM); 

     //MD=BGM.getData();//not necessary 
     //loop=new ContinuousAudioDataStream(MD);//not necessarry 

     sleep(48000);// enter the elapse time of ur sond to avoid noise 
    }catch(Exception e){ 

     JOptionPane.showMessageDialog(null, e); 
    } 


    MGP.start(loop);// It does nothing.I was trying to use this but no success. 
     } 
    } 
    }; 
    sound.start();