Play Videos in a Listview

1.1k views Asked by At

I've been looking for a solution for hours. I created a custom baseAdapter for listview. It's working fine with images. After imageView worked, i thought a VideoView would be fine too. Then, i saw i had to use a TextureViev instead of it, worked on it, its logic was clear but at the end, i got some errors with no solution on the web.

I tried use a Glide, it didn't worked, and i came up here back again.

While mediaplayer preparing i'm getting these errors;

E/ExtMediaPlayer-JNI: env->IsInstanceOf fails

E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0

When media prepared;

W/MediaPlayer: info/warning (3, 0)

When trying to start mediaplayer

[ 12-19 19:32:11.967 378:31124 E/ ] not in avi mode

I need every little inspiration to keep going on it, thank you.

I defined baseAdapter in a inner class in MainActivity class with implementing mediaplayer and textureview methods;

public  class postAdapter extends BaseAdapter implements TextureView.SurfaceTextureListener, MediaPlayer.OnBufferingUpdateListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnVideoSizeChangedListener {
     ArrayList<Posts> posts;
     public postAdapter(ArrayList<Posts> postsToList)
     {
         posts=postsToList;
     }
     MediaPlayer mMediaPlayer;
     TextureView textureView;
     VideoView animatedView;

     @Override
     public int getCount() {
        // Log.d(TAG, "getCount: "+posts.size());
         return posts.size();

     }

     public void addRangeToTop(ArrayList<Posts> newposts)
     {
        for (int i=0;i<newposts.size();i++)
        {
            posts.add(i,newposts.get(i));
        }
     }
     @Override
     public Object getItem(int position) {

         return null;
     }

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

     @Override
     public View getView(int position, View convertView, ViewGroup parent) {

         View child = getLayoutInflater().inflate(R.layout.animatedpost, null);
         TextView titleView = (TextView) child.findViewById(R.id.titleView);
          ImageView imageView = (ImageView) child.findViewById(R.id.capsView);
       animatedView=(VideoView) child.findViewById(R.id.animatedView);
       textureView=(TextureView)child.findViewById(R.id.textureView);
         TextView voteView = (TextView) child.findViewById(R.id.voteView);
        Button button=(Button) child.findViewById(R.id.button2);



        textureView.setSurfaceTextureListener(this);














         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {


          /* ArrayList<Posts> newp=new ArrayList<Posts>();
                 for (int i=10;i<15;i++)
                 {
                     Posts post=new Posts();
                     post.setPostTitle(i+"dsf");
                     post.setPostVote(i+"sdfsd");
                     newp.add(post);

                 }
                 int index = lv.getFirstVisiblePosition()+newp.size() ;
                 View v2 = lv.getChildAt(0);
                 int top = (v2 == null) ? 0 : v2.getTop();
                 addRangeToTop(newp);
                 notifyDataSetChanged();
                lv.setSelectionFromTop(index, top);*/
                /* Uri uri = Uri.parse("http://somepath"); // missing 'http://' will cause crashed
                 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                 startActivity(intent);*/

             }
         });
         titleView.setText(posts.get(position).getPostTitle());
         voteView.setText(posts.get(position).getPostVote());
        // imageView.setImageResource(R.drawable.df);



         return child;
     }


     @Override
     public void onBufferingUpdate(MediaPlayer mp, int percent) {

     }

     @Override
     public void onCompletion(MediaPlayer mp) {

     }

     @Override
     public void onPrepared(MediaPlayer mp) {

         Log.d(TAG, "onPrepared: ");

            mp.start();

     }

     @Override
     public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

     }

     @Override
     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
         mMediaPlayer=new MediaPlayer();
         Surface s = new Surface(surface);


         try {

             mMediaPlayer.setDataSource("http://somefunnyvideopath.mp4");
             mMediaPlayer.setSurface(s);


             mMediaPlayer.setOnBufferingUpdateListener(this);
             mMediaPlayer.setOnCompletionListener(this);
             mMediaPlayer.setOnPreparedListener(this);
             mMediaPlayer. setOnVideoSizeChangedListener( this);



             mMediaPlayer.prepareAsync();

         } catch (IOException e) {
             e.printStackTrace();
         }
     }

     @Override
     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

     }

     @Override
     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
         return false;
     }

     @Override
     public void onSurfaceTextureUpdated(SurfaceTexture surface) {

     }
 }
1

There are 1 answers

2
yanivtwin On

I would make mediaplayer a singleton class , so you won't have to instanciate him more than once , not sure that's your problem but it's better

public class MyMediaPlayer {
    MediaPlayer mp;
    private static volatile MyMediaPlayer instance = null;
    private MyMediaPlayer() { }
    public static MyMediaPlayer getInstance() {
        if (instance == null) {
            synchronized (MyMediaPlayer.class) {
                if (instance == null) {
                    instance = new MyMediaPlayer();}}}
        return instance;
    }}

also try with an mp4 from raw resource or from your gallery so you know your device can play it and later when it works try to change to your link,

hope it helps.