直播从Java服务器传输音频到Android客户端。

huangapple 未分类评论64阅读模式
标题翻译

Live Streaming audio from java server to Android Client

问题

以下是您提供的代码的中文翻译部分:

  1. 我想从我的计算机流式传输音频到安卓客户端我已经获取了输入现在我希望将其通过流传输并播放现在我的问题是在输入端我有AudioInputStream类和其他类我能够使用它们但在安卓端它说它们是私有的无法在类外部访问我想在安卓上播放传入的音频我应该怎么做我现在只想支持一个客户端
  2. 服务器
  3. import java.net.*;
  4. import java.io.*;
  5. import javax.sound.sampled.*;
  6. public class MainServer{
  7. // ...(您提供的服务器代码)
  8. }
  9. class HandlingClient extends Thread{
  10. // ...(您提供的HandlingClient类的代码)
  11. }
  12. 安卓客户端
  13. import androidx.appcompat.app.AppCompatActivity;
  14. import android.os.Bundle;
  15. import android.os.StrictMode;
  16. import android.view.View;
  17. import android.widget.EditText;
  18. import java.io.IOException;
  19. import java.net.Socket;
  20. public class MainActivity extends AppCompatActivity {
  21. // ...(您提供的MainActivity类的代码)
  22. }

注意:由于您要求仅返回翻译好的部分,我已经将代码翻译成了中文。如果您有关于代码的具体问题或进一步的指导,欢迎随时提问。

英文翻译

I want to stream audio from my computer to android clients. I have taken the input and now I want that to be transmitted over the stream and be played. Now my problem is that on the input side I have AudioInputStream class and other classes which I am able to use but on the android side it says that they are private and cannot be accessed outside the class. I want to play the coming audio on android. What should I do. I just want to support 1 client for now.

Server:

  1. import java.net.*;
  2. import java.io.*;
  3. import javax.sound.sampled.*;
  4. public class MainServer{
  5. private static AudioFormat getAudioFormat() {
  6. AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
  7. float rate = 44100.0f;
  8. int channels = 2;
  9. int sampleSize = 16;
  10. boolean bigEndian = true;
  11. InetAddress addr;
  12. AudioFormat format = new AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8) * channels, rate, bigEndian);
  13. return format;
  14. }
  15. private static TargetDataLine configureStreams(){
  16. try{
  17. AudioFormat format = getAudioFormat();
  18. DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
  19. // checks if system supports the data line
  20. if (!AudioSystem.isLineSupported(info)) {
  21. System.out.println("Line not supported");
  22. System.exit(0);
  23. }
  24. TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
  25. line.open(format);
  26. line.start();
  27. System.out.println("Got the line and started to listen");
  28. return line;
  29. }
  30. catch(Exception e){
  31. System.err.println(e);
  32. }
  33. return null;
  34. }
  35. public static void main(String args[]){
  36. try{
  37. ServerSocket server = new ServerSocket(3000);
  38. System.out.println("Server is listening");
  39. TargetDataLine audio = configureStreams();
  40. while(true){
  41. Socket socket = server.accept();
  42. System.out.println("Current accepted socket: "+socket);
  43. HandlingClient client = new HandlingClient(socket, audio);
  44. client.start();
  45. }
  46. }
  47. catch(Exception e){
  48. System.err.println(e);
  49. }
  50. }
  51. }
  52. class HandlingClient extends Thread{
  53. AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
  54. private Socket socket;
  55. private OutputStream output;
  56. private TargetDataLine audio;
  57. HandlingClient(Socket socket, TargetDataLine audio){
  58. this.socket=socket;
  59. this.audio=audio;
  60. }
  61. public void run(){
  62. try{
  63. AudioInputStream ais = new AudioInputStream(audio);
  64. output=socket.getOutputStream();
  65. byte[]data = new byte[1024];
  66. System.out.println("Start recording and sending");
  67. while(true){
  68. data=ais.readNBytes(1024);
  69. output.write(data);
  70. }
  71. }
  72. catch(Exception e){
  73. System.err.println(e);
  74. System.out.println("OutputStream: "+output);
  75. System.out.println("Socket: "+socket);
  76. }
  77. }
  78. }

Android Client:

  1. import androidx.appcompat.app.AppCompatActivity;
  2. import android.os.Bundle;
  3. import android.os.StrictMode;
  4. import android.view.View;
  5. import android.widget.EditText;
  6. import java.io.IOException;
  7. import java.net.Socket;
  8. public class MainActivity extends AppCompatActivity {
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_main);
  13. StrictMode.ThreadPolicy myCustomizableThread = new StrictMode.ThreadPolicy.Builder().permitAll().build();
  14. StrictMode.setThreadPolicy(myCustomizableThread);
  15. //Starting from here
  16. }
  17. public void connect(View view){
  18. final EditText editText = findViewById(R.id.url);
  19. String url = editText.getText().toString().trim(); //User enters the url in the form like 192.168.43.102:3000
  20. String target = url.substring(0, url.indexOf(':'));
  21. int port = Integer.parseInt(url.substring(url.indexOf(':')+1, url.length()));
  22. try {
  23. Socket socket = new Socket(target, port);
  24. //Media player here to play the recording
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. }

huangapple
  • 本文由 发表于 2020年3月16日 20:31:37
  • 转载请务必保留本文链接:https://java.coder-hub.com/60706087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定