我是android的新手,并且我在其中堆叠:
我有一个带有多个按钮的活动,“每个按钮都有视频链接” ,当我单击它时,将转到player_activity进行播放。
因此,我不想使用多个按钮,而是要使用json文件列出视频并阅读它们。
1)我下载了json代码并根据需要对其进行了自定义。
2) Json的“标题+标题效果很好”。
3)我在json中添加了一个网址“我就把它放在那里”。
现在,我想在读取json时使该URL工作,当我单击任何项目时,它都会读取其链接。
这是我的json文件 :
{
"contacts": [
{
"title": "video Title 1",
"discription": "discription here"
"link" :"http://117.196.231.0:86/hls/10.m3u8"
},
{
"title": "video Title 2",
"discription": "discription here"
"link" :"http://117.196.231.0:86/hls/10.m3u8"
}
]
}
这是带有链接的活动 :
final String link_1 = "http://117.196.231.0:86/hls/10.m3u8";
final String link_2 = "http://117.196.231.0:86/hls/10.m3u8";
final String link_3 = "http://117.196.231.0:86/hls/10.m3u8";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pro_activity_main);
Button btn1 = findViewById(R.id.iptv1);
Button btn2 = findViewById(R.id.iptv2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(getApplicationContext(), activity_VideoPlayer.class).setData(Uri.parse(link_1))
.putExtra(activity_VideoPlayer.CONTENT_ID_EXTRA, 0)
.putExtra(activity_VideoPlayer.CONTENT_TYPE_EXTRA, DemoUtil.TYPE_HLS);
startActivity(intent);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(getApplicationContext(), activity_VideoPlayer.class).setData(Uri.parse(link_2))
.putExtra(activity_VideoPlayer.CONTENT_ID_EXTRA, 0)
.putExtra(activity_VideoPlayer.CONTENT_TYPE_EXTRA, DemoUtil.TYPE_HLS);
startActivity(intent);
}
});
}
}
json主要活动 :
private static String url = "https://www.dropbox.com/s/q8rttgi4vl8a6at/help_here.json?dl=0";
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 normal_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);
}
}
}
json HttpHandler :
private static final String TAG = normal_json_HttpHandler.class.getSimpleName();
public normal_json_HttpHandler() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
任何帮助将不胜感激。谢谢
答案 0 :(得分:0)
一种方法是在同一json中以秒为单位显示视频的时长:
{
"contacts": [
{
"title": "video Title 1",
"discription": "discription here"
"link" :"http://117.196.231.0:86/hls/10.m3u8"
"duration" :"15"
},
{
"title": "video Title 2",
"discription": "discription here"
"link" :"http://117.196.231.0:86/hls/10.m3u8"
"duration" :"30"
}
]
}
播放第一个视频时,您可以安排第二个视频在第一个视频时间过去后使用计时器运行/播放:
startTimer(content.getDuration());
您可以这样设置一个计时器,以确保前一个计时器被销毁:
public void startTimer(float delay) {
float intDelay = delay * 1000;
//set a new Timer
if (mainTimer != null) {
mainTimer.cancel();
mainTimer.purge();
mainTimer = null;
}
mainTimer = new Timer();
mainTimer.schedule(new TimerTask() {
@Override
public void run() {
h.post(new Runnable() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
playContent();
}
});
}
});
}
}, (int) intDelay);
}
仅通过将uri设置为视频数据源,方法playContent()才会负责。