簡體   English   中英

如何讀取音頻文件? 我應該使用哪種方法?

[英]How to read an audio file? Which method should I use?

我有一個帶有2個按鈕的面板。 當我單擊按鈕1時,我只想讀取一個音頻文件(在這種情況下為.WAV)。 然后,當我單擊按鈕2時,我想停止播放音樂。

我做了一些研究,但是對於不同的方法我有些困惑。 就我而言,哪一個最好? 有人可以解釋一下AudioClip,JavaSound和JavaMediaFramework之間的區別嗎?

我也嘗試了一個示例,但其中包含錯誤。

這是我的Main.class

import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class Main
{
    public static void main(String[] args) 
    {
           SoundPlayer player = new SoundPlayer("C:/Documents and Settings/All Users/Documents/Ma musique/Échantillons de musique/Symphonie n° 9 de Beethoven (scherzo).wma");
           InputStream stream = new ByteArrayInputStream(player.getSamples()); 
           player.play(stream);
    }
}

這是我的SoundPlayer.class

import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.sound.sampled.*;

public class SoundPlayer 
{
    private AudioFormat format;
    private byte[] samples;
    /**
     * 
     * @param filename le lien vers le fichier song (URL ou absolute path)
     */
    public SoundPlayer(String filename)
    {
        try
        {
            AudioInputStream stream = AudioSystem.getAudioInputStream(new File(filename));
            format = stream.getFormat();
            samples = getSamples(stream);
        }
        catch (UnsupportedAudioFileException e){e.printStackTrace();}
        catch (IOException e){e.printStackTrace();}
    }

    public byte[] getSamples()
    {
        return samples;
    }

    public byte[] getSamples(AudioInputStream stream)
    {
        int length = (int)(stream.getFrameLength() * format.getFrameSize());
        byte[] samples = new byte[length];
        DataInputStream in = new DataInputStream(stream);
        try
        {
            in.readFully(samples);
        }
        catch (IOException e){e.printStackTrace();}
        return samples;
    }

    public void play(InputStream source)
    {
        int bufferSize = format.getFrameSize() * Math.round(format.getSampleRate() / 10);
        byte[] buffer = new byte[bufferSize];
        SourceDataLine line;
        try
        {
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
            line = (SourceDataLine)AudioSystem.getLine(info);
            line.open(format, bufferSize);
        }
        catch (LineUnavailableException e)
        {
            e.printStackTrace();
            return;
        }
        line.start();

        try
        {
            int numBytesRead = 0;
            while (numBytesRead != -1)
            {
                numBytesRead = source.read(buffer, 0, buffer.length);
                if (numBytesRead != -1)
                    line.write(buffer, 0, numBytesRead);
            }
        }
        catch (IOException e){e.printStackTrace();}

        line.drain();
        line.close();
    }
}

LOGCAT

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
    at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
    at SoundPlayer.<init>(SoundPlayer.java:19)
    at Main.main(Main.java:8)
Exception in thread "main" java.lang.NullPointerException
    at java.io.ByteArrayInputStream.<init>(Unknown Source)
    at Main.main(Main.java:9)

提前,非常感謝!

該例外將保留。 * .wma文件不受標准支持。

最簡單的解決方案是使用* .wav文件或其他受支持的文件

您可以獲取更多信息:

https://stackoverflow.com/tags/javasound/info

SoundPlayer player = new SoundPlayer("C:/Documents and Settings/All Users/" + 
  "Documents/Ma musique/Échantillons de musique/" + 
   "Symphonie n° 9 de Beethoven (scherzo).wma")

啊,WMA。 很棒的格式,Java(標准版)不提供支持它的服務提供者接口。

您將需要提供一個SPI以允許Java Sound支持它,或者使用其他API。 我不知道任何支持WMA的API。 您可以用其他格式編碼嗎?

請參閱Java聲音信息。 以支持MP3,但是它需要JMF的MP3 SPI。

寫下您的音樂文件的完整路徑,它將起作用

我已經找到解決問題的辦法。 就我而言,使用JAVAZOOM庫是很好的。

這是一個示例,僅在啟動時播放音頻文件(無圖形部分)

public class Sound 
{   
    private boolean isPlaying = false;
    private AdvancedPlayer player = null;

    public Sound(String path) throws Exception 
    {
        InputStream in = (InputStream)new BufferedInputStream(new FileInputStream(new File(path)));
        player = new AdvancedPlayer(in);
    }

    public Sound(String path,PlaybackListener listener) throws Exception 
    {
        InputStream in = (InputStream)new BufferedInputStream(new FileInputStream(new File(path)));
        player = new AdvancedPlayer(in);
        player.setPlayBackListener(listener);
    }

    public void play() throws Exception
    {
        if (player != null) 
        {
            isPlaying = true;
            player.play();
        }
    }

    public void play(int begin,int end) throws Exception 
    {
        if (player != null) 
        {
            isPlaying = true;
            player.play(begin,end);
        }
    }

    public void stop() throws Exception 
    {
        if (player != null)
        {
            player.stop();
            isPlaying = false;

        }
    }

    public boolean isPlaying() 
    {
        return isPlaying;
    }

    public static void main(String[] args) 
    {
        System.out.println("lecture de son");
        try 
        {
            Sound sound = new Sound("C:/Documents and Settings/cngo/Bureau/Stage-Save/TCPIP_AndroidJava/TCPIP_V6_Sound/OpeningSuite.mp3");
            System.out.println("playing : " + sound.isPlaying());
            sound.play();
            System.out.println("playing : " + sound.isPlaying());
        } 
        catch (Exception e){e.printStackTrace();}
    }
}

感謝@ murtaza.webdev的回答!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM