gpt4 book ai didi

android - 如何在 Android 的 ListView 中播放视频

转载 作者:行者123 更新时间:2023-11-29 01:47:02 25 4
gpt4 key购买 nike

我想在我的应用程序中播放视频。我用 VideoView 创建了一个自定义的 ListViewVideoView 显示成功,但一个问题是当我移动 ListView 时 VideoView 改变了它的位置。这在我的应用程序中看起来不太好。我的要求是,当点击 button 时,视频应该只在 ListView 的那个区域开始播放。我怎样才能做到这一点?

下面是在ListView中显示VideoView的代码:

public class MyVideoAdapter extends BaseAdapter {

Context context;
String a[];
LayoutInflater inflate;
VideoView vv;
ImageButton img;

public MyVideoAdapter(Context context, String[] a) {
this.context = context;
this.a = a;
inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return a.length;
}

@Override
public Object getItem(int position) {
return position;
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (convertView == null)
v = inflate.inflate(R.layout.row, null);
img = (ImageButton) v.findViewById(R.id.imageButton1);
String ans = a[position];

if (ans.equals("one")) {
vv = (VideoView) v.findViewById(R.id.VideoView);
MediaController mc = new MediaController(context);
mc.setAnchorView(vv);
mc.setMediaPlayer(vv);
// Uri uri=Uri.parse("https://www.dropbox.com/s/d5blvemu8yse38s/kesari.mp4");
// vv.setMediaController(mc);
String path = "android.resource://com.example.wealthcreation/raw/kesari";
vv.setVideoURI(Uri.parse(path));

img.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

img.setVisibility(View.INVISIBLE);
vv.start();

}
});

// img.setImageResource(R.drawable.k1);
// ImageButton img=(ImageButton)v.findViewById(R.id.imageButton1);
// Button btn1=(Button)v.findViewById(R.id.btnplay);

/*
img.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
vv.setVisibility(View.VISIBLE);
img.setVisibility(View.GONE);
MediaController mc = new MediaController(context);
mc.setAnchorView(vv);
mc.setMediaPlayer(vv);
// Uri uri=Uri.parse("https://www.dropbox.com/s/d5blvemu8yse38s/kesari.mp4");
vv.setMediaController(mc);
String path = "android.resource://com.example.wealthcreation/raw/kesari";
vv.setVideoURI(Uri.parse(path));
vv.start();
String videoId = "CUuBdtsQx6U";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"
+ videoId));
intent.putExtra("VIDEO_ID", videoId);
context.startActivity(intent);
// Toast.makeText(context, "Hii",Toast.LENGTH_LONG).show();
// this intent define an action and and parse the url
// context.startActivity(new
// Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=CUuBdtsQx6U")));

/*
WebView webview = new WebView(context);

String htmlString = "<html> <body> <embed src=\"youtube link\"; type=application/x-shockwave-flash width="
+ widthOfDevice
+ " height="
+ heightOfDevice
+ "> </embed> </body> </html>";

webview.loadData(htmlString, "text/html", "UTF-8");

try
{
MediaPlayer mp = new MediaPlayer();
mp.setDataSource("http://www.youtube.com/watch?v=CUuBdtsQx6U");
mp.prepare();
mp.start();
}
catch (Exception e)
{
Log.d("videos", "", e);
}
}

});
*/

}

if (ans.equals("two"))
{
/*
img.setImageResource(R.drawable.k2);
Button btn2 = (Button) v.findViewById(R.id.btnplay);

btn2.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
Toast.makeText(context, "Hii", Toast.LENGTH_LONG).show();
// context.startActivity(new
// Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=f6fmZFDE5EA")));
// this intent define an action and and parse the url
// context.startActivity(new
// Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=CUuBdtsQx6U")));
String videoId = "f6fmZFDE5EA";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"
+ videoId));
intent.putExtra("VIDEO_ID", videoId);
context.startActivity(intent);
// context.startActivity(new
// Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?feature=player_detailpage&v=f6fmZFDE5EA")));
}

});
*/
}

if (ans.equals("three"))
{
/*
Button btn3 = (Button) v.findViewById(R.id.btnplay);

btn3.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
String videoId = "f6fmZFDE5EA";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"
+ videoId));
intent.putExtra("VIDEO_ID", videoId);
context.startActivity(intent);

// Toast.makeText(context, "three",Toast.LENGTH_LONG).show();
// context.startActivity(new
// Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=f6fmZFDE5EA")));
// this intent define an action and and parse the url
// context.startActivity(new
// Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=CUuBdtsQx6U")));
}

});
*/
}
if (ans.equals("four"))
{
/*
img.setImageResource(R.drawable.k4);
Button btn4 = (Button) v.findViewById(R.id.btnplay);

btn4.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
String videoId = "VBMuzOrVD18";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"
+ videoId));
intent.putExtra("VIDEO_ID", videoId);
context.startActivity(intent);

// Toast.makeText(context, "four",Toast.LENGTH_LONG).show();
// context.startActivity(new
// Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=VBMuzOrVD18")));
// this intent define an action and and parse the url
// context.startActivity(new
// Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=CUuBdtsQx6U")));
}

});
*/
}
if (ans.equals("fifth"))
{
/*
img.setImageResource(R.drawable.k5);
Button btn5 = (Button) v.findViewById(R.id.btnplay);
btn5.setOnClickListener(new OnClickListener()
{

@Override
public void onClick(View v)
{
String videoId = "-UlSAYhHqqc";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"
+ videoId));
intent.putExtra("VIDEO_ID", videoId);
context.startActivity(intent);

// Toast.makeText(context, "fith",Toast.LENGTH_LONG).show();
// context.startActivity(new
// Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=-UlSAYhHqqc")));
// this intent define an action and and parse the url
// context.startActivity(new
// Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=CUuBdtsQx6U")));
}

});
*/
}
return v;
}

}

最佳答案

在您的情况下,您已经解析了 XML 格式的数据,因此您必须将所有数据存储在 HashMap Arraylist 中,并使用特定的视频 url 键。

现在,如果您需要在 ListView 的 Item Click 上打开视频,那么您可以像这样在字符串中检索您的视频 url

String  myUrl = urHashMaparraylist.get(position).get("videolocation");

现在将此 myUrl 传递给下一个 Activity,并将此字符串设置为

 Uri video = Uri.parse(myUrl);
videoView.setVideoURI(video);

listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

String myUrl = urHashMaparraylist.get(position).get("videolocation");
Intent n = new Intent(YourActivityName.this , NewActivityName.class);
n.putExtra("videolocation",myUrl);
startActivity(n);

}

});

现在在您的下一个 Activity 中将其检索为

Uri video = Uri.parse(url);
videoView.setVideoURI(video);

有关视频流的更多引用...您可以查看以下网址: http://developer.samsung.com/android/technical-docs/Android-Media-Streaming-Tutorial你甚至可以查看我的答案 How Do I Play Video in ListView like Instagram and Vine?

关于android - 如何在 Android 的 ListView 中播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20856813/

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