java如何在我的应用程序中嵌入youtube,现在它将进入youtube网站
实际上,我是从json获取url,现在在我的listview中,它显示了json解析中的所有youtube url列表。点击url后,它将进入youtube页面,视频正在播放,我不想从我的应用程序进入其他网站,视频必须在我的应用程序中显示,因为我将如何在我的应用程序中嵌入youtube
如何在我的listview中显示youtube视频列表,现在它显示url,我希望它必须显示listview的小视频如果我们单击视频,它将使用嵌入在我的应用程序中播放youtube视频
我的活动。爪哇
public class PoojaVideos extends Activity implements FetchDataListener1 {
private ProgressDialog dialog;
ListView lv2;
private List<Application1> items;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item1);
lv2 =(ListView)findViewById(R.id.listV_main);
lv2.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv2.getItemAtPosition(position);
Application1 obj_itemDetails = (Application1)o;
final Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(((Application1) o).getUrlWiki()));
startActivity(i);
}
});
//praycount.setOnClickListener(this);
initView();
}
private void initView(){
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://www.ginfy.com/api/v1/videos.json";
FetchDataTask1 task = new FetchDataTask1(this);
task.execute(url);
}
@Override
public void onFetchComplete(List<Application1> data) {
this.items = data;
// dismiss the progress dialog
if ( dialog != null )
dialog.dismiss();
// create new adapter
ApplicationAdapter1 adapter = new ApplicationAdapter1(this, data);
// set the adapter to list
lv2.setAdapter(adapter);
}
@Override
public void onFetchFailure(String msg) {
if ( dialog != null )
dialog.dismiss();
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
} This is the activity for showing listview and after clicking the item in list it is going to youtube page.
我的布局。xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
>
<ListView
安卓:layout_height="wrap_content"
安卓:layout_width="fill_parent" 安卓:id="@+id/listV_main"/>
</LinearLayout>
我不想进入youtube网站,视频必须显示在我的应用程序中,如何使用嵌入
应用程序适配器。爪哇
public class ApplicationAdapter1 extends ArrayAdapter<Application1> {
private List<Application1> items;
private LayoutInflater inflator;
private PoojaVideos activity;
private ProgressDialog dialog;
public ApplicationAdapter1(PoojaVideos context, List<Application1> items){
super(context, R.layout.activity_row1, items);
this.items = items;
inflator = LayoutInflater.from(getContext());
activity=context;
}
@Override
public int getCount(){
return items.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
//View v = convertView;
if ( convertView == null ){
convertView = inflator.inflate(R.layout.activity_row1, null);
holder = new ViewHolder();
holder.prayersLinkWiki = (TextView) convertView.findViewById(R.id.prayersLinkWiki);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
Application1 app1 = items.get(position);
holder.prayersLinkWiki.setText(Html.fromHtml(app1.getUrlWiki()));
return convertView;
}
class ViewHolder {
public TextView prayersLinkWiki;
}
//return convertView;
}
获取数据任务1。爪哇
public class FetchDataTask1 extends AsyncTask<String, Void, String>
{
private final FetchDataListener1 listener;
private OnClickListener onClickListener;
private String msg;
public FetchDataTask1(FetchDataListener1 listener)
{
this.listener = listener;
}
@Override
protected String doInBackground(String... params)
{
if ( params == null )
return null;
// get url from params
String url = params[0];
try
{
// create http connection
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
// connect
HttpResponse response = client.execute(httpget);
// get response
HttpEntity entity = response.getEntity();
if ( entity == null )
{
msg = "No response from server";
return null;
}
// get response content and convert it to json string
InputStream is = entity.getContent();
return streamToString(is);
}
catch ( IOException e )
{
msg = "No Network Connection";
}
return null;
}
@Override
protected void onPostExecute(String sJson)
{
if ( sJson == null )
{
if ( listener != null )
listener.onFetchFailure(msg);
return;
}
try
{
// convert json string to json object
JSONObject jsonObject = new JSONObject(sJson);
JSONArray aJson = jsonObject.getJSONArray("youtube_url");
// create apps list
List<Application1> apps = new ArrayList<Application1>();
for ( int i = 0; i < aJson.length(); i++ )
{
JSONObject json = aJson.getJSONObject(i);
Application1 app1 = new Application1();
app1.setUrlWiki("https://www.youtube.com/watch?v="+json.getString("youtube_url"));
// add the app to apps list
apps.add(app1);
}
//notify the activity that fetch data has been complete
if ( listener != null )
listener.onFetchComplete(apps);
}
catch ( JSONException e )
{
e.printStackTrace();
msg = "Invalid response";
if ( listener != null )
listener.onFetchFailure(msg);
return;
}
}
/**
* This function will convert response stream into json string
*
* @param is
* respons string
* @return json string
* @throws IOException
*/
public String streamToString(final InputStream is) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ( (line = reader.readLine()) != null )
{
sb.append(line + "\n");
}
}
catch ( IOException e )
{
throw e;
}
finally
{
try
{
is.close();
}
catch ( IOException e )
{
throw e;
}
}
return sb.toString();
}
}
这是用于显示布局的行url
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:descendantFocusability="blocksDescendants">
<TextView
安卓:id="@+id/prayersLinkWiki"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:autoLink="web"
安卓:textSize="15sp" />
</RelativeLayout>
在我的listview中,它将显示url,单击url后,它将转到youtube网站进行视频,对于holder url中的listview,我们可以使用embed作为此行中的youtube视频列表holder.prayersLinkWiki.setText(Html.fromHtml(app1.getUrlWiki()));
# 1 楼答案
删除代码
final Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(((Application1) o).getUrlWiki())); startActivity(i);
添加代码以打开与webview的对话框。在webview中加载youtube URL