Play audio files in sipphone

Source: Internet
Author: User

This article records the function of playing music files based on Java sip softphone. We have introduced several sipphones. We found that Java sip softphone, an open-source software, is easy to modify and can choose whether to retain its GUI based on your needs, then, modify a few codes to play the specified music file after dialing. However, there are still several issues to be solved in the future: 1. It is an analysis of the entire source code framework process. At present, I only deleted the GUI, but the underlying sip rtp transmission is not involved; 2. problems occur when the downloaded version is suspended or the remote client is busy. 3. Currently, the audio file is only in wav format supported by default. Other formats can be expanded, however, the method I implemented should still be problematic, because the interval between obtaining and sending sound data is not fixed yet. I only tested it for 20 ms and will see it later.
To play a sound file, you must first find out where the data is obtained. Net. sourceforge. peers. media package. buffer = soundManager is displayed in the class Capture. readData ();, and then view the SoundManager class. You can find functions such as openAndStartLines (), readData (), and writeData, it is exactly what we need to capture and write microphone audio data into the speaker. The readData () method is as follows:
[Java]
1 ./**
2. * audio read from microphone, read all available data
3. * @ return
4 .*/
5. public synchronized byte [] readData (){
6. if (targetDataLine = null ){
7. return null;
8 .}
9. int ready = targetDataLine. available ();
10. while (ready = 0 ){
11. try {
12. Thread. sleep (2 );
13. ready = targetDataLine. available ();
14.} catch (InterruptedException e ){
15. return null;
16 .}
17 .}
18. if (ready <= 0 ){
19. return null;
20 .}
21. byte [] buffer = new byte [ready];
22. targetDataLine. read (buffer, 0, buffer. length );
23. if (mediaDebug ){
24. try {
25. microphoneOutput. write (buffer, 0, buffer. length );
26.} catch (IOException e ){
27. logger. error ("cannot write to file", e );
28. return null;
29 .}
30 .}
31. return buffer;
32 .}
The debugging information is removed, combined with the code in the class Capture, and it is found that data is only obtained through the buffer, and then the data is sent through the rawData of the PipedOutputStream type; therefore, as long as the data is in readData () so that the buffer can return the audio file data to be sent.
According to the above analysis, the rest is to read audio files. java supports four audio formats by default, namely AU, AIFF, WAVE, and MIDI. If more formats are needed, you need to download the additional package (refer to 1), and then first from the (refer to 2) commonly used volume ound class diagram as follows:

 


Obtain the system's mixer that complies with the AudioFormat attribute specified in DataLine.info from AudioSystem. The following code is displayed in the constructor class SoundManager and openAndStartLines () function:
[Java] view plaincopyprint?
1. // linear PCM 8 kHz, 16 bits signed, mono-channel, little endian
2. audioFormat = new AudioFormat (8000, 16, 1, true, false );
3. targetInfo = new DataLine. Info (TargetDataLine. class, audioFormat );
4. sourceInfo = new DataLine. Info (SourceDataLine. class, audioFormat );
 
[Java]
1. public void openAndStartLines (){
2. logger. debug ("openAndStartLines ");
3. // I deleted some debugging code.
4. try {
5. targetDataLine = (TargetDataLine) AudioSystem. getLine (targetInfo );
6.
7.
8. targetDataLine. open (audioFormat );
9.} catch (LineUnavailableException e ){
10. logger. error ("target line unavailable", e );
11. return;
12 .}
13. targetDataLine. start ();
14. try {
15. sourceDataLine = (SourceDataLine) AudioSystem. getLine (sourceInfo );
16. sourceDataLine. open (audioFormat );
17.} catch (LineUnavailableException e ){
18. logger. error ("source line unavailable", e );
19. return;
20 .}
21. sourceDataLine. start ();
22 .}
Although we have read so much, we do not need to write data into the speaker, but only get and return the data. Therefore (refer to 1) We get the input stream from the file. The Code is as follows:

[Java]
1. if (testNum <1 ){
2. File file = new File ("test.wav ");
3. // AudioInputStream audioInputStream = null; // file stream
4. // AudioFormat audioFormat = null; // File Format
5. // get the file input stream
6. try {
7. audioInputStream = AudioSystem. getAudioInputStream (file );
8.} catch (UnsupportedAudioFileException e ){
9. // TODO Auto-generated catch block
10. e. printStackTrace ();
11.} catch (IOException e ){
12. // TODO Auto-generated catch block
13. e. printStackTrace ();
14 .}
15. audioFormat = audioInputStream. getFormat ();
16. // code for converting the file code can be added here
17. testNum ++;
18. System. out. println ("testNum" + testNum );
19 .}
20. testNum ++;
21. System. out. println ("testNum" + testNum );
22. int ready = 0;
23. try {
24. ready = audioInputStream. available ();
25.
26. // while (ready = 0 ){
27. try {
28. Thread. sleep (20 );
29. ready = audioInputStream. available ();
30.} catch (InterruptedException e ){
31. return null;
32 .}
33 .//}
34.} catch (IOException e1 ){
35. // TODO Auto-generated catch block
36. e1.printStackTrace ();
37 .}
38. if (ready <= 0 ){
39. return null;
40 .}
41.
42. byte [] buffer = new byte [320]; // readData is read cyclically
43. try {
44. audioInputStream. read (buffer, 0, buffer. length );
45.} catch (IOException e1 ){
46. // TODO Auto-generated catch block
47. e1.printStackTrace ();
48 .}
49. System. out. println ("ready:" + ready );
50. return buffer;
Then, convert an MP3 or other format file into a wav file based on the original sampling frequency, number of channels, and so on to test the file. Pay attention to the interval between sleep, if not, data cannot be processed in a timely manner, causing a pause. The problem is also here. What is the proper interval and how much data is read by the buffer? This can be set based on the file attributes, but should there be a better way? Continue searching.
Finally, I thought that I didn't talk about the environment setup process. Let's sort it out next time.

Author: w7849516230
 

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.