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

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

Live Streaming audio from java server to Android Client

问题

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

我想从我的计算机流式传输音频到安卓客户端我已经获取了输入现在我希望将其通过流传输并播放现在我的问题是在输入端我有AudioInputStream类和其他类我能够使用它们但在安卓端它说它们是私有的无法在类外部访问我想在安卓上播放传入的音频我应该怎么做我现在只想支持一个客户端

服务器

import java.net.*;
import java.io.*;
import javax.sound.sampled.*;
public class MainServer{
    // ...(您提供的服务器代码)
}
class HandlingClient extends Thread{
    // ...(您提供的HandlingClient类的代码)
}

安卓客户端

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.EditText;

import java.io.IOException;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {
    // ...(您提供的MainActivity类的代码)
}

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

英文翻译

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:

import java.net.*;
import java.io.*;
import javax.sound.sampled.*;
public class MainServer{
    private static AudioFormat getAudioFormat() {
        AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
        float rate = 44100.0f;
        int channels = 2;
        int sampleSize = 16;
        boolean bigEndian = true;
        InetAddress addr;
        AudioFormat format = new AudioFormat(encoding, rate, sampleSize, channels, (sampleSize / 8) * channels, rate, bigEndian);
        return format;
    }
    private static TargetDataLine configureStreams(){
        try{
            AudioFormat format = getAudioFormat();
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
            // checks if system supports the data line
            if (!AudioSystem.isLineSupported(info)) {
                System.out.println("Line not supported");
                System.exit(0);
            }
            TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info);
            line.open(format);
            line.start();   
            System.out.println("Got the line and started to listen");
            return line;
        }
        catch(Exception e){
            System.err.println(e);
        }
        return null;
    }
    public static void main(String args[]){
        try{
            ServerSocket server = new ServerSocket(3000);
            System.out.println("Server is listening");
            TargetDataLine audio = configureStreams();
            while(true){
                Socket socket = server.accept();
                System.out.println("Current accepted socket: "+socket);
                HandlingClient client = new HandlingClient(socket, audio);
                client.start();
            }
        }
        catch(Exception e){
            System.err.println(e);
        }
    }
}
class HandlingClient extends Thread{
    AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
    private Socket socket;
    private OutputStream output;
    private TargetDataLine audio;
    HandlingClient(Socket socket, TargetDataLine audio){
        this.socket=socket;
        this.audio=audio;
    }
    public void run(){
        try{
            AudioInputStream ais = new AudioInputStream(audio);
            output=socket.getOutputStream();
            byte[]data = new byte[1024];
            System.out.println("Start recording and sending");
            while(true){
                data=ais.readNBytes(1024);
                output.write(data);
            }
        }
        catch(Exception e){
            System.err.println(e);
            System.out.println("OutputStream: "+output);
            System.out.println("Socket: "+socket);
        }
    }
}

Android Client:

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.EditText;

import java.io.IOException;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        StrictMode.ThreadPolicy myCustomizableThread = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(myCustomizableThread);
        //Starting from here
    }


    public void connect(View view){
        final EditText editText = findViewById(R.id.url);
        String url = editText.getText().toString().trim(); //User enters the url in the form like 192.168.43.102:3000
        String target = url.substring(0, url.indexOf(':'));
        int port = Integer.parseInt(url.substring(url.indexOf(':')+1, url.length()));
        try {
            Socket socket = new Socket(target, port);
            //Media player here to play the recording

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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:

确定