从优库开发者网站下下来优库的SDK,直接将YoukuLoginSDK,YoukuPlayerOpenSDK,PlayerDemo依次导入到Eclipse中。其中YoukuLoginSDK,YoukuPlayerOpenSDK是作为PlayerDemo的library库的。还有就是在YoukuPlayerOpenSDK开发指南中,也有关于视频下载缓存,视频播放等的介绍。比较简单易懂,也就不再多做说明了。
在这里说明一下我在PlayerDemo的基础上新添加的一个游戏视频显示按钮-----它从优库视频中搜索出了5条游戏视频,通过http的get方式(优库API中有)。先上代码:
package com.example.playerdemo;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class DotaActivity extends Activity {
private ListView listView;
private Handler handler;
private static List<Map<String, String>> list;
private SimpleAdapter simpleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.dota_activity);
list = new ArrayList<Map<String, String>>();
listView = (ListView) findViewById(R.id.list);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 0:
break;
case 1:
simpleAdapter = new SimpleAdapter(getApplicationContext(),
list, android.R.layout.simple_list_item_2,
new String[] { "title", "duration" }, new int[] {
android.R.id.text1, android.R.id.text2 });
listView.setAdapter(simpleAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Log.i("jiangguohu","list.get(arg2).get(id) == "+list.get(arg2).get("id"));
Intent i = new Intent(DotaActivity.this, PlayerActivity.class);
i.putExtra("vid", list.get(arg2).get("id").trim());
DotaActivity.this.startActivity(i);
}
});
break;
default:
break;
}
}
};
thread.start();
}
/**
* 把输入流转换成字符数组
*
* @param inputStream
* 输入流
* @return 字符数组
* @throws Exception
*/
public static byte[] readStream(InputStream inputStream) throws Exception {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
bout.write(buffer, 0, len);
}
bout.close();
inputStream.close();
return bout.toByteArray();
}
Thread thread = new Thread(new Runnable() {
public void run() {
boolean b = false;
try {
Map<String, String> params = new HashMap<String, String>();
params.put("client_id", "XXXXXXXXXXXXXXXXXXXX");//换成你应用的client_id
params.put("count", "5");
params.put("category", "游戏");
// params.put("page", "1");
b = sendGETRequest(
"https://openapi.youku.com/v2/videos/by_category.json",
params);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("jiangguohu", "exception2 e = " + e);
}
if (b) {
handler.sendEmptyMessage(1);
} else {
handler.sendEmptyMessage(0);
}
}
});
private static boolean sendGETRequest(String path,
Map<String, String> params) throws Exception {
// 发送地http://192.168.100.91:8080/videoService/login?username=abc&password=123
// StringBuilder是用来组拼请求地址和参数
StringBuilder sb = new StringBuilder();
sb.append(path).append("?");
if (params != null && params.size() != 0) {
for (Map.Entry<String, String> entry : params.entrySet()) {
// 如果请求参数中有中文,需要进行URLEncoder编码
sb.append(entry.getKey()).append("=")
.append(URLEncoder.encode(entry.getValue(), "utf-8"));
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
}
URL url = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
Log.i("jiangguohu", "conn.getResponseCode() == 200");
InputStream is = conn.getInputStream(); // 获取输入流
byte[] data = readStream(is); // 把输入流转换成字符数组
String json = new String(data); // 把字符数组转换成字符串
Log.i("jiangguohu", "json = " + json);
JSONObject jsonObject = new JSONObject(json);
JSONArray jsonArray = (JSONArray) jsonObject.opt("videos");
Map<String, String> map = null;
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject item = jsonArray.getJSONObject(i); // 每条记录又由几个Object对象组成
String title = item.getString("title"); // 获取对象对应的标题
String id = item.getString("id");
String link = item.getString("link");
int duration = item.getInt("duration");
Log.i("jiangguohu","item.getString category = "+item.getString("category"));
map = new HashMap<String, String>(); // 存放到MAP里面
map.put("id", id);
map.put("title", title);
map.put("link", link);
map.put("duration", duration+"");
list.add(map);
}
return true;
}
return false;
}
}
其中json对应的字段名字等在优库OpenAPI中都有详细说明。本来是想去搜索DOTA视频的,可是当我设置params.put("category", "DOTA");之后没有搜到,搜出来的结果是乱七八糟的东西。有待改进。