2017-09-10 8 views
0

이제 사용자가 비디오를 서버에 업로드하고, 업로드 한 후에 MainActivity에 나타나야하고, Firebase를 사용하여 로그인/등록을 설정했지만 Firebase가 스트리밍을 지원하지 않습니다. 동영상 (내 시도에 따라), 따라서 나는 서버에 대해 오랫동안 인터넷 검색을 해왔고 비디오 업로드 및 스트리밍으로 서버가 어떻게 작동 하는지를 알았지 만 끝점에 도달했습니다.Android 기기에서 서버 또는 호스트로 동영상을 업로드 한 다음 다시 검색하는 방법은 무엇입니까?

내가하고 싶은 일은 사용자가 로컬 장치에서 서버로 비디오를 업로드 한 다음 서버에서 비디오 스트리밍을 다운로드하여 나중에 사용자가 해당 시스템과 상호 작용할 수있는 시스템을 구축하는 것입니다. 나는 그것을 어떻게하는지 모르겠다. 나는 많은 것을 검색했지만해야 할 일/발견 할만한 것을 찾지 못했다.

답변

1

비디오를 업로드하는 방법과 비디오를 업로드하는 방법은 실제로 두 가지 별도의 질문이다. 안드로이드에서 업로드를 들어

:

당신은 안드로이드에 업로드하는 데 사용할 수있는 라이브러리가 있습니다

- 아래의 코드는 아파치 다중 클라이언트를 사용하며, 테스트 및 작동한다가. 다른 http 라이브러리에는 발리 (Volley)와 개조가 포함됩니다. 예를 들어, HLS 또는 DASH - 후자는 적응 비트 전송률 스트리밍 (ABR을 사용할 수 있습니다 -

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

서버 정적 콘텐츠 또는 비디오 스트리밍 서버가 할 수 있습니다 그냥 HTTP 서버를 필요로하는 서버 측의 비디오를 사용할 수 있도록하려면) 더 나은 품질을 위해,하지만 당신의 필요에 과잉 될 수 있습니다.

:

하나의 접근 방식은 당신이 비디오를 재생하는 당신이 ExoPlayer처럼 안드로이드 플레이어를 사용할 수있는 동영상의 URL을가는 가지 제공