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

📄 sound.java~10~

📁 JBuilder实现的具有单机
💻 JAVA~10~
字号:
package mymatrix;

import java.io.*;
import javax.sound.sampled.*;
import javax.sound.midi.*;

public class Sound {
  String m_SoundName;
  Clip clip;

  Sequencer sequencer;

  public Sound(String SoundName) {
  m_SoundName=SoundName;
  jsInit();

  }

  public Sound(String name,int i){
    this.Playmimd(name);
  }

  private void jsInit(){
  try{
    File file = new File(m_SoundName);
    AudioInputStream stream = AudioSystem.getAudioInputStream(file);
    AudioFormat format = stream.getFormat();
    // specify what kind of line we want to create
    DataLine.Info info = new DataLine.Info(Clip.class, format);
    // create the line
    clip = (Clip)AudioSystem.getLine(info);
    // load the samples from the stream
    clip.open(stream);
    clip.start();

    }
  catch(Exception se){
    se.printStackTrace();
  }
 }

 public void PlayMp3(String name){
   // create the format used for playback
// (uncompressed, 44100Hz, 16-bit, mono, signed, little-endian)
AudioFormat playbackFormat =
    new AudioFormat(44100, 16, 1, true, false);

// open the source file
    try{
      AudioInputStream source =
      AudioSystem.getAudioInputStream(new File(name));

      // convert to playback format
      source = AudioSystem.getAudioInputStream(playbackFormat, source);

      AudioFormat format = source.getFormat();
    // specify what kind of line we want to create
     DataLine.Info info = new DataLine.Info(Clip.class, format);
    // create the line
    clip = (Clip)AudioSystem.getLine(info);
    // load the samples from the stream
    clip.open(source);
    clip.start();

    }
    catch(Exception ex){
      ex.printStackTrace();
    }


 }

 public void Playmimd(String filename){

   try{
     // open the midi file
    Sequence sequence = MidiSystem.getSequence(new File(filename));
   // open the sequencer
    sequencer = MidiSystem.getSequencer();
    sequencer.open();
   // play the midi sequence
    sequencer.setSequence(sequence);




   }
   catch(Exception ex){
     ex.printStackTrace();
   }

}

  public void start(){
    clip.start();
  }
  public void start(int i){
    // begin playback of the sound clip
    sequencer.start();
  }

  public void stop(){
    sequencer.stop();
  }

  public void loop(int LoopTime){
    clip.loop(LoopTime);
  }

  public void pause(){
    try{
      sequencer.wait();
    }
    catch(Exception pe){
      pe.printStackTrace();
    }
  }

  public void continue_play(){
     sequencer.notify();
  }

}

⌨️ 快捷键说明

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