How to upload a video from an Android device, to a server or a host and then retrieve it again? -
now building android application want user upload video server, after uploading it, should appear on mainactivity, i've set login/register using firebase firebase doesn't support streaming videos(according attempts), i've been googling long time servers , how servers work video uploading , streaming i've reached end point.
what want is, building system user can upload video local device, server, , downloading(video streaming server), user later on can interact it. not sure how it, have searched lot couldn't find needed do/learn
how upload video , how make available 2 separate questions.
for uploading android:
there number of libraries can use upload android - code below uses apache multipart client , tested , works. other http libraries include volley , retrofit.
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> { /* class asynchtask upload video server on background thread * */ private videouploadtasklistener thistasklistener; private string serverurl; private string videopath; public videouploadtask(videouploadtasklistener ourlistener) { //constructor log.d("videouploadtask","constructor"); //set listener thistasklistener = ourlistener; } @override protected integer doinbackground(string... params) { //upload video in background log.d("videouploadtask","doinbackground"); //get server url , local video path parameters if (params.length == 2) { serverurl = params[0]; videopath = params[1]; } else { //one or of params not present - log error , return log.d("videouploadtask doinbackground","one or of params not present"); return -1; } //create new multipart http request upload video httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(serverurl); //create multipart entity , add parts try { log.d("videouploadtask doinbackground","building request 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 error log.d("videouploadtask doinbackground","unsupportedencodingexception error when setting stringbody title or description"); e1.printstacktrace(); return -1; } //send request server httpresponse serverresponse = null; try { log.d("videouploadtask doinbackground","sending request"); serverresponse = httpclient.execute( httppost ); } catch (clientprotocolexception e) { //log error log.d("videouploadtask doinbackground","clientprotocolexception"); e.printstacktrace(); } catch (ioexception e) { //log error log.d("videouploadtask doinbackground","ioexception"); e.printstacktrace(); } //check response code log.d("videouploadtask doinbackground","checking response code"); if (serverresponse != null) { log.d("videouploadtask doinbackground","serverrespone" + serverresponse.getstatusline()); httpentity responseentity = serverresponse.getentity( ); if (responseentity != null) { //log response code , consume content log.d("videouploadtask doinbackground","responseentity not null"); try { responseentity.consumecontent( ); } catch (ioexception e) { //log (further...) error... log.d("videouploadtask doinbackground","ioexception consuming content"); e.printstacktrace(); } } } else { //log response code null log.d("videouploadtask doinbackground","serverresponse = null"); return -1; } //shut down connection manager httpclient.getconnectionmanager( ).shutdown( ); return 1; } @override protected void onpostexecute(integer result) { //check return code , update listener log.d("videouploadtask onpostexecute","updating listener after execution"); thistasklistener.onuploadfinished(result); }
to make video available on server side need http server can server static content or video streaming server - latter allow use adaptive bit rate streaming (abr - e.g. hls or dash) better quality, may overkill needs.
either approach provide withe url video can use android player exoplayer play video with:
Comments
Post a Comment