Reputation: 141
This is my code.
public class MainActivity extends Activity {
ListView list;
public ArrayList<String> videoList;
public CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list =(ListView)findViewById(R.id.list);
videoList = new ArrayList<String>();
videoList.add("https://www.youtube.com/watch?v=Zgfi7wnGZlE");
videoList.add("https://www.youtube.com/watch?v=Zgfi7wnGZlE");
videoList.add("https://www.youtube.com/watch?v=Zgfi7wnGZlE");
videoList.add("http://www.youtube.com/watch?v=DdlWPL53PvQ#sthash.fW5EtDFb.dpuf");
Log.d("size of videoList:",""+ videoList.size());
adapter = new CustomListAdapter(MainActivity.this, videoList);
list.setAdapter(adapter);
}
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private ArrayList<String> videoList;
public CustomListAdapter(Activity activity, ArrayList<String> videoList) {
this.activity = activity;
this.videoList = videoList;
}
@Override
public int getCount() {
return videoList.size();
}
@Override
public Object getItem(int location) {
return videoList.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
VideoView video = (VideoView)convertView.findViewById(R.id.video_view);
video.setVideoPath(videoList.get(position));
video.start();
return convertView;
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_row_selector" />
</RelativeLayout>
list_raw.xml
<?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="wrap_content"
android:background="@drawable/list_row_selector"
android:padding="8dp" >
<!-- Thumbnail Image -->
<VideoView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</RelativeLayout>
I can't get video and no any error display in logcat thenb what is the issue? I added Internet permission in manifest is any or permission or any other settting is required?
Upvotes: 1
Views: 5330
Reputation: 621
I think we should not put video view directly in the list, Since each and every view in list are redrawn or invalidated lot of times and if any video is being played and watched by user and he accidentally scrolls it off the screen , Android OS will free up that memory and hence again video will be played from Start. Video.start() will be called again and again
Best Solution is to provide a thumbnail image of any frame of video . Replace Video view with image view and on click of it open a new screen where put a video view and play on to it.
Upvotes: 1
Reputation: 11873
Try setting a VideoURI
instead of VideoPath
and check again,
VideoView video = (VideoView)convertView.findViewById(R.id.video_view);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(video);
video.setMediaController(mediaController);
video.setVideoURI(Uri.parse(videoList.get(position)));
video.start();
UPDATE: If you are planning to use video's from YouTube only then the best way would be using the official YouTube API. Check here,
Android YouTube app Play Video Intent
How to Play YouTube Video in Android App
Upvotes: 0