如何用Java写一个原生wav播放器

MusicPlayer.java
package musicplayer;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

/**
 *
 * @author Chandler
 */
public class MusicPlayer extends JFrame{
    
    private Container container;
    private JButton playbutton;
    private PlayBackThread playbackthread;
    //默认构造函数
    public MusicPlayer(String title){
        super(title);
        container = this.getContentPane();
        playbutton = new JButton("播放");
        playbutton.setSize(50,20);
        playbutton.addActionListener(new PlayActionListener());
        this.setLayout(new BorderLayout());
        this.setSize(500,400);
        container.add(playbutton,BorderLayout.CENTER);
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        int x = (int)(toolkit.getScreenSize().getWidth()-this.getWidth())/2;
        int y = (int)(toolkit.getScreenSize().getHeight()-this.getHeight())/2;
        this.setLocation(x,y);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        MusicPlayer musicplayer = new MusicPlayer("MusicPlayer");
    }
    class PlayActionListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println(e.getActionCommand());
            playbackthread = new PlayBackThread();
            playbackthread.start();
        }
    }
}

PlayBackThread.java

package musicplayer;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.LineUnavailableException;

import java.io.RandomAccessFile;

import java.util.Scanner;

/**
 *
 * @author Chandler
 */
public class PlayBackThread extends Thread{
    private SourceDataLine dataline;
    private final int dataOffset = 0x2e;
    public PlayBackThread(){
        super("playbackthread");
    }
    
    @Override
    public void run(){
        try{
           RandomAccessFile raf = new RandomAccessFile("C:\\Users\\Chandler\\Documents\\NetBeansProjects\\MusicPlayer\\src\\musicplayer\\John Denver - Country Roads.wav","r");
           AudioFormat af;
           af = new AudioFormat(44100,16,2,true,false);
           dataline = (SourceDataLine)AudioSystem.getSourceDataLine(af);
           dataline.open(af);
           raf.seek(dataOffset);
           int hasRead=0;
           dataline.start();
           byte[] buff=new byte[4096];
           Scanner input = new Scanner(System.in);
           while((hasRead=raf.read(buff))>0){
//             print(raf.getFilePointer(),buff);
               dataline.write(buff, 0, hasRead);
           }
           dataline.stop();
        } catch(Exception e){
            e.printStackTrace();
        }
    }
        public static void print(long pointer, byte[] buff){
            System.out.format("%x: " ,pointer);
            System.out.format("%x ", buff[0]);
            System.out.format("%x ", buff[1]);
            System.out.format("%x ", buff[2]);
            System.out.format("%x ", buff[3]);
            
            System.out.println();
    }
}

John Denver - Country Roads.wav



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值