hi..
am quite new to sound applications in java. I want a audio control panel as output. i.e. if i click play button audio should play, if i click stop audio should stop and if i click pause audio should pause/hold until the play or resume button is clicked. i have done with playing and stopping audio. but could not pause and resume the audio . i used audioClip interface to play audio. functions i used were clip.play(), clip.stop(). but clip.pause or clip.resume() does not exist in api.
so can anyone suggest me any other better idea.

Thanx in advance.

Dani AI

Generated

Short answer and a minimal fix: the Clip API doesn’t expose pause()/resume() because stop() and start() already act as pause/resume — stop() does not reset the play position and start() resumes from where it stopped. To implement a pause button, save the clip’s position, call stop(), and on resume set the position back and call start(). Example:

long clipTime = 0;

// pause
clipTime = clip.getMicrosecondPosition();
clip.stop();

// resume
clip.setMicrosecondPosition(clipTime);
clip.start();

This approach is the usual pattern and is documented in the Clip API; community examples show the same getMicrosecondPosition / setMicrosecondPosition technique. (docs.oracle.com)

When the audio is long (music tracks) it’s better to stream and manage playback yourself with a SourceDataLine rather than loading everything into a Clip. Streaming gives finer control over buffering, and pause/resume means stopping the line and later restarting while tracking how many bytes/frames were already written — buffer sizing (small chunks, e.g. ~50 ms) matters to avoid underruns. (stackoverflow.com)

Format note and practical alternatives: the core Java Sound stack doesn’t include a robust MP3 decoder by default, so MP3 files typically need a third‑party decoder (for example JLayer/mp3 SPI) or use JavaFX’s MediaPlayer, which exposes easy play(), pause(), stop() and simplifies GUI wiring for common formats. For a quick desktop UI-based player, JavaFX often ends up being the easiest route. (github.com)

Tie-ins to the thread: suggested com.samsung.util (that’s an external/vendor API) and ’s reminder about jars/classpath is exactly right — third‑party packages must be on the classpath (or added as a Maven/Gradle dependency) to be importable. Also make sure clip.open(AudioInputStream) is called before start(), avoid clip.close() if you want to resume, and if a particular JVM/sound implementation misbehaves consider using a LineListener or small synchronization/delays as a workaround.

Recommended Answers

All 5 Replies

You could import the com.samsung.util.AudioClip class into your java class. Then you can reference the java docs from the link below to see how to use it.

http://www.j2megame.org/j2meapi/Samsung_API_1_0/com/samsung/util/AudioClip.html

Keep in mind that you can only use .midi, .mmf, or .mp3 audio with this class. If you prefer making the code custom to your solution then you may want to refer to the link below for another tutorial.

http://docs.oracle.com/javase/tutorial/sound/playing.html

,let me have a look at the links il get back...Thanx

i tried to import the package but i am getting an error that 'cannot find the package'.help please.
Thanx.

Are the class files for the package in a jar file? Is that jar file on the classpath when you compile the program?

i havent created a jar file yet.what i simply did is i created a media player which is able to play and stop only so i wanted to do other funtions like pausing media and resuming. the package com.samsung.util was recommended by i am not able to use it in my program because the program cannot find the package.i need help to be able to use the package in my program to perform other funtions.Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.