⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 musicplayer.java

📁 一个j2me的小游戏
💻 JAVA
字号:
public class MusicPlayer
{

    static final int FORMAT_TONE = 1;
    private InstrumentChannel channel[];

    public MusicPlayer(int instrumentNum)
    {
        try
        {
            channel = new InstrumentChannel[instrumentNum];
        }
        catch(Exception e)
        {
            for(int i = 0; i < channel.length; i++)
            {
                channel[i] = null;
            }

        }
    }

    public void Pause()
    {
        for(int i = 0; i < channel.length; i++)
        {
            if(channel[i] != null)
            {
                channel[i].Pause();
            }
        }

    }

    public void Resume()
    {
        for(int i = 0; i < channel.length; i++)
        {
            if(channel[i] != null)
            {
                channel[i].Resume();
            }
        }

    }

    public void SetTune(int chNum, String string, int soundFormat)
    {
        try
        {
            channel[chNum] = new InstrumentChannel(ConvertHexToBinary(string), soundFormat);
        }
        catch(Exception e) { }
    }

    public int GetInstrumentNumber(int chNum, String string)
    {
        return channel.length;
    }

    public void SetVolume(int level)
    {
        for(int i = 0; i < channel.length; i++)
        {
            channel[i].SetVolume(level);
        }

    }

    public int GetVolume(int chNum)
    {
        return channel[chNum].GetVolume();
    }

    public void Play(int chId, int loop)
    {
        if(channel[chId] != null)
        {
            channel[chId].Stop();
            channel[chId].Play(loop);
        }
    }

    public void Stop(int chId)
    {
        if(channel[chId] != null)
        {
            channel[chId].Stop();
        }
    }

    private static byte[] ConvertHexToBinary(String hexData)
        throws Exception
    {
        if(hexData.length() % 2 != 0)
        {
            throw new Exception("Must be an even number of hex digits");
        }
        byte binaryData[] = new byte[hexData.length() / 2];
        for(int i = 0; i < binaryData.length; i++)
        {
            String byteStr = hexData.substring(i * 2, i * 2 + 2);
            if(byteStr.charAt(0) == '-')
            {
                throw new Exception("Invalid hex digit: -");
            }
            int value;
            try
            {
                value = Integer.parseInt(byteStr, 16);
            }
            catch(NumberFormatException e)
            {
                throw new Exception("Invalid hex digits: " + byteStr);
            }
            binaryData[i] = (byte)value;
        }

        return binaryData;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -