qq_24840453 2014-12-27 03:48 采纳率: 0%
浏览 6215

怎么实现java按钮的播放与暂停,按一次就是暂停,按一次就是播放?

import java.awt.EventQueue;
import java.io.*;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.JButton;
import javax.media.*;
import java.applet.*;
import jmapps.ui.JMFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JSpinner;
import javax.swing.JComboBox;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.*;
import jmapps.jmstudio.CaptureDialog;
import jmapps.ui.PlayerFrame;
import jmapps.util.CDSWrapper;
import jmapps.util.JMFUtils;
public class bofangqi extends Applet implements ControllerListener{

private JFrame frame;
//播放对象
    private Player player;
    //是否循环播放
    private boolean first,loop;
    //文件路径
    private String path;
    //存放MP3文件
    private List<String> mp3List;
    //当前MP3文件数量
    private int mp3NO=0;
    public boolean i=true;

    bofangqi(List<String> mp3List)
    {
        this.mp3List=mp3List;
    }
/**
 * Launch the application.
 */
    //播放方法
    public void start()
    {
        try {
            player = Manager.createPlayer(new File(mp3List.get(mp3NO)).toURI().toURL());
        } catch (NoPlayerException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
            System.out.println("不能播放此文件!");
            return;
        } catch (IOException e) {
            // TODO 自动生成 catch 块
            e.printStackTrace();
            return;
        } 
        if(player==null)
        {
            System.out.println("播放文件为空!");
            return;
        }
        player.addControllerListener(this);
        //提取媒体内容
        player.prefetch();
    }

public void controllerUpdate(ControllerEvent e) {
        //当媒体播放结束时,循环播放
        if(e instanceof EndOfMediaEvent)
        {
            mp3NO++;
            System.out.println(mp3NO);
            if(mp3NO<mp3List.size())
            {

                this.start();
            }
            return;
        }

            //当提取媒体的内容结束 
        if (e instanceof PrefetchCompleteEvent) { 
            System.out.println("内容结束");
            player.start(); 

            return; 
        } 

            //当实例化后 
        if (e instanceof RealizeCompleteEvent) { 
            System.out.println("实例化");
            //pack(); //执行pack()操作 
            return; 
        } 

}
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                bofangqi window = new bofangqi();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    List<String> path=new ArrayList<String>();
    path.add("C:\\Users\\Administrator.P5LPMC6WDCYRLP5\\Music\\无言无语.mp3");
    path.add("C:\\Users\\Administrator.P5LPMC6WDCYRLP5\\Music\\美丽之景.mp3");
    path.add("C:\\Users\\Administrator.P5LPMC6WDCYRLP5\\Music\\你的名字我的姓氏.mp3");
    bofangqi play=new bofangqi(path);
    play.start();


}

/**
 * Create the application.
 */
public bofangqi() {
    initialize();
}

/**
 * Initialize the contents of the frame.
 */
public void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 382, 361);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(null);


    JButton btnNewButton = new JButton("\u524D\u4E00\u9996");
    btnNewButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            if(mp3NO<0)
            {
            mp3NO=mp3List.size()-1;

            }
            else{
                mp3NO--;

            }
        }
    });
    btnNewButton.setBounds(27, 64, 93, 23);
    frame.getContentPane().add(btnNewButton);

    final JButton btnNewButton_1 = new JButton("\u6682\u505C");
    btnNewButton_1.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

            if(i==true) {btnNewButton_1.setText("播放");
             i=false;
             player.notify();
            }
            else {btnNewButton_1.setText("暂停");
            i=true;}
        }
    });
    btnNewButton_1.setBounds(130, 64, 93, 23);
    frame.getContentPane().add(btnNewButton_1);

    JButton btnNewButton_2 = new JButton("\u4E0B\u4E00\u9996");
    btnNewButton_2.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
        }
    });
    btnNewButton_2.setBounds(235, 64, 93, 23);
    frame.getContentPane().add(btnNewButton_2);
}

}

  • 写回答

2条回答 默认 最新

  • mi_liang8023 2014-12-27 05:34
    关注

    统计点击次数呗,或者做一个TAG记录播放还是暂停状态,这个应该很好解决吧

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算