How to play Videos ,Music, Image on Netgem STB from Android App
Posted By : Chandan Wadhwa | 19-Aug-2014
To play a Video,Music or Image over NetBox we call a WebService from Android which is provided by Netgem.
Following is the detailed description of webservice
http://netbox_ip/Live/External/play?url=videourl&position=position to start the stream (in milliseconds)
Parameter Description
Name | Required | Description |
position | optional | The position to start the stream (in milliseconds) |
url | mandatory | URL of the stream to play |
Android code to play Audio, Music or Image to Netxbox
(In my tutorial i am going to explain the video playing part)
a) Layout of Video player with a fling button
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <VideoView android:id="@+id/video" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignParentBottom="true"/> <ImageView android:src="@drawable/adpoints_flingbutton" android:id="@+id/btnfling" android:layout_width="50dip" android:layout_height="50dip" android:text="Fling Button" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true"/> </RelativeLayout>
b) Code to play video and when fling button is clicked it will transfer the video from current position over STB
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import com.adPoint.STB.STBMacId; import com.adpointap.parsers.XMLParser; import com.adpointglobal.global.GlobalApis; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import android.widget.VideoView; import android.view.View.OnClickListener; import java.net.*; public class MyTagVideos extends Activity { String videopath=”https://s3-eu-west-1.amazonaws.com/adpoints/Dive_Ford+B+MAX.mp4”; ProgressDialog dialog; VideoView video; ImageView btn; String stbip; //variable to store stb IP int videopos; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_tag_videos); video=(VideoView)findViewById(R.id.video); btn=(ImageView)findViewById(R.id.btnfling); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // To retrieve the current playing position of video videopos=video.getCurrentPosition(); // To pause the video video.pause(); // Asyn Task to post videopath,position to Webservice STBUrlPost stburl=new STBUrlPost(); stburl.execute(); } }); } class STBUrlPost extends AsyncTask<String,Void, String> { @Override protected String doInBackground(String... params) { // To extract only IP part String url=stbip.substring(0,stbip.length()-1)+"/Live/External/play" ; POSTURL_STB(url,videopath,videopos); return null; } } String POSTURL_STB(String url,String videourl,int videotime){ try { // 1. create HttpClient HttpClient httpclient = new DefaultHttpClient(); // 2. make POST request to the given URL //UTF-8 encoding in applied on videourl variable to remove encoded character from url videourl=URLEncoder.encode(videourl, "UTF-8"); String posturl=url+"?url="+videourl+"&position="+videotime; HttpPost httpPost = new HttpPost(posturl); // 3. Execute POST request to the given URL HttpResponse httpResponse = httpclient.execute(httpPost); } catch (Exception e) { Log.d("InputStream", e.getLocalizedMessage()); } }
Thanks
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Chandan Wadhwa
Chandan is an Android Apps developer with good experience in building native Android applications.