简体   繁体   English

如何将视频从Android设备上传到服务器或主机,然后再次检索?

[英]How to upload a video from an Android device, to a server or a host and then retrieve it again?

Now am building an android application where i want the user to upload video to the server, then after uploading it, it should appear on the MainActivity, I've set up the login/register using Firebase but Firebase doesn't support streaming Videos(according to my attempts), thus i've been googling for long time about servers and how servers work with video uploading and streaming but i've reached an end point. 现在正在构建一个Android应用程序,我希望用户将视频上传到服务器,然后将其上传到MainActivity,我已经使用Firebase设置了登录/注册,但是Firebase不支持流视频( (根据我的尝试),因此我一直在搜索服务器以及服务器如何处理视频上传和流式传输,但已经达到了终点。

What i want to do is, building a system where user can upload a video from his local device, into a server, and then downloading(video streaming from a server), so the user later on can interact with it. 我要做的是,建立一个系统,用户可以在其中将视频从其本地设备上载到服务器中,然后下载(从服务器中流式传输视频),以便以后用户可以与它进行交互。 I am not sure how to do it, i have searched a lot but couldn't find what i needed to do/learn 我不确定该怎么做,我已经搜索了很多,但是找不到我需要做的事/学习

How you upload the video and how you make it available are really two separate questions. 如何上传视频以及如何提供视频实际上是两个独立的问题。

For uploading from Android: 要从Android上传:

There are a number of libraries you can use to upload to Android - the code below uses the apache multipart client and it is tested and works. 您可以使用许多库将其上传到Android-下面的代码使用apache multipart客户端,它已经过测试并且可以工作。 Other http libraries include Volley and retrofit. 其他http库包括Volley和翻新。

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;
import android.util.Log;

public class VideoUploadTask extends AsyncTask<String, String, Integer> {
    /* This Class is an AsynchTask to upload a video to a server on a background thread
     * 
     */

    private VideoUploadTaskListener thisTaskListener;
    private String serverURL;
    private String videoPath;

    public VideoUploadTask(VideoUploadTaskListener ourListener) {
        //Constructor
        Log.d("VideoUploadTask","constructor");

        //Set the listener
        thisTaskListener = ourListener;
    }

    @Override
    protected Integer doInBackground(String... params) {
        //Upload the video in the background
        Log.d("VideoUploadTask","doInBackground");

        //Get the Server URL and the local video path from the parameters
        if (params.length == 2) {
            serverURL = params[0];
            videoPath = params[1];
        } else {
            //One or all of the params are not present - log an error and return
            Log.d("VideoUploadTask doInBackground","One or all of the params are not present");
            return -1;
        }


        //Create a new Multipart HTTP request to upload the video
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(serverURL);

        //Create a Multipart entity and add the parts to it
        try {
            Log.d("VideoUploadTask doInBackground","Building the request for file: " + videoPath);
            FileBody filebodyVideo = new FileBody(new File(videoPath));
            StringBody title = new StringBody("Filename:" + videoPath);
            StringBody description = new StringBody("Test Video");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            reqEntity.addPart("videoFile", filebodyVideo);
            reqEntity.addPart("title", title);
            reqEntity.addPart("description", description);
            httppost.setEntity(reqEntity);
        } catch (UnsupportedEncodingException e1) {
            //Log the error
            Log.d("VideoUploadTask doInBackground","UnsupportedEncodingException error when setting StringBody for title or description");
            e1.printStackTrace();
            return -1;
        }

        //Send the request to the server
        HttpResponse serverResponse = null;
        try {
            Log.d("VideoUploadTask doInBackground","Sending the Request");
            serverResponse = httpclient.execute( httppost );
        } catch (ClientProtocolException e) {
            //Log the error
            Log.d("VideoUploadTask doInBackground","ClientProtocolException");
            e.printStackTrace();
        } catch (IOException e) {
            //Log the error
            Log.d("VideoUploadTask doInBackground","IOException");
            e.printStackTrace();
        }

        //Check the response code
        Log.d("VideoUploadTask doInBackground","Checking the response code");
        if (serverResponse != null) {
            Log.d("VideoUploadTask doInBackground","ServerRespone" + serverResponse.getStatusLine());
            HttpEntity responseEntity = serverResponse.getEntity( );
            if (responseEntity != null) {
                //log the response code and consume the content
                Log.d("VideoUploadTask doInBackground","responseEntity is not null");
                try {
                    responseEntity.consumeContent( );
                } catch (IOException e) {
                    //Log the (further...) error...
                    Log.d("VideoUploadTask doInBackground","IOexception consuming content");
                    e.printStackTrace();
                }
            } 
        } else {
            //Log that response code was null
            Log.d("VideoUploadTask doInBackground","serverResponse = null");
            return -1;
        }

        //Shut down the connection manager
        httpclient.getConnectionManager( ).shutdown( ); 
        return 1;
    }

    @Override
    protected void onPostExecute(Integer result) {
        //Check the return code and update the listener
        Log.d("VideoUploadTask onPostExecute","updating listener after execution");
        thisTaskListener.onUploadFinished(result);
    }

To make the video available on the server side you just need a HTTP server that can server static content or a video streaming server - the latter will allow you use Adaptive Bit Rate Streaming (ABR - eg HLS or DASH) for better quality, but may be overkill for your needs. 为了使视频在服务器端可用,您只需要一个HTTP服务器即可提供静态内容或视频流服务器-后者将允许您使用自适应比特率流(ABR-例如HLS或DASH)以获得更好的质量,但是可能满足您的需求。

Either approach will provide you withe a URL for the video which you can use an Android player like ExoPlayer to play back the video with: 两种方法都可以为您提供视频的URL,您可以使用ExoPlayer这样的Android播放器通过以下方式播放视频:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM