java编写的简单播放器

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;


public class Player extends JApplet{
 AudioClip currentaudio;
 JButton openaudio,playaudio,loopaudio,stopaudio;

 
 public void init(){
  Container container =getContentPane();
  container.setLayout(new FlowLayout());
  ButtonHandler handler=new ButtonHandler();
  openaudio=new JButton("打开");
  playaudio=new JButton("播放");
  loopaudio=new JButton("循环");
  stopaudio=new JButton("停止");
  openaudio.addActionListener(handler);
  playaudio.addActionListener(handler);
  loopaudio.addActionListener(handler);
  stopaudio.addActionListener(handler);
  container.add(openaudio);
  container.add(playaudio);
  container.add(loopaudio);
  container.add(stopaudio);
  
  
  
 }
 
 public void stop(){
  currentaudio.stop();
  
 }
 
 
 class choiceListener implements ItemListener{
  public void itemStateChanged(ItemEvent e){
   currentaudio.stop();
   
   
  }
 }
 
 
 private class ButtonHandler implements ActionListener{
  public void actionPerformed(ActionEvent e){
   if(e.getSource()==openaudio)
    JFileChooser chooser=new JFileChooser();
   chooser.addChoosableFileFilter(new MusicFile());
   int option=chooser.showOpenDialog(this);
   if(option==javax.swing.JFileChooser.APPROVE_OPTION){
    java.io.File file=chooser.getSelectedFile();
    if(file==null) return;
    String str=new String();
    str=chooser.getSelectedFile().getName();
    list.add(str);
    
    try{
     FileInputStream input=new FileInputStream(
       chooser.getSelectedFile().getPath());
     FileOutputStream output=new FileOutputStream(
       "C://eclipse" +
       "//workspace//lc0506401_19//"+
       "hi_hit.mid");

     int in=input.read();
     while(in!=-1){
     output.write(in);
     in=input.read();
     }
     }catch (IOException t){
     System.out.println(t.toString());
     }
     
    System.out.print(chooser.getSelectedFile().getName());
    
     audio=getAudioClip(getDocumentBase(),
      "1.mid");
   }
   else if(e.getSource()==playaudio)
    currentaudio.play();
   
   else if(e.getSource()==loopaudio)
    currentaudio.loop();
   
   else if(e.getSource()==stopaudio)
    currentaudio.stop();
   
  }
 }
 private static class MusicFile extends javax.swing.filechooser.FileFilter{
  public boolean accept(java.io.File file){
   if(file==null)
    return false;
   return file.isDirectory() ||
   file.getName().toLowerCase().endsWith(".mid")||
   file.getName().toLowerCase().endsWith(".wav")||
   file.getName().toLowerCase().endsWith(".au");

   }
  public String getDescription(){
   return "Music files(*.mid,*.mav,*.au)";
  }
 }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值