j_audio.java.bak

来自「一个十分好的java基础学习的课件」· BAK 代码 · 共 80 行

BAK
80
字号
// ////////////////////////////////////////////////////////
// 
// J_Audio.java
// 
// Created by Jun-Hai Yong,    on XX xx, xxxx (Date)
// ////////////////////////////////////////////////////////
// Readme:
// Audio Example.
// ////////////////////////////////////////////////////////
// Using this example, please explicitly refer to the book:
//     Jun-Hai Yong. Programming in Java. 
//     Beijing: Tsinghua University Press, 2004.
// 使用本例子,请注明引用:
//     雍俊海. Java 程序设计. 北京: 清华大学出版社, 2004.
// Please note that the author and publisher make no 
// warranty of any kind on the examples provided.
// ////////////////////////////////////////////////////////

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

public class J_Audio extends JApplet implements ActionListener, ItemListener
{
    private AudioClip m_soundFirst,  m_soundSecond, m_soundCurrent;
    private JButton   m_buttonPlay,  m_buttonLoop,  m_buttonStop;
    private JComboBox m_comboChoose;

    public void init( ) // Build interfaces and set sounds
    {
        Container container = getContentPane( );
        container.setLayout( new FlowLayout( ) );

        String choices[] = { "hi", "bark" };
        m_comboChoose = new JComboBox( choices );
        m_comboChoose.addItemListener( this );
        container.add( m_comboChoose );

        m_buttonPlay = new JButton( "Play" );
        m_buttonPlay.addActionListener( this );
        container.add( m_buttonPlay );
        
        m_buttonLoop = new JButton( "Loop" );
        m_buttonLoop.addActionListener( this );
        container.add( m_buttonLoop );
        
        m_buttonStop = new JButton( "Stop" );
        m_buttonStop.addActionListener( this );
        container.add( m_buttonStop );
        
        // load sounds and set m_soundCurrent
        m_soundFirst = getAudioClip( getDocumentBase( ), "hi.au" );
        m_soundSecond = getAudioClip( getDocumentBase( ), "bark.au" );
        m_soundCurrent = m_soundFirst;
    } // End of method: init

    public void stop( ) // Stop playing sound
    {
        m_soundCurrent.stop( );
    }

    public void itemStateChanged( ItemEvent e )
    {
        m_soundCurrent.stop( );
        m_soundCurrent = m_comboChoose.getSelectedIndex( ) == 0 ? m_soundFirst : m_soundSecond;
    } // End of method: itemStateChanged

    public void actionPerformed(ActionEvent e)
    {
        if ( e.getSource( ) == m_buttonPlay ) 
            m_soundCurrent.play( );
        else if ( e.getSource( ) == m_buttonLoop ) 
            m_soundCurrent.loop( );
        else if ( e.getSource( ) == m_buttonStop ) 
            m_soundCurrent.stop( );
    } // End of method: actionPerformed

} // End of class: J_Audio

⌨️ 快捷键说明

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