0

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 ===============", "==");

        }
    });


}

}

1 Answers1

0

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

Androider
  • 257
  • 3
  • 11
  • thank you for fast reply random "goodguygreg" if it's not a hassle for you can i ask where to start some links perhaps? regarding this socket server thing... – user3213989 Jan 20 '14 at 07:04
  • Here is some links: http://android-er.blogspot.co.il/2011/01/implement-simple-socket-server-in.html http://android-er.blogspot.co.il/2011/01/simple-communication-using.html – Androider Jan 20 '14 at 07:11
  • And some similar question http://stackoverflow.com/questions/2511045/streaming-to-the-android-mediaplayer – Androider Jan 20 '14 at 07:13
  • thank you very much i appreciate your help i will post here regarding my progress. – user3213989 Jan 20 '14 at 07:13
  • @Androider-Yesterday i learned the client and server trick and now able to send text message through both devices and now i'm stuck in passing data file and have few questions.. -First is do i need to chunk my mp4 file to transmit it using packet? -Second i stumble upon the spydroid [link](http://code.google.com/p/spydroid-ipcamera/source/checkout) open source code and wondering if i can use it to convert my video files to streamable media or is this impossible i mean spydroid also uses .mp4 files in streaming right? – user3213989 Jan 22 '14 at 01:54
  • Like you write the text you will need to write stream. In some thread you will "download" stream and each chank will be copyed and write to file and socket server. – Androider Jan 22 '14 at 06:27
  • @Androider-sorry for my persistent but can you give me an example that is working and have comments because i found some sources but they do not provide information or comments for the source so i am not able to study and debug them properly – user3213989 Jan 22 '14 at 09:33