【问题标题】:Android:Playing a mp4 Video file while downloading it using LAN is Possible?Android:可以在使用 LAN 下载的同时播放 mp4 视频文件吗?
【发布时间】:2014-02-09 05:50:45
【问题描述】:

我是 android 开发的新手,遇到这个问题我的客户希望仅使用 LAN 连接将视频文件从他的相机存储流式传输到他的 android 手机。这可能吗?现在我唯一能做的就是从手机存储中播放视频并流式传输 http 或 RTSP 流式传输视频,但是是否可以在通过 LAN 发送视频文件时流式传输视频文件?谢谢。

@Androider-我很抱歉没有发表评论,因为我现在不能这是我的代码,任何形式的帮助都将不胜感激,谢谢。 编辑:

客户端

` 公共类客户端扩展了 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();
}

服务器端

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

}

错误服务器端

[2014-01-22 15:20:15 - AndroidSocketSERVER] ActivityManager: 开始: 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:错误类型 3

[2014-01-22 15:20:15 - AndroidSocketSERVER] ActivityManager:错误:Activity 类 {com.javacodegeeks.android.androidsocketserver/com.javacodegeeks.android.androidsocketserver.Server} 不存在。

在我发送后它在客户端崩溃。不确定这是否是一个错误,因为我的服务器有问题....

@Androider - 抱歉更新晚了,这是我的结果代码,我现在可以传递视频文件并按位控制它,但问题是我的数据总是因为缺少字节或类似的东西而损坏现在我该如何流式传输?我无法播放该文件,因为它不完整?如果是这样,那么字节控制的目的是什么?我希望你能再次帮助我谢谢。

代码客户端和服务器:

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

        }
    });


}

}

【问题讨论】:

    标签: android video stream download mp4


    【解决方案1】:

    其实是这样的。您需要在应用程序中构建自己的套接字服务器并从中播放视频。然后您将控制字节输入流并将下载的部分保存到文件中,而同一部分将转到媒体播放器

    【讨论】:

    • 感谢您快速回复随机“goodguygreg”,如果这对您来说不麻烦的话,请问从哪里开始一些链接?关于这个套接字服务器的事情......
    • 还有一些类似的问题stackoverflow.com/questions/2511045/…
    • 非常感谢您的帮助,我将在此处发布有关我的进度的信息。
    • @Androider-昨天我学习了客户端和服务器技巧,现在能够通过这两个设备发送短信,现在我被困在传递数据文件中并且有几个问题.. -首先是我需要分块我的 mp4 文件以使用数据包传输它吗? -其次,我偶然发现了 spydroid link 开源代码,我想知道我是否可以使用它来将我的视频文件转换为流媒体,或者这不可能,我的意思是 spydroid 也在流媒体中使用 .mp4 文件吗?
    猜你喜欢
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    相关资源
    最近更新 更多