无需RTSP即可制作音频流

时间:2014-10-04 16:07:26

标签: audio-streaming tcpsocket

我正在尝试为音频流实现客户端/服务器系统。客户端应该询问文件,服务器应该发送它。然后,客户端应立即播放该文件。 我已经阅读了一些基于rtp和rtsp协议的解决方案,但它们对我的用例来说过于复杂。我想通过TCP套接字使用简单的文件传输,但客户端应该立即播放音频文件,而不必等待下载结束。这种解决方案可行吗?

这是我正在使用的代码。

SERVER:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class ConcurTCPServer implements Runnable 
{
    static ServerSocket listenSocket;
    Socket sock = null;
    static final int port = 12345;
    InputStream recvStream = null;
    OutputStream sendStream = null;
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    String request;
    String response;

    public ConcurTCPServer(Socket s) throws IOException, UnknownHostException 
    {
        sock = s;
        recvStream = sock.getInputStream();
        sendStream = sock.getOutputStream();
    }

    public void run()
    {
        getRequest();
        process();
        sendResponse();
        close();
    }

    void getRequest()
    {
        try
        {
            int dataSize;
            byte[] recvBuff = new byte[1024];
            dataSize = recvStream.read(recvBuff, 0, 1024);
            request = new String(recvBuff, 0, dataSize);
            System.out.println("Request for: " + request);
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }

    void process()
    {
        //TODO
    }

    void sendResponse()
    {       
        try
        {
            File myFile = new File ("/Users/user/Desktop/" + request);
            byte [] mybytearray  = new byte [(int)myFile.length()];
            fis = new FileInputStream(myFile);
            bis = new BufferedInputStream(fis);
            bis.read(mybytearray,0,mybytearray.length);
            System.out.println("Sending..." + myFile.getPath() + ". Dimension: " + myFile.length());
            sendStream.write(mybytearray,0,mybytearray.length);
            sendStream.flush();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    void close()
    {
        try
        {
            System.out.println("Done");
            recvStream.close();
            sendStream.close();
            sock.close();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }

    public static void main(String [] args) throws IOException
    {
        listenSocket = new ServerSocket(port);
        while(true)
        {
            System.out.println("Waiting...");
            ConcurTCPServer server = new ConcurTCPServer(listenSocket.accept());
            System.out.println("Client connected");
            Thread thread = new Thread(server);
            thread.start();
        }
    }
}

客户端:

    import java.net.*;
import java.io.*;

import sun.audio.AudioData;
import sun.audio.AudioDataStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class TCPClient 
{
    Socket sock;
    OutputStream sendStream;
    InputStream recvStream;
    AudioStream audioStream;
    AudioData audiodata;
    AudioDataStream audioDataStream;
    String request;
    String response;

    public TCPClient(String server, int port) throws IOException, UnknownHostException 
    {
        sock = new Socket(server, port);
        sendStream = sock.getOutputStream();
        recvStream = sock.getInputStream();
    }

    void makeRequest(String word)
    {
        request = word;
    }

    void sendRequest()
    {
        try
        {
            byte[] sendBuff = new byte[request.length()];
            sendBuff = request.getBytes();
            System.out.println("Sending request for: " + request);
            sendStream.write(sendBuff, 0, sendBuff.length);
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }

    void getResponse()
    {
        try
        {
            System.out.println("Playing...");
            audioStream = new AudioStream(recvStream);
            audiodata = audioStream.getData();
            audioDataStream = new AudioDataStream(audiodata);
            AudioPlayer.player.start(audioDataStream);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    void close()
    {
        try
        {
            System.out.println("Done");
            if (audioStream != null) audioStream.close();
            sendStream.close();
            recvStream.close();
            sock.close();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException
    {
        final int servPort = 12345;
        final String servName = "localhost";
        TCPClient client = new TCPClient(servName, servPort);
        System.out.println("Connected");
        client.makeRequest("welcome.wav");
        client.sendRequest();
        client.getResponse();
        client.close();
    }
}

它只适用于小.wav,所以我认为该文件实际上并不是在流媒体中播放,而是在下载后立即启动。如果文件大小约为1Mb或更高,则客户端返回异常:

java.io.IOException: could not create AudioData object
at sun.audio.AudioStream.getData(AudioStream.java:125)
at com.dicet.unisalento.TCPClient.getResponse(TCPClient.java:54)
at com.dicet.unisalento.TCPClient.main(TCPClient.java:88)

而服务器返回异常:

java.net.SocketException: Broken pipe
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:113)
at java.net.SocketOutputStream.write(SocketOutputStream.java:159)
at com.dicet.unisalento.ConcurTCPServer.sendResponse(ConcurTCPServer.java:70)
at com.dicet.unisalento.ConcurTCPServer.run(ConcurTCPServer.java:35)
at java.lang.Thread.run(Thread.java:744)

请帮忙吗?

环顾四周,我了解到我需要使用HTTP渐进式下载,但我没有使用HTTP渐进式下载找到任何客户端/服务器示例实现。

0 个答案:

没有答案