亚洲欧美第一页_禁久久精品乱码_粉嫩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一区二区三区免费野_久草精品视频
欧美一级欧美三级在线观看| 中文字幕亚洲电影| 男女视频一区二区| 欧美一区日本一区韩国一区| 三级久久三级久久| 67194成人在线观看| 老司机午夜精品99久久| 精品欧美一区二区久久| 韩国女主播一区二区三区| 国产欧美中文在线| 91蜜桃免费观看视频| 一区二区三区日本| 欧美精品三级日韩久久| 激情综合五月天| 国产精品天干天干在观线| 色一区在线观看| 日韩国产精品久久久久久亚洲| 精品久久一区二区| 成人午夜av影视| 亚洲综合在线电影| 日韩美女一区二区三区| 国产精品一区二区果冻传媒| 亚洲同性同志一二三专区| 欧美色网站导航| 国产精品一区一区| 亚洲激情五月婷婷| 久久一夜天堂av一区二区三区 | 日本aⅴ亚洲精品中文乱码| 精品国产成人系列| 波多野结衣欧美| 欧美激情一二三区| 国产精品福利av| 成人午夜av在线| 日韩电影免费在线观看网站| 国产亚洲综合在线| 在线观看日韩毛片| 国产乱码字幕精品高清av | 精品一区二区三区免费视频| 7777精品伊人久久久大香线蕉完整版 | 2020国产精品久久精品美国| 成人av综合一区| 日韩精品亚洲一区二区三区免费| 亚洲午夜久久久久中文字幕久| 国产九九视频一区二区三区| 亚洲国产精品传媒在线观看| 97精品国产露脸对白| 免费观看在线色综合| 91污片在线观看| 亚洲国产日韩精品| 久久亚洲免费视频| 色偷偷成人一区二区三区91 | 亚洲成人动漫av| 国产欧美一区二区三区沐欲| 欧美性极品少妇| 不卡一卡二卡三乱码免费网站| 天天爽夜夜爽夜夜爽精品视频| 国产精品狼人久久影院观看方式| 日韩一区二区中文字幕| 91国内精品野花午夜精品| 国产激情一区二区三区| 午夜精品久久久久影视| 亚洲欧美日韩综合aⅴ视频| 久久午夜老司机| 欧美一区二区三区四区五区| 91黄色激情网站| 成人激情免费视频| 国产尤物一区二区| 久久国产综合精品| 日韩福利电影在线| 亚洲高清不卡在线| 一区二区三区高清| 亚洲欧美另类久久久精品| 国产精品拍天天在线| 亚洲精品一区二区三区精华液| 欧美另类高清zo欧美| 在线精品视频一区二区三四| 91麻豆6部合集magnet| 成人久久18免费网站麻豆| 精品一区二区在线视频| 另类综合日韩欧美亚洲| 日韩 欧美一区二区三区| 日本视频免费一区| 青椒成人免费视频| 久久99久久久欧美国产| 久久99国产精品久久99 | 亚洲精品中文在线影院| 亚洲欧美日韩电影| 亚洲激情网站免费观看| 亚洲一级不卡视频| 日韩和的一区二区| 国内精品久久久久影院薰衣草| 国产精品1区2区3区| 国产高清不卡二三区| 成人性生交大片免费看中文 | 麻豆专区一区二区三区四区五区| 美女视频免费一区| 极品少妇一区二区三区精品视频| 国产专区欧美精品| eeuss鲁一区二区三区| 91久久国产最好的精华液| 欧美午夜精品一区二区蜜桃| 日韩一区二区三区电影| 久久精品夜色噜噜亚洲a∨| 国产精品国产三级国产a| 亚洲精品伦理在线| 日本欧美久久久久免费播放网| 精品一区二区精品| 不卡电影一区二区三区| 依依成人精品视频| 欧美a级一区二区| 天天av天天翘天天综合网色鬼国产 | 色悠久久久久综合欧美99| 色激情天天射综合网| 欧美高清dvd| 久久久久久久久久久黄色| 国产精品福利一区二区三区| 亚洲电影你懂得| 国产一区二区免费视频| 在线观看精品一区| 欧美一卡二卡在线| 国产精品传媒视频| 日韩国产在线观看一区| 成人少妇影院yyyy| 日韩国产一区二| www.欧美日韩| 91精品国产一区二区三区| 国产精品国产馆在线真实露脸| 日本三级亚洲精品| 91免费国产在线| 韩国精品久久久| 国产1区2区3区精品美女| 国产三级一区二区三区| 亚洲精品成a人| 国产乱一区二区| 欧美电影影音先锋| 亚洲欧美在线视频观看| 精品一区二区三区欧美| 欧美区在线观看| 亚洲欧美中日韩| 国产一区二区不卡| 欧美妇女性影城| 亚洲视频在线一区二区| 国产一区二区中文字幕| 欧美视频在线不卡| 日韩一区日韩二区| 精彩视频一区二区| 欧美色欧美亚洲另类二区| 久久久.com| 日韩黄色一级片| 99re这里都是精品| 色欧美片视频在线观看| 久久精品在线免费观看| 手机精品视频在线观看| 91免费在线视频观看| 欧美乱熟臀69xxxxxx| 亚洲精品菠萝久久久久久久| 国产成人综合网站| 在线成人高清不卡| 一区二区三区在线视频播放| 波多野结衣中文字幕一区 | 日韩电影一区二区三区四区| 国产·精品毛片| 日韩欧美亚洲另类制服综合在线| 最近中文字幕一区二区三区| 国产精品一区二区免费不卡 | 亚洲天堂网中文字| 国产一区二区不卡老阿姨| 26uuu成人网一区二区三区| 亚洲www啪成人一区二区麻豆| 欧美综合一区二区| 亚洲男同1069视频| 成人白浆超碰人人人人| 日韩欧美中文字幕一区| 久久国产福利国产秒拍| 欧美一区2区视频在线观看| 亚洲一区二区在线视频| 成人免费视频视频在线观看免费| 欧美不卡视频一区| 亚洲一二三级电影| 色哟哟国产精品免费观看| 亚洲人成精品久久久久| 99精品久久只有精品| 国产欧美视频一区二区三区| 国产精品白丝jk白祙喷水网站| 欧美一区二区美女| 日本不卡的三区四区五区| 91精选在线观看| 日韩黄色小视频| 久久久久国产精品厨房| 国内外成人在线| 久久久久久久久久久久电影 | 亚洲欧美一区二区三区孕妇| 国产福利精品一区二区| 亚洲手机成人高清视频| 欧洲视频一区二区| 亚洲一区二区视频| www.99精品| 午夜激情综合网| 精品国一区二区三区| 国产精品996|