How to resolve the algorithm Play recorded sounds step by step in the Java programming language
How to resolve the algorithm Play recorded sounds step by step in the Java programming language
Table of Contents
Problem Statement
Load at least two prerecorded sounds, and demonstrate as many of these features as you can: Describe: [Note: If it seems to be a good idea, this task may be revised to specify a particular timeline rather than just 'demonstrate these features'.] Where applicable, please categorize examples primarily by the audio facility used (library/API/program/platform) rather than the language if the language is incidental (e.g. "Mac OS X CoreAudio" or "mplayer" rather than "C" or "bash").
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Play recorded sounds step by step in the Java programming language
This code implements an audio player with basic features like play, pause, resume, restart, jump to a specific time, stop and quit the program. The user can interact with the audio player through the console.
To do that, the code creates a Clip
object, which is a low-level API that allows to play audio files, and an AudioInputStream
object, which is a higher-level API that provides a way to read audio data from an input source, like a file.
The Clip
object is then configured to loop the audio file continuously, and the AudioInputStream
object is used to open the audio file.
The user can then interact with the audio player through the console, using the Scanner
object, to pause, resume, restart, jump to a specific time, stop or quit the program.
The code also defines an enum
called Status
that represents the current state of the audio player, which can be PLAYING
, PAUSED
or STOPPED
.
The select
method is used to select the desired action based on the user's input, and the resetAudioStream
method is used to reset the audio stream when the audio file is stopped or restarted.
The code also defines some private variables, like scanner
, clip
, currentClipPosition
, status
and audioStream
, which are used to store the state of the audio player and the audio file.
The FILE_PATH
constant is used to specify the path to the audio file that will be played.
Source code in the java programming language
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
public final class PlayRecordedSounds {
public static void main(String[] aArgs) {
PlayRecordedSounds soundPlayer = new PlayRecordedSounds();
soundPlayer.play();
scanner = new Scanner(System.in);
int choice = 0;
while ( choice != 6 ) {
System.out.println("1. Pause");
System.out.println("2. Resume");
System.out.println("3. Restart");
System.out.println("4. Jump to specific time");
System.out.println("5. Stop");
System.out.println("6. Quit the program");
choice = scanner.nextInt();
soundPlayer.select(choice);
}
scanner.close();
}
private enum Status { PLAYING, PAUSED, STOPPED }
private PlayRecordedSounds() {
resetAudioStream();
}
private void select(int aChoice) {
switch ( aChoice ) {
case 1 -> pause();
case 2 -> resume();
case 3 -> restart();
case 4 -> jump();
case 5 -> stop();
case 6 -> quit();
default -> { /* Take no action */ }
}
}
private void play() {
status = Status.PLAYING;
clip.start();
}
private void pause() {
if ( status == Status.PAUSED ) {
System.out.println("The audio is already paused");
return;
}
currentClipPosition = clip.getMicrosecondPosition();
clip.stop();
status = Status.PAUSED;
}
private void resume() {
if ( status == Status.PLAYING ) {
System.out.println("The audio is already being played");
return;
}
clip.close();
resetAudioStream();
clip.setMicrosecondPosition(currentClipPosition);
status = Status.PLAYING;
play();
}
private void restart() {
clip.stop();
clip.close();
resetAudioStream();
currentClipPosition = 0;
clip.setMicrosecondPosition(currentClipPosition);
status = Status.PLAYING;
play();
}
private void jump() {
System.out.println("Select a time between 0 and " + clip.getMicrosecondLength());
final long request = scanner.nextLong();
if ( request > 0 && request < clip.getMicrosecondLength() ) {
clip.stop();
clip.close();
resetAudioStream();
currentClipPosition = request;
clip.setMicrosecondPosition(currentClipPosition);
status = Status.PLAYING;
play();
}
}
private void stop() {
currentClipPosition = 0;
clip.stop();
clip.close();
status = Status.STOPPED;
}
private void quit() {
try {
scanner.close();
clip.close();
audioStream.close();
Runtime.getRuntime().exit(0);
} catch (IOException ioe) {
ioe.printStackTrace(System.err);
}
}
private void resetAudioStream() {
try {
audioStream = AudioSystem.getAudioInputStream( new File(FILE_PATH) );
clip = AudioSystem.getClip();
clip.open(audioStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException exception) {
exception.printStackTrace(System.err);
}
}
private static Scanner scanner;
private static Clip clip;
private static long currentClipPosition;
private static Status status;
private static AudioInputStream audioStream;
private static final String FILE_PATH = "./test_piece.wav";
}
You may also check:How to resolve the algorithm Least common multiple step by step in the Python programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the LabVIEW programming language
You may also check:How to resolve the algorithm Letter frequency step by step in the Elixir programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the Fortran programming language
You may also check:How to resolve the algorithm Solve a Hidato puzzle step by step in the Rust programming language