[Tiện ích] - Phát nhạc trong ứng dụng Java desktop - Play sound in java application

Joe

Thành viên VIP
21/1/13
2,927
1,314
113
ManOhMan,
it's ok if you copy the codes, but you should pay more attention on what the author said
khuyên dùng định dạng wav
. And, furthermore, what the error message told you
No line matching interface SourceDataLine supporting format PCM_SIGNED 22050.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian is supported
You "rape" the codes by letting them roll over a MP3....
 
  • Like
Reactions: tranhuyvc

tranhuyvc

Administrator
Staff member
21/1/10
6,246
229
63
No line matching interface SourceDataLine supporting format PCM_SIGNED 22050.0 Hz, 16 bit, stereo, 4 bytes/frame
đọc lỗi cũng biết file sound bạn truyền vô mặc định decode không hỗ trợ nhé, bạn test bằng file wav xem nhé
 

Joe

Thành viên VIP
21/1/13
2,927
1,314
113
Hoangosb,
it's always a good habit that a developer studies the case theoretically before he or she starts to materialize his/her approach. Even when he/she copies somewhere the codes. And you should try to make this habit to your own habit. If you read the TRUE source of JAVA sound processing then you have to go to the ORACLE (former SUN) site and try to acquire the information. In your case: click HERE. Read it and, especially, pay more attention to the next page. Knowledge is always available and accessible. Only UNavailable and INaccessible for those who are too lazy to read. Example:
For other kinds of encodings, a frame might contain additional information besides the samples, and the frame rate might be completely different from the sample rate. For example, consider the MP3 (MPEG-1 Audio Layer 3) encoding, which is not explicitly mentioned in the current version of the Java Sound API, but which could be supported by an implementation of the Java Sound API or by a third-party service provider. In MP3, each frame contains a bundle of compressed data for a series of samples, not just one sample per channel. Because each frame encapsulates a whole series of samples, the frame rate is slower than the sample rate. The frame also contains a header. Despite the header, the frame size in bytes is less than the size in bytes of the equivalent number of PCM frames. (After all, the purpose of MP3 is to be more compact than PCM data.) For such an encoding, the sample rate and sample size refer to the PCM data that the encoded sound will eventually be converted into before being delivered to a digital-to-analog converter (DAC).


File Formats
A file format specifies the structure of a sound file, including not only the format of the raw audio data in the file, but also other information that can be stored in the file. Sound files come in various standard varieties, such as WAVE (also known as WAV, and often associated with PCs), AIFF (often associated with Macintoshes), and AU (often associated with UNIX systems).
Meaning: MP3 is NOT supported and that was the exception you got.
If you want to access to MP3 and play it you have only 2 choices:
1) look for a 3rd party provider if your app is written in "conventional" JAVA
2) implement your app in JFX. Click HERE -section pitfall with MediaPlayer. Here you will see how MP3 is implemented with JFX, JFX/SWING and 3rdPary JavaZOOM.