标题翻译
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();
}
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论