gpt4 book ai didi

java - 如何将youtube嵌入我的应用程序中,现在将其转到youtube网站

转载 作者:行者123 更新时间:2023-12-03 06:33:43 26 4
gpt4 key购买 nike

实际上我是从json获取网址,现在在我的列表 View 中显示了json解析中存在的所有youtube url列表。单击该url之后,它将转到youtube页面并且该视频正在播放,我不想转到其他网站从我的应用程序中,视频必须在我的应用程序中显示,为此我将如何使用嵌入到我的应用程序中的youtube。

如何在我的列表 View 中显示youtube视频列表,现在它显示了url,我希望它必须显示listview的小视频,如果我们单击该视频,它将使用嵌入在我的应用程序中播放youtube视频

Myactivity.java

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.

mylayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<ListView
android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/listV_main"/>
</LinearLayout>

我不想进入youtube网站,该视频必须在我的应用程序中显示,以了解如何使用嵌入

应用程序Adapter.java
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;



}

Fetchdatatask1.java
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:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants">

<TextView
android:id="@+id/prayersLinkWiki"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:autoLink="web"
android:textSize="15sp" />

</RelativeLayout>

在我的列表 View 中,它将显示该网址,单击该网址后,它会转到youtube网站上进行视频播放,对于持有人网址中的列表 View ,我们可以在此行中使用embed作为youtube视频列表 holder.prayersLinkWiki.setText(Html.fromHtml(app1.getUrlWiki()));

最佳答案

删除代码final Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(((Application1) o).getUrlWiki()));
startActivity(i);
添加代码以使用webview打开对话框。在网络 View 中加载youtube URL

关于java - 如何将youtube嵌入我的应用程序中,现在将其转到youtube网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17340843/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com