Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Main #120

Closed
wants to merge 2 commits into from
Closed

Main #120

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,33 @@ public static int calculateLuhnChecksum(long num) {
}
```

### Play sound

```java
public static void playSound(String soundFile) throws IOException, UnsupportedAudioFileException, LineUnavailableException {
File f = new File("./" + soundFile);
AudioInputStream audioIn = null;
Clip clip = null;
try {
audioIn = AudioSystem.getAudioInputStream(f.toURI().toURL());
clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
FloatControl volume = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(1.0f); // Reduce volume by 10 decibels.
clip.drain();
}
finally {
try {
clip.close();
}
catch (Exception exp) {
exp.printStackTrace();
}
}
}
```

## Networking

### HTTP GET
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/media/PlaySoundSnippet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package media;

import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;

/*
* 30 Seconds of Java code library
*
*/
public class PlaySoundSnippet {


/**
* Factorial. Works only for small numbers
*
* @param soundFile which needs to be played
* @throws IOException if an I/O error occurs
* @throws UnsupportedAudioFileException if the file did not contain valid data of a recognized file type and format
* @throws LineUnavailableException if a line cannot be opened because it is unavailable
*/
public static void playSound(String soundFile) throws IOException, UnsupportedAudioFileException, LineUnavailableException {
File f = new File("./" + soundFile);
AudioInputStream audioIn = null;
Clip clip = null;

try {
audioIn = AudioSystem.getAudioInputStream(f.toURI().toURL());
clip = AudioSystem.getClip();
clip.open(audioIn);

clip.start();

FloatControl volume = (FloatControl)clip.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(1.0f); // Reduce volume by 10 decibels.

clip.drain();

}
finally {
try {
clip.close();
}
catch (Exception exp) {
exp.printStackTrace();
}
}

}
}


25 changes: 25 additions & 0 deletions src/test/java/media/PlaySoundSnippetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package media;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/*
* Tests for 30 Seconds of Java code library
*
*/
class PlaySoundSnippetTest {
/**
* Tests for {@link PlaySoundSnippet#playSound(String)}.
*/
@Test
void playSound() {
String filename = "media/file_example_WAV_1MG.wav";

assertDoesNotThrow(() -> {
PlaySoundSnippet.playSound(filename);
});

}

}
Binary file added src/test/resources/media/file_example_WAV_1MG.wav
Binary file not shown.