• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

how to plal video listview from json file

Hallo91

Lurker
May 14, 2018
8
0
I have an exoplayer example which plays an Iptv live stream from a link.
Full code has only 1 java + xml activity, now i want to use a json file to load a list of items "links" , and when i click on item, takes me to the exoplayer activity . so i did this :

1. i downloaded a json code and customize it as i want with title + discription(discription as link).

i want to click on discription item, to play it's video link, any help please :


Here is my json file:

{
"contacts": [
{
"title": "video 1",
"discription": "http://117.196.231.0:86/hls/10.m3u8"
},
{
"title": "video 2",
"discription": "http://117.196.212.0:86/hls/54.m3u8"
}
]
}

Samlpe Activity :

public class normal_iptv extends Activity implements VideoRendererEventListener {


private SimpleExoPlayerView simpleExoPlayerView;
private SimpleExoPlayer player;

Uri mp4_Video_link = Uri.parse("http://117.196.231.0:86/hls/10.m3u8");

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.normal_iptv);

BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

final LoadControl loadControl = new DefaultLoadControl();

player = ExoPlayerFactory.newSimpleInstance(this, trackSelector, loadControl);
simpleExoPlayerView = new SimpleExoPlayerView(this);
simpleExoPlayerView = findViewById(R.id.player_view);

//Set media controller
simpleExoPlayerView.setUseController(true);
simpleExoPlayerView.requestFocus();

// Bind the player to the view.
simpleExoPlayerView.setPlayer(player);

DefaultBandwidthMeter bandwidthMeterA = new DefaultBandwidthMeter();
//Produces DataSource instances through which media data is loaded.
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, "exoplayer2example"), bandwidthMeterA);

//FOR LIVESTREAM LINK:
MediaSource videoSource = new HlsMediaSource(mp4_Video_link, dataSourceFactory, 1, null, null);
final LoopingMediaSource loopingSource = new LoopingMediaSource(videoSource);

// Prepare the player with the source.
player.prepare(loopingSource);

player.addListener(new ExoPlayer.EventListener() {
@override
public void onTimelineChanged(Timeline timeline, Object manifest) {
}

@override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}

@override
public void onLoadingChanged(boolean isLoading) {
}

@override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
}

@override
public void onRepeatModeChanged(int repeatMode) {
}

@override
public void onPlayerError(ExoPlaybackException error) {
player.stop();
player.prepare(loopingSource);
player.setPlayWhenReady(true);
}

@override
public void onPositionDiscontinuity() {
}

@override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
});

player.setPlayWhenReady(true);
player.setVideoDebugListener(this);
}

@override
public void onVideoEnabled(DecoderCounters counters) {

}

@override
public void onVideoDecoderInitialized(String decoderName, long initializedTimestampMs, long initializationDurationMs) {

}

@override
public void onVideoInputFormatChanged(Format format) {

}

@override
public void onDroppedFrames(int count, long elapsedMs) {

}

@override
public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) {

}

@override
public void onRenderedFirstFrame(Surface surface) {

}

@override
public void onVideoDisabled(DecoderCounters counters) {

}


@override
protected void onStop() {
super.onStop();

}

@override
protected void onStart() {
super.onStart();

}

@override
protected void onResume() {
super.onResume();

}

@override
protected void onPause() {
super.onPause();

}

@override
protected void onDestroy() {
super.onDestroy();
player.release();
}

}


json activity

/// json file link
private static String url = "https://www.dropbox.com/s/loleo014140mifa";

ArrayList<HashMap<String, String>> contactList;
ListAdapter adapter;
private String TAG = normal_json_main.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.normal_json_main);

contactList = new ArrayList<>();
lv = findViewById(R.id.list);
new GetContacts().execute();
}

private class GetContacts extends AsyncTask<Void, Void, Void> {

@override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(normal_json_main.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(true);
pDialog.show();

}

@override
protected Void doInBackground(Void... arg0) {
normal_json_HttpHandler sh = new json_HttpHandler();

/// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);

Log.e(TAG, "Response from url: " + jsonStr);

if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);

/// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("contacts");

/// looping through All Contacts
for (int i = 0; i < contacts.length(); i++)
JSONObject c = contacts.getJSONObject(i);

String video_title = c.getString("title");
String video_discription = c.getString("discription");


/// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();

/// adding each child node to HashMap key => value
contact.put("title", video_title);
contact.put("discription", video_discription);

/// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@override
public void run() {
Toast.makeText(getApplicationContext(), "Json parsing error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});

}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@override
public void run() {
Toast.makeText(getApplicationContext(), "Something Went wrong!", Toast.LENGTH_LONG).show();
}
});

}

return null;
}

@override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();


adapter = new SimpleAdapter(normal_json_main.this, contactList,R.layout.normal_json_list_item, new String[]{"title", "discription"}, new int[]{R.id.vid_title,
R.id.vid_disc});

lv.setAdapter(adapter);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

///=== on item click play the video

}

});

}

}
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones