2016-02-20 150 views
0

我試圖製作一個Android應用程序,用戶將在其中錄製短視頻,並在錄製完成後,視頻自動上傳到服務器。我找到了一個完全符合我需要的教程,但是使用圖片而不是視頻。我是否會以同樣的方式處理視頻,或者如何更改教程中的代碼以使其成爲視頻而不是圖片?如何使用Android錄製視頻並上傳到服務器?

教程鏈接:https://www.youtube.com/watch?v=3tEiiUOLemA

+0

此饋送可能適合你 [android-capture-video-and-upload-it-to-server](http://stackoverflow.com/questions/21259799/android-capture-video-and-upload-它到服務器使用單鍵) –

+0

我正在看它,它看起來很熟悉。它是YouTube上的一個教程,似乎大部分代碼已被棄用。我沒有看過這篇文章。我會再次嘗試教程,看看這個問題是否有幫助。謝謝! –

回答

0

我用下面的代碼來自Android的成功將視頻上傳到Web服務器。

請注意,Android中有多個用於HTTP通信的替代庫 - 對於視頻,如果您不想使用下面的選項,並且支持Multipart消息,則需要確保選擇任何一個。在Android的HTTP的一個很好的概述是在這裏:https://packetzoom.com/blog/which-android-http-library-to-use.html

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

} 
+0

如何以及從哪裏獲取params [0]和params [1]值? – deejay

+0

它們作爲參數從任何調用任務傳入。根據您要上傳到的服務器以及視頻在您的設備上的位置,它們會有所不同。 – Mick

0

This is更好的教程,用於實現在應用此功能。但文件//方案現在不允許在API級別24之後。因此,您需要look here ,我希望通過這2個鏈接您將輕鬆實現此目的。

相關問題