I want to play video in my application. I created a custom ListView
with VideoView
. The VideoView
displayed successfully but one problem is that when I am move list view VideoView
changed its position. This is not looking good in my application. My requirement is that when click on button
video should start played in that area of list view only. How could I achieve this?
Here is code of displaying VideoView
in ListView
:
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;
}
}