JAVA高级界面练习55声音播放器(应用程序中)_第1页
JAVA高级界面练习55声音播放器(应用程序中)_第2页
JAVA高级界面练习55声音播放器(应用程序中)_第3页
JAVA高级界面练习55声音播放器(应用程序中)_第4页
JAVA高级界面练习55声音播放器(应用程序中)_第5页
免费预览已结束,剩余1页可下载查看

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、练习55声音播放器(应用程序中)本例知识点一句话讲解新学使用 FilelnputStream 类读取文件内容知识使用 AudioPlayer 类播放声音已学使用File类选取文件知识使用Button类生成按钮一、练习具体要求本练习制作应用程序中的声音播放器,运行效果如图55-1所示。执行本例实例后,将 创建一个带有4个按钮的运行界面。按下Open按钮可以选择要播放的声音文件(*.au),按下 Play按钮可以播放当前的声音文件,按下Loop按钮可以循环播放当前声音文件,按下Stop 按钮可以停止播放当前声音文件。本实例的知识点有:FileDialog类,FilelnputStream类, Au

2、dioStream 类和 AudioData 类的应用。二、程序及注释(1)编程思路:本例因为要选择声音文件,所以首先要打开一个声音文件,以此文件 名作为FilelnputStream构造函数的参数,实例化一个FilelnputStream类的对象fis。然 后,因为要保存声音数据,所以首先要用fis作为数AudioStream构造函数的参数:然后实 例化一个AudioStream类对象as,声音数据就存在as的Data域;再调用函数as. getDataO 把声音数据保存在AudioData的对象theData里。最后,因为播放声音文件,所以首先要用 theData对象实例化AudioDat

3、aStream对象ads (如果需要一次播放声音数拯的话)或者用 ContinuousAudioDataStream对象cads (如果需要循环播放的话),然后只要以ads或cads 为参数,调用函数AudioPlayer, player, start (),就完成了播放声音的全过程。(2)程序实现及注释:/SoundPlayer javaimport sun audio *;import java awt *;import javaimport java awt event *;public class SoundPlayer extends Frameimplements Filename

4、Filterz ActionListener(Button openButton = new Button (Open1);Button playButton = new Button(MPlayH);Button loopButton = new Button(Loop);Button stopButton = new Button (Stop1);Label filename = nex Label (n);File theFile = null;AudioData theData = null;Inputstream nowPlaying = null;public SoundPlaye

5、r()/设宜标题栏文字super(SoundPlayern);/关闭窗口时退出程序aaaWindowListener(new WindowAdapter() publie voidwindowclosing (WinaoxEvent e) Systemexit(0););/设宜窗口大小setSize(300,200);/设宜窗口背景色setBackgrouna(Color orange);/设宜窗口上方面板控件内容Panel north = new Panel ();north setLayout (new FloxLayout (FloxLayout LEFT);northadd(new L

6、abel(File:H);northadd(“NORTH”,filename);add(Northz north);/设宜窗口下方面板控件内容Panel south = new Panel (); south add(openButton);south.add(playButton);south add(loopButton);south add(scopButton);add (South1, south);/为按钮添加事件监听openButton addActicnListener(this);playButton addActlonListEREM(this);loopButton ad

7、dActionList亡(this);stopButton aadActionListener(this);/主函数,应用程序入口处public static void main(String args)SoundPlayer sp = new SoundPlayer();sp show();)/打开文件public void open)FileDialog fa = new FileDialog(this,please select a file); fd setFilenameFilter (this);show();trytheFile = new File(fdgetDirectory

8、()+/H+fdgetFile(); if (theFile != null)(filename setText(theFile ge匸Name();Fileinputstream fis = new Fileinputstream(theFile);AudioStream as = new AudioStream(fis);theData = as.getData); catch(lOException e)System亡rr printin (e);catch(SecurityException e)System .out printin (security exception11);/开

9、始播放publie void play)stop();iftheData = null) open ();if(theData != null)AudioDataStream ads = new AudioDataStream(theDara);AudioPlayer.player start(ads); nowPlaying = ads;/停止播放public void stop()if(nowPlaying != null)AudioPlayer.player stop(nowPlaying); nowPlaying = null;/循环播放public void loop)(stop()

10、;if(theData = null) open ();if(theData != null)ContinuousAudioDataStz亡am cads=newContinuousAudioDataStrecim (theData);AudioPlayer.playerstart(ca6s);nowPlaying = cads;/爭件响应publie void actionPezform亡6(ActionEvent e)if(e g亡tSourc亡()= playButton)play ();ife gtSource() = openButton)open();if(e.gtSourc亡()=loopButton)loop();if (e.getSourc亡()=stopButton)stop();public boolean accept(File air String name) return true;三、练习效果(如图55-1所示)本实例代码编j 完卑,存盘为:C: j2sdkl. 4. 0j avaprograms SoundP 1 ay er. java。打 开计算机的命令提示符窗1,然后在命令提示符窗口中,立位到javaprogra

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论