2014-01-20 95 views
-1

我是一名Android開發新手,遇到這個問題我的客戶希望從他的攝像頭存儲流視頻文件到他的android手機只使用局域網連接..這可能嗎?現在我唯一能做的就是從手機存儲器中播放視頻並傳輸http或RTSP流視頻,但是可以在通過LAN發送視頻文件的同時流式傳輸視頻文件嗎?謝謝。Android:使用LAN下載播放mp4視頻文件可能嗎?

@ Androider-I對不予評論表示歉意,因爲我無法反正這是我現在的代碼,任何形式的幫助將不勝感激。 編輯:

客戶端

` 公共類客戶端擴展活性狀況 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(); 
} 

服務器端

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.

而在它之後崩潰客戶端我發送,不確定是否s是一個錯誤,因爲我的服務器有問題....

@Androider -Sorry for late update,這是我的結果代碼,我現在能夠通過視頻文件,並通過位控制它,但問題是我的數據總是被損壞,因爲丟失了一個字節或類似的東西,現在我該如何傳輸它?我無法播放該文件,因爲它不完整?如果是這種情況,那麼字節控制的目的是什麼?我希望你能再次幫助我thx。

代碼客戶端和服務器:

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

     } 
    }); 


} 

}

回答

0

其實這是。您需要在應用程序中構建自己的套接字服務器並從中播放視頻。然後你將有控制字節輸入流和保存dowloaded部分文件,而同一部分將去mediaplayer

+0

感謝您快速回復隨機「goodguygreg」,如果它不是一個麻煩你可以問我在哪裏開始一些鏈接也許?關於這個套接字服務器的東西... – user3213989

+0

這裏有一些鏈接: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

+0

和一些類似的問題http://stackoverflow.com/questions/2511045/streaming-to-the-android-mediaplayer – Androider