亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? ch12.htm

?? Java游戲開發
?? HTM
?? 第 1 頁 / 共 2 頁
字號:
<HTML>

<HEAD>
   <TITLE>Chapter 12 -- Playing Sound with Java</TITLE>
   <META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT COLOR=#FF0000>Chapter 12</FONT></H1>
<H1><B><FONT SIZE=5 COLOR=#FF0000>Playing Sound with Java</FONT></B>
</H1>
<P>
<HR WIDTH="100%"></P>
<P>
<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>
</FONT></FONT></H3>


<UL>
<LI><A HREF="#JavaSoundSupport" >Java Sound Support</A>
<UL>
<LI><A HREF="#TheAudioClipClass" >The AudioClip Class</A>
<LI><A HREF="#AlternativestoUsingAudioClip" >Alternatives to Using AudioClip</A>
</UL>
<LI><A HREF="#SampleAppletWildAnimals" >Sample Applet: WildAnimals</A>
<LI><A HREF="#Summary" >Summary</A>
<LI><A HREF="#QA" >Q&amp;A</A>
<LI><A HREF="#Workshop" >Workshop</A>
<UL>
<LI><A HREF="#Quiz" >Quiz</A>
<LI><A HREF="#Exercises" >Exercises</A>
</UL>
</UL>
<HR>
<P>
On <A HREF="ch11.htm" >Day 11</A>, you learned all about sound
and how it is used in computer games, as well as how to find and
record your own sounds. However, you didn't learn anything about
how to actually implement sound in Java. Sure, you might have
sampled a bunch of neat sounds, but they aren't of much use until
you under-stand how to play them in a real applet.
<P>
Today you learn all about sound and how it works in Java. You
find out all the not-so-gory details about how sound is represented
in Java, along with the classes and methods used to load and play
sounds. It turns out that the current release of Java has pretty
limited support for sound. Nevertheless, more than enough audio
support is there to liven up Java games. You finish up today's
lesson by using the sound support in Java 1.0 to build a pretty
neat applet that plays multiple sound effects.
<P>
The following topics are covered in today's lesson:
<UL>
<LI>Java sound support
<LI>Sample applet: WildAnimals
</UL>
<H2><A NAME="JavaSoundSupport"><B><FONT SIZE=5 COLOR=#FF0000>Java
Sound Support</FONT></B></A></H2>
<P>
The current sound support in Java comes in the form of a class
and a few methods in the <TT><FONT FACE="Courier">Applet</FONT></TT>
class. The <TT><FONT FACE="Courier">AudioClip</FONT></TT> class,
which is part of the applet package, models a digital audio sound
clip in the AU file format. You learn about this class next. You
learn about the methods supporting sound in the <TT><FONT FACE="Courier">Applet</FONT></TT>
class later in today's lesson.
<P>
The AU file format, which you learned about in yesterday's lesson,
is currently the only sound format supported by Java. If you recall,
it is designed around 8,000 Hz mono 8-bit ULAW encoded audio clips.
This is a fairly low-quality sound format, and it severely limits
Java in providing professional audio capabilities. However, in
the current context of the Web, just being able to play AU audio
clips in Java is plenty for many applets.
<P>
As far as games go, the quality of the sound isn't always as crucial
as you might think. Many sound effects (animal noises, for example)
don't require very high-quality audio. You find this out firsthand
in this lesson when you implement an applet using various animal
sound effects.
<H3><A NAME="TheAudioClipClass"><B>The </B><TT><B><FONT SIZE=4 FACE="Courier">AudioClip</FONT></B></TT><B><FONT SIZE=4>
Class</FONT></B></A></H3>
<P>
The <TT><FONT FACE="Courier">AudioClip</FONT></TT> class models
a sound clip in Java. It is an abstract class, so you can't directly
create instances of it. The only way to create <TT><FONT FACE="Courier">AudioClip</FONT></TT>
objects is by calling one of the <TT><FONT FACE="Courier">getAudioClip</FONT></TT>
methods of the <TT><FONT FACE="Courier">Applet</FONT></TT> class.
You'll learn more about that in a moment. But first, take a look
at the methods in the <TT><FONT FACE="Courier">AudioClip</FONT></TT>
class.
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public abstract void play()<BR>
public abstract void loop()<BR>
public abstract void stop()</FONT></TT>
</BLOCKQUOTE>
<P>
As you can see, these methods are very high-level and quite simplistic.
You can't ask for a much easier interface than just calling <TT><FONT FACE="Courier">play</FONT></TT>
to play an audio clip and <TT><FONT FACE="Courier">stop</FONT></TT>
to stop an audio clip. The only twist is the <TT><FONT FACE="Courier">loop</FONT></TT>
method, which plays an audio clip repeatedly in a loop until you
explicitly call <TT><FONT FACE="Courier">stop</FONT></TT> to stop
it. The <TT><FONT FACE="Courier">loop</FONT></TT> method is useful
when you have an audio clip that needs to be repeated, such as
a music clip or a footstep sound.
<P>
None of the methods in <TT><FONT FACE="Courier">AudioClip</FONT></TT>
require parameters; the <TT><FONT FACE="Courier">AudioClip</FONT></TT>
object is entirely self-contained. For this reason, there isn't
a lot to learn about using the <TT><FONT FACE="Courier">AudioClip</FONT></TT>
class. By simply understanding the three methods implemented by
the <TT><FONT FACE="Courier">AudioClip</FONT></TT> class (<TT><FONT FACE="Courier">play</FONT></TT>,
<TT><FONT FACE="Courier">loop</FONT></TT>, <TT><FONT FACE="Courier">stop</FONT></TT>),
you are practically already a Java sound expert!
<P>
Before your ego gets too inflated, remember that you still haven't
learned the details of how to create an <TT><FONT FACE="Courier">AudioClip</FONT></TT>
object. As I mentioned a little earlier, you use one of the <TT><FONT FACE="Courier">Applet</FONT></TT>
class's <TT><FONT FACE="Courier">getAudioClip</FONT></TT> methods
to create and initialize an <TT><FONT FACE="Courier">AudioClip</FONT></TT>
object. The two versions of <TT><FONT FACE="Courier">getAudioClip</FONT></TT>
are as follows:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public AudioClip getAudioClip(URL url)
<BR>
public AudioClip getAudioClip(URL url, String name)<BR>
</FONT></TT>
</BLOCKQUOTE>
<P>
<CENTER> <TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
I mentioned earlier that you can't create an <TT><FONT FACE="Courier">AudioClip</FONT></TT> object directly because <TT><FONT FACE="Courier">AudioClip</FONT></TT> is an abstract class. Because the class is abstract, you might be wondering how an <TT><FONT 
FACE="Courier">AudioClip</FONT></TT> object can be created at all. Technically, it is impossible to ever create an object based on an abstract class. However, in the case of <TT><FONT FACE="Courier">AudioClip</FONT></TT>, you use the <TT><FONT 
FACE="Courier">getAudioClip</FONT></TT> method to get a platform-specific <TT><FONT FACE="Courier">AudioClip</FONT></TT> derived object. In other words, <TT><FONT FACE="Courier">getAudioClip</FONT></TT> acts as a native method that returns a native class 
derived from <TT><FONT FACE="Courier">AudioClip</FONT></TT>. The method and class are native because sound support varies so widely on different platforms. The purpose of the <TT><FONT FACE="Courier">AudioClip</FONT></TT>, therefore, is to standardize the 
interface for the native audio clip classes, which results in a general, platform-independent programming solution.
</BLOCKQUOTE>

</TD></TR>
</TABLE></CENTER>
<P>
<P>
The only difference between these two <TT><FONT FACE="Courier">getAudioClip</FONT></TT>
methods is whether or not the URL parameter contains a complete
reference to the name of the audio clip. In the first version,
it is assumed that the URL contains the complete name; the second
version uses the name in a separate <TT><FONT FACE="Courier">name</FONT></TT>
parameter. You will typically use the second version, because
you can easily retrieve the base URL of the applet or the HTML
document in which the applet is embedded. You do this by using
either the <TT><FONT FACE="Courier">getCodeBase</FONT></TT> or
the <TT><FONT FACE="Courier">getDocumentBase</FONT></TT> method
of <TT><FONT FACE="Courier">Applet</FONT></TT>, like this:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">AudioClip clip1 = getAudioClip(getCodeBase(),
&quot;sound1.au&quot;);<BR>
AudioClip clip2 = getAudioClip(getDocumentBase(), &quot;sound2.au&quot;);</FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier">getCodeBase</FONT></TT> method returns
the base URL of the applet itself, whereas <TT><FONT FACE="Courier">getDocumentBase</FONT></TT>
returns the base URL of the HTML document containing the applet.
It is usually smarter to use <TT><FONT FACE="Courier">getCodeBase</FONT></TT>
to specify the base URL for loading resources used by a Java applet.
The reason for this is that it is often useful to organize Java
applets into a directory structure beneath the HTML documents
in which they appear. Furthermore, you usually reference images
and sounds either from the same directory where the applet is
located or from a subdirectory beneath it.
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
I like to organize images and sounds used by an applet in a directory called <TT><FONT FACE="Courier">Res</FONT></TT>, beneath the directory containing the actual Java classes. This isolates the executable part of an applet from the resource content used 
by the applet, resulting in a more organized file structure.
</BLOCKQUOTE>

</TD></TR>
</TABLE></CENTER>
<P>
<H3><A NAME="AlternativestoUsingAudioClip"><B>Alternatives to
Using </B><TT><B><FONT SIZE=4 FACE="Courier">AudioClip</FONT></B></TT></A>
</H3>
<P>
In the audio discussion thus far, I might have led you to believe
that you must use an <TT><FONT FACE="Courier">AudioClip</FONT></TT>
object to play sounds in Java. This isn't entirely true! The truth
is that you are only required to create an <TT><FONT FACE="Courier">AudioClip</FONT></TT>
object if you want to play looped sounds. For normal (nonlooped)
sounds, you have the option of using one of the <TT><FONT FACE="Courier">play</FONT></TT>
methods in the <TT><FONT FACE="Courier">Applet</FONT></TT> class
instead of using an <TT><FONT FACE="Courier">AudioClip</FONT></TT>
object. The definitions for the <TT><FONT FACE="Courier">play</FONT></TT>
methods implemented in <TT><FONT FACE="Courier">Applet</FONT></TT>
are as follows:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public void play(URL url)<BR>
public void play(URL url, String name)</FONT></TT>
</BLOCKQUOTE>
<P>
These <TT><FONT FACE="Courier">play</FONT></TT> methods take exactly
the same parameters taken by the <TT><FONT FACE="Courier">getAudioClip</FONT></TT>
methods. In fact, the <TT><FONT FACE="Courier">play</FONT></TT>
methods in <TT><FONT FACE="Courier">Applet</FONT></TT> actually
call <TT><FONT FACE="Courier">getAudioClip</FONT></TT> to get
an <TT><FONT FACE="Courier">AudioClip</FONT></TT> object and then
use the <TT><FONT FACE="Courier">AudioClip</FONT></TT> object's
<TT><FONT FACE="Courier">play</FONT></TT> method to play the sound.
In this way, the <TT><FONT FACE="Courier">Applet</FONT></TT> <TT><FONT FACE="Courier">play</FONT></TT>
methods basically provide a higher level method of playing audio.
This is evident in Listing 12.1, which shows the Java 1.0 source
code for the <TT><FONT FACE="Courier">Applet</FONT></TT> <TT><FONT FACE="Courier">play</FONT></TT>
methods.
<HR>
<BLOCKQUOTE>
<B>Listing 12.1. The Java 1.0 </B><TT><B><FONT FACE="Courier">Applet</FONT></B></TT><B>
class's </B><TT><B><FONT FACE="Courier">play</FONT></B></TT><B>
methods.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">public void play(URL url) {<BR>
&nbsp;&nbsp;AudioClip clip = getAudioClip(url);<BR>
&nbsp;&nbsp;if (clip != null) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;clip.play();<BR>
&nbsp;&nbsp;}<BR>
}<BR>
<BR>
public void play(URL url, String name) {<BR>
&nbsp;&nbsp;AudioClip clip = getAudioClip(url, name);<BR>
&nbsp;&nbsp;if (clip != null) {<BR>
&nbsp;&nbsp;&nbsp;&nbsp;clip.play();<BR>
&nbsp;&nbsp;}<BR>
}</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The <TT><FONT FACE="Courier">Applet</FONT></TT> <TT><FONT FACE="Courier">play</FONT></TT>
methods both create a temporary <TT><FONT FACE="Courier">AudioClip</FONT></TT>
object and then use it to play the sound by calling its <TT><FONT FACE="Courier">play</FONT></TT>
method. This provides an even higher level of interface to playing
nonlooped sounds than the <TT><FONT FACE="Courier">AudioClip</FONT></TT>
class provides.
<H2><A NAME="SampleAppletWildAnimals"><B><FONT SIZE=5 COLOR=#FF0000>Sample
Applet: WildAnimals</FONT></B></A></H2>
<P>
Now that you are a Java sound expert (at least in theory), it's
time to put your newfound knowledge to work in a sample applet.
Because this book is ultimately about writing games, it's important
for you to never compromise in making your applets as entertaining
as possible. For this reason, the applet you're going to develop
to demonstrate sound is a little more than a simple sound player.
As a matter of fact, it's quite wild! Figure 12.1 shows a screen
of the WildAnimals applet, which uses the Java <TT><FONT FACE="Courier">AudioClip</FONT></TT>
class to generate some entertaining results. The source code,
executable, images, and sounds for WildAnimals are located on
the accompanying CD-ROM.
<P>
<A HREF="f12-1.gif" ><B>Figure 12.1 : </B><I>The WildAnimals sample applet.</I></A>
<P>
The screen shot of WildAnimals doesn't quite convey the real purpose
of the applet. So, at this point, I encourage you to run it for
yourself from the CD-ROM to get the real effect. Just in case
you're the impatient type and choose to skip the wild animal experience,
I'll fill you in on what's happening. WildAnimals randomly plays
a variety of wild animal sounds to go with the eyes that are staring
at you from the darkness.
<P>
Now that you know what it does, let's take a look at the implementation
of WildAnimals. The <TT><FONT FACE="Courier">WildAnimals</FONT></TT>
class really only defines one member variable beyond the variables
required for animation that you've already seen in prior applets:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">private AudioClip[] clip = new AudioClip[5];</FONT></TT>
</BLOCKQUOTE>
<P>
This array of <TT><FONT FACE="Courier">AudioClip</FONT></TT> objects
is used to store the wild animal sounds.
<P>
The <TT><FONT FACE="Courier">init</FONT></TT> method in <TT><FONT FACE="Courier">WildAnimals</FONT></TT>
initializes the audio clips by calling the <TT><FONT FACE="Courier">getAudioClip</FONT></TT>
method:

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久众筹精品私拍模特| 91麻豆精东视频| 成人精品视频网站| 欧美日韩国产在线观看| 国产欧美一区二区三区鸳鸯浴 | 欧美在线影院一区二区| 波多野结衣中文字幕一区二区三区| 色综合久久中文字幕综合网| 精品国产一二三| 琪琪久久久久日韩精品| 色综合久久综合网97色综合| 久久精品夜色噜噜亚洲a∨| 美女一区二区视频| 欧美日韩欧美一区二区| 欧美日韩精品高清| 亚洲女人小视频在线观看| 亚洲自拍偷拍麻豆| 91免费版pro下载短视频| 欧美三日本三级三级在线播放| 色先锋久久av资源部| 色哦色哦哦色天天综合| 欧美日韩亚洲高清一区二区| 亚洲日穴在线视频| 日韩精品成人一区二区三区| 色94色欧美sute亚洲线路一久 | 午夜精品一区二区三区免费视频| 成人精品国产福利| 国产精品日产欧美久久久久| 国产乱码字幕精品高清av| av影院午夜一区| 国产精品私人自拍| 热久久久久久久| 678五月天丁香亚洲综合网| 久久亚洲精品国产精品紫薇| 精品在线视频一区| 91丝袜呻吟高潮美腿白嫩在线观看| 欧洲色大大久久| 久久亚区不卡日本| 成人自拍视频在线| 欧美日韩美女一区二区| 久久久精品黄色| 成人国产在线观看| 亚洲精品中文字幕乱码三区| 精品在线播放免费| 欧美特级限制片免费在线观看| 亚洲黄色av一区| 欧美精品丝袜久久久中文字幕| 三级在线观看一区二区 | 国产女人18毛片水真多成人如厕 | 国产日产欧美一区二区三区| 成人午夜精品在线| 一区二区三区免费观看| 国产黄色91视频| 日韩一区二区三区在线观看| 亚洲欧美国产三级| 国产精品888| 午夜国产不卡在线观看视频| 884aa四虎影成人精品一区| 国产精品区一区二区三区| 日本sm残虐另类| 国产女人18水真多18精品一级做| 99久久99久久免费精品蜜臀| 午夜精品在线看| 欧美激情一区二区三区| 精品写真视频在线观看| 国产精品久久久久久久久快鸭| 91成人免费网站| 精品一区二区三区欧美| 日韩午夜av电影| 亚洲国产毛片aaaaa无费看| 日韩三级免费观看| 日韩在线a电影| 欧美群妇大交群中文字幕| 亚洲欧美另类久久久精品2019| 欧美群妇大交群中文字幕| 国产精品一区二区久久不卡 | 亚洲国产精品成人综合| 欧美性淫爽ww久久久久无| 亚洲人成电影网站色mp4| 懂色av一区二区夜夜嗨| 首页国产丝袜综合| 欧美三区在线观看| 一区二区三区在线免费播放| 337p粉嫩大胆噜噜噜噜噜91av| 在线日韩一区二区| 成人午夜私人影院| 久久97超碰色| 午夜一区二区三区在线观看| 一区二区三区精品视频在线| 99视频一区二区| 国产呦精品一区二区三区网站| 亚洲一区二区在线播放相泽| 精品视频色一区| 99精品视频在线播放观看| 美美哒免费高清在线观看视频一区二区 | 在线亚洲一区二区| 一二三四区精品视频| 欧美日韩二区三区| 日韩极品在线观看| 一区二区三区在线看| 中文字幕亚洲精品在线观看 | 亚洲一区二区欧美日韩| 亚洲私人黄色宅男| 国产日韩欧美综合一区| 大胆欧美人体老妇| 国产精品白丝av| 国产精品欧美极品| 久久久久高清精品| 99r精品视频| 亚洲在线视频网站| 亚洲精品写真福利| 7777女厕盗摄久久久| 极品销魂美女一区二区三区| 蜜臀av性久久久久av蜜臀妖精| 日韩av高清在线观看| 欧美变态tickle挠乳网站| 日韩色视频在线观看| 精品国产自在久精品国产| 欧美videofree性高清杂交| 精品乱人伦小说| 91一区一区三区| 色婷婷国产精品久久包臀 | 成人深夜在线观看| 中文字幕在线观看一区| 一区在线中文字幕| 在线91免费看| 日韩欧美国产一区二区在线播放| 欧美成人r级一区二区三区| av一区二区不卡| 免费在线观看不卡| 亚洲色图自拍偷拍美腿丝袜制服诱惑麻豆| 欧美激情在线一区二区| 亚洲精品免费在线观看| 亚洲一区二区av在线| 欧美韩日一区二区三区| 亚洲天堂av老司机| 午夜电影网一区| 国产一区二区电影| 日韩影院免费视频| 亚洲已满18点击进入久久| 久久久午夜精品| 欧美高清视频一二三区| 国产亚洲欧美日韩在线一区| 欧美私模裸体表演在线观看| 国产成人免费视频一区| 色综合久久精品| 欧美一区二区免费| 国产精品无码永久免费888| 日韩欧美一二三| 欧美日韩免费一区二区三区| 精品国产乱码久久久久久图片| 中文成人av在线| 丝袜诱惑制服诱惑色一区在线观看| 国产精品一区二区不卡| 91九色最新地址| 久久中文字幕电影| 欧美成人精品二区三区99精品| 中文字幕在线观看不卡| 欧美韩国一区二区| 欧美aⅴ一区二区三区视频| 亚洲一区在线观看免费观看电影高清| 欧美经典一区二区| 久久久精品国产免费观看同学| 亚洲日本欧美天堂| 亚洲美女屁股眼交| 亚洲三级在线免费观看| 美女看a上一区| 黄一区二区三区| 欧美在线一区二区| 国产精品高潮呻吟| 精品一区二区在线看| 欧美日韩精品免费观看视频| 亚洲欧美综合在线精品| 国产成人午夜电影网| 91精品久久久久久蜜臀| 亚洲人成精品久久久久| 国产成人精品免费在线| 日韩欧美成人一区二区| 亚洲二区在线观看| 色狠狠一区二区| 欧美日韩精品电影| 亚洲私人黄色宅男| 在线看不卡av| 欧美日韩精品一区二区三区四区| 在线日韩av片| 亚洲欧美日韩久久| 成人福利在线看| 在线一区二区视频| 日韩一区二区三区高清免费看看| 亚洲免费观看高清完整版在线观看熊| 国产在线一区观看| 欧美白人最猛性xxxxx69交| 日韩av网站免费在线| 欧美日韩成人激情| 午夜精品久久久久影视| 欧美性猛交xxxx黑人交| 欧美大片一区二区| 久久精品久久久精品美女| 国产电影一区在线| 久久精品夜色噜噜亚洲a∨|