簡體   English   中英

無法讓JApplet播放音樂mp3文件

[英]Can't get JApplet to play music mp3 file

我正在嘗試讓音樂在JApplet的后台播放。

小程序本身工作正常,但我聽不到任何音樂。

我想知道是否必須將文件設為mp3。

    //AnimationDemo1.java

import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.applet.*;


public class Developers extends JApplet
{
    private AudioClip backgroundmusic;



    public void init()
    {   
        URL urlformusic = getClass().getResource("audio/song1.mp3");
        backgroundmusic = Applet.newAudioClip(urlformusic);
        backgroundmusic.loop();
        add(new DevelopersPanel());
    }
    public void start() {
        backgroundmusic.loop();
    }
    public void stop(){
        backgroundmusic.stop();
        }
    public void destroy() {
        backgroundmusic.stop();
        }
}// end of class of extended JApplet

class DevelopersPanel extends JPanel
{       


    private int numImages = 3;
    private ImageIcon[] loop = new ImageIcon[numImages];
    private String[] description = new String[3];
    private int currentImage = 0;


    public DevelopersPanel()
    {
        description[0] = "Charlie Brown works at Charleston Restraunt" +
            "as a Shift Leader, Server, and Classroom Trainer.";
        description[1] = "Snoopy, well he just does his own thing.";
        description[2] = "Lucy helped keep everyone working on the project sane.";
        for(int x = 0;x<loop.length;x++)
        {
            URL url = this.getClass().getResource("image/pic" + x + ".jpg");
            loop[x] = new ImageIcon(url);
        }

    } //end of constructor

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        Dimension d = getSize();

        g.drawImage(loop[currentImage].getImage(), 10, 10,d.width/2, d.height/2, this);
        g.drawString(description[currentImage],d.width-d.width+20, d.height-20);
        currentImage = (currentImage + 1) % numImages;

        try{
            Thread.sleep(3000);
        }
        catch(InterruptedException e){
        }
        repaint();

    }

} //end of extended JPanel class

任何幫助將不勝感激。

我對Java還是陌生的,請保持簡單。

我想知道是否必須將文件設為mp3。

是的。 Java作為標准僅支持非常有限數量的格式。

要播放MP3,我通常會使用Java Sound,並將MP3服務提供程序接口添加到運行時類路徑。 請參閱Java聲音信息。 頁面以獲取更多詳細信息。

有一個很小且非常易於使用的java庫。 它提供了一個支持mp3,MIDI,wav的聲音播放器。它支持播放單個文件,文件夾和m3u列表。 播放器還具有諸如循環和隨機播放之類的功能,以及其他有用的功能。 您可以從以下網址獲取所有源代碼和api文檔: http : //imr-lib.blogspot.com

暫無
暫無

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

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