質問

I'm a newbie in android development and encounter this problem my client wish to stream video files from his camera storage to his android phone using only LAN connection.. is this possible? for now the only thing that i can do is play a video from the storage of the phone and stream http or RTSP streams video but is it possible to stream a video file while sending it through LAN? thank you.

@Androider-I apologize for not commenting because i can't anyway this is my code for now and any kind of help will be appreciated thank you. Edited:

Client Side

` public class Client extends Activi ty {

private Socket client;
 private FileInputStream fileInputStream;
 private BufferedInputStream bufferedInputStream;
 private OutputStream outputStream;
 private Button button;
 private TextView text;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  button = (Button) findViewById(R.id.button1);   
  text = (TextView) findViewById(R.id.textView1);   

  //Button press event listener
  button.setOnClickListener(new View.OnClickListener() {

   public void onClick(View v) {




File file = new File("/storage/emulated/BaseAhri.jpg"); 

try {

 client = new Socket("10.0.2.2", 4444);

 byte[] mybytearray = new byte[(int) file.length()]; 

 fileInputStream = new FileInputStream(file);
 bufferedInputStream = new BufferedInputStream(fileInputStream); 

 bufferedInputStream.read(mybytearray, 0, mybytearray.length); 

 outputStream = client.getOutputStream();

 outputStream.write(mybytearray, 0, mybytearray.length);
 outputStream.flush();
 bufferedInputStream.close();
 outputStream.close();
       client.close();

       text.setText("File Sent");


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

Server Side

private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStream inputStream;
private static FileOutputStream fileOutputStream;
private static BufferedOutputStream bufferedOutputStream;
private static int filesize = 10000000; 
private static int bytesRead;
private static int current = 0;

public static void main(String[] args) throws IOException {


    serverSocket = new ServerSocket(4444); 

    System.out.println("Server started. Listening to the port 4444");


    clientSocket = serverSocket.accept();


    byte[] mybytearray = new byte[filesize];  

    inputStream = clientSocket.getInputStream();
    fileOutputStream = new FileOutputStream("/sdcard/DCIM/Camera/BaseAhri.jpg");
    bufferedOutputStream = new BufferedOutputStream(fileOutputStream);});
   System.out.println("Receiving...");


    bytesRead = inputStream.read(mybytearray, 0, mybytearray.length);
    current = bytesRead;

    do {
        bytesRead = inputStream.read(mybytearray, current, (mybytearray.length - current));
        if (bytesRead >= 0) {
            current += bytesRead;
        }
    } while (bytesRead > -1);


    bufferedOutputStream.write(mybytearray, 0, current);
    bufferedOutputStream.flush();
    bufferedOutputStream.close();
    inputStream.close();
    clientSocket.close();
    serverSocket.close();

    System.out.println("Sever recieved the file");

}

Error Server Side

[2014-01-22 15:20:15 - AndroidSocketSERVER] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.javacodegeeks.android.androidsocketserver/.Server }

[2014-01-22 15:20:15 - AndroidSocketSERVER] ActivityManager: Error type 3

[2014-01-22 15:20:15 - AndroidSocketSERVER] ActivityManager: Error: Activity class {com.javacodegeeks.android.androidsocketserver/com.javacodegeeks.android.androidsocketserver.Server} does not exist.

And in the client side it crashes after i send.not sure if this is an error since my server have a problem....

@Androider -Sorry for late update,Here is my resulting code where i am now able to pass the video file and control it by bits but the problem is my data are always corrupted because of a missing byte or something like that and now how do i stream it? i am not able to play the file because it is not complete? if that's the case then what is the purpose of byte controll? i hope you can help me again thx.

COde Client and Server:

serverTransmitButton = (Button) findViewById(R.id.button_TCP_server);
    serverTransmitButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i("Start Server Button Clicked", "yipee");

            try {
                // create socket
                // TODO: the port should match the one in Client
                ServerSocket servsock = new ServerSocket(5005);
                while (true) {
                    Log.i("************", "Waiting...");

                    Socket sock = servsock.accept(); // blocks until connection opened
                    Log.i("************", "Accepted connection : " + sock);

                    // sendfile

                    // TODO: put the source of the file

                        int filesize=8192;
                    File myFile = new File ("/sdcard/DCIM/Camera/test.mp4");
                    byte [] mybytearray  = new byte [filesize];

                    Log.i("####### file length = ", String.valueOf(myFile.length()) );
                    FileInputStream fis = new FileInputStream(myFile);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    bis.read(mybytearray,0,mybytearray.length);
                    OutputStream os = sock.getOutputStream();
                    Log.i("************", "Sending...");
                    int read;
                    while((read = fis.read(mybytearray)) != -1){
                    os.write(mybytearray,0,read);


                    }

                    os.flush();
                    os.close();
                    fis.close();
                    bis.close();
                }   
            } catch (IOException e) {
                Log.i("Io execption ", "e: " + e);
            }

            Log.i("=============== the end of start ==============", "==");
        }   
    });

    clientReceiveButton = (Button) findViewById(R.id.button_TCP_client);
    clientReceiveButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i("Read Button Clicked", "yipee");

            try {
                int bufferSize;// filesize temporary hardcoded

                long start = System.currentTimeMillis();
                int bytesRead;
                int current;
                // localhost for testing
                // TODO: server's IP address. Socket should match one above in server
                Socket sock = new Socket("192.168.123.186",5005);

                Log.i("************", "Connecting...");

                // receive file
                bufferSize=sock.getReceiveBufferSize();
                byte [] mybytearray  = new byte [bufferSize];
                InputStream is = sock.getInputStream();

                FileOutputStream fos = new FileOutputStream("/storage/emulated/0/testinggo.mp4");
                BufferedOutputStream bos = new BufferedOutputStream(fos);
                bytesRead = is.read(mybytearray,0,mybytearray.length);


                while((current = is.read(mybytearray)) >0){
                bos.write(mybytearray, 0 , current);
                }

                bos.flush();
                bos.close();
                is.close();
                                    sock.close




            } catch ( UnknownHostException e ) {
                Log.i("******* :( ", "UnknownHostException");
            } catch (IOException e){
                Log.i("Read has IOException", "e: " + e);
            }

            Log.i("=============== the end of read ===============", "==");

        }
    });


}

}

役に立ちましたか?

解決

Actually it is. You will need to build your own socket server in app and play video from it. Then you will have control over byte input stream and save dowloaded part to file while same part will go to mediaplayer

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top