Playing Sounds in Java (2)

Source: Internet
Author: User
Tags try catch

Sound is a vital part of a game.

Last time I talked about using AudioClip simple two lines of code to play the Sound (http://blog.csdn.net/cnmm22/article/details/44674173). Today I'll introduce another way to use Audiostream.

It is difficult to use the method itself to locate a resource during the process of using both methods.

The last time we sent AudioClip a URL to locate a resource, this time, we continue this idea and refine how to locate a resource file in Java.

This time. I'll change the wording:

String path = new File (GetClass (). getClassLoader (). GetResource (""). Touri ()). GetPath ();
FileInputStream Fileau = new FileInputStream (path + "\sounds\1\1.mp3");

We didn't pass the URL this time, but first we also use the reflection mechanism, getclass (). getClassLoader (). Getresource.touri ()). GetPath (), get the absolute position of the Project Bin folder in the computer, because it is reflected, So the resource can still be found after the file is moved. The following path + "\sounds\1\1.mp3" is the URI of the file itself, where the SOUNDS/1 folder is a demonstration, representing any path dropping under the bin. Notice that the backslash is replaced by a double backslash.

We pass this path (String name) to FileInputStream and get the resource file we want to play audiostream.

Next is the playback code:

Audiostream as = new Audiostream (Fileau);
AudioPlayer.player.start (AS);

Suppose we use a URL to locate a resource file this time, the reference code is:

URL u = XXX.class.getClassLoader (). GetResource ("sounds/1/diao.wma");
as = new Audiostream (U.openstream ());
AudioPlayer.player.start (AS);

If we need to play a piece of music repeatedly, we need to write:

Audiodata data = As.getdata ();
Continuousaudiodatastream GG = new Continuousaudiodatastream (data);
AudioPlayer.player.start (GG);

Finally, don't forget: As.close (), and of course, try catch.

Finally, I need to explain. Java I mentioned the two methods of sound playback, the support for all audio files is not ideal, there are many formats, such as MP3 is unable to play drops, files generally need to convert to WAV, after this because of transcoding problems, not even all WAV can play, this will make the file too large, And currently I haven't found a good pause in jdk1.8 to continue playing the music method. So I hope this article can give some reference to the Java to locate a resource file by reflection.

Playing Sounds in Java (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.