简体   繁体   中英

Code to play list of mp4 url fetching from the server in android studio

I know these questions are previously asked by so many peoples but for my requirement, I'm not able to get the solution. I have created the main class there I implemented the method to fetch a list of video name's and placing it in to in recycler view. So when the user clicks on any particular video name getting the URL.So the problem is how can I implement the logic to play that clicked item. MainActivity :

public class MainActivity extends AppCompatActivity {

//ProgressDialog mDialog;
VideoView videoView;
Button btnPlayPause;
//ImageButton btnPlayPause;
String videoURL;

ProgressDialog pd;
private final String url_video = "url://xyz.com/abc/video";
private static final int RECOVERY_REQUEST = 1;

private RecyclerView recyclerView;
private VideoListAdapter videoListAdapter;
private ArrayList<VideoDetail> videoDetailsArrayList = new ArrayList<>();

private View.OnClickListener videoItemClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        videoURL  =   view.getTag().toString();

        //setVideo();

    }
};



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnPlayPause= (Button) findViewById(R.id.btn_play_pause);
    videoView = (VideoView) findViewById(R.id.videoView1);

    setAdapter();

    loadRequestList();
}



private void loadRequestList() {

    if (isNetworkAvailable()) {

        pd = new ProgressDialog(MainActivity.this);
        pd.setMessage("Loading data...");
        pd.show();

        //ProgressUtils.getInstance(this).show("Loding Video List........");

        StringRequest stringRequest = new StringRequest(Request.Method.GET, url_video, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                try {
                    //ProgressUtils.getInstance(MainActivity.this).cancel();
                    pd.dismiss();

                    if (TextUtils.isEmpty(response)) {
                        return;
                    }
                    VideoDetailsModel videoDetailsModel = VideoResponseHandler.getVideoListDetails(response);
                    // System.out.println("printing Data of video"+videoDetailsModel.getDetails());
                    if (videoDetailsModel == null) {

                        return;
                    }

                    if (videoDetailsModel.getDetails() == null) {

                        return;
                    }
                    //System.out.println("printing list3\n");

                    videoListAdapter.setVideoList((ArrayList<VideoDetail>) videoDetailsModel.getDetails());
                    //System.out.println("printing list video\n");

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


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                pd.dismiss();

                //ProgressUtils.getInstance(MainActivity.this).cancel();
                Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG);


            }
        });

        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        requestQueue.add(stringRequest);
    }else{

        builderDialog(MainActivity.this).show();

    }
}
private AlertDialog.Builder builderDialog(MainActivity listVideoActivity) {

    AlertDialog.Builder builder = new AlertDialog.Builder(listVideoActivity);
    builder.setTitle("No Internet Connection");
    builder.setMessage("You Need To Have Mobile Data or Wifi to access this. Press OK to Exit");

    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {

            startActivity(new Intent(MainActivity.this,MainActivity.class));

            //finish();
        }
    });

    return builder;

}

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activityNetworkInfo = connectivityManager.getActiveNetworkInfo();

    return activityNetworkInfo != null && activityNetworkInfo.isConnected();
}

private void setAdapter() {

    recyclerView = (RecyclerView) findViewById(R.id.recyclerVideo);
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
    videoListAdapter = new VideoListAdapter(videoDetailsArrayList,videoItemClickListener);

    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setAdapter(videoListAdapter);



}

} Adapter Class look like this :

public class VideoListAdapter extends RecyclerView.Adapter<VideoListAdapter.MyViewHandler> {

private ArrayList<VideoDetail> videoDetailArrayList = new ArrayList<>();
private View.OnClickListener videoItemClickListener;


public VideoListAdapter(ArrayList<VideoDetail> videoDetailArrayList, View.OnClickListener videoItemClickListener) {
    this.videoDetailArrayList = videoDetailArrayList;
    this.videoItemClickListener = videoItemClickListener;


}

@Override
public MyViewHandler onCreateViewHolder (ViewGroup parent, int viewType){


    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_video_list, parent, false);


    return new MyViewHandler(view);
}

@Override
public void onBindViewHolder (MyViewHandler holder,int position){

    if (videoDetailArrayList == null) {
        return;
    }
    VideoDetail videoDetail = videoDetailArrayList.get(position);

    if (videoDetail == null) {
        return;
    }

    if (holder instance_of MyViewHandler) {

        if (TextUtils.isEmpty(videoDetail.getFileName())) {
            return;
        }

        holder.tvVideo.setText(videoDetail.getFileName());
        holder.tvVideo.setTag(videoDetail.getFilePath());
        holder.videoImage.setTag(videoDetail.getFilePath());
        if (videoItemClickListener != null) {
            holder.tvVideo.setOnClickListener(videoItemClickListener);
            holder.videoImage.setOnClickListener(videoItemClickListener);
        }
    }

}


@Override
public int getItemCount() {
    return videoDetailArrayList.size();
}

public class MyViewHandler extends RecyclerView.ViewHolder {

    private final TextView tvVideo;
    private final ImageView videoImage;


    public MyViewHandler(View itemView) {
        super(itemView);

        tvVideo = (TextView) itemView.findViewById(R.id.tvVideo);
        videoImage = (ImageView) itemView.findViewById(R.id.videoImage);

    }
}


public void setVideoList(ArrayList<VideoDetail> videoDetails) {

    if (videoDetails != null) {
        this.videoDetailArrayList = videoDetails;
    }

    notifyDataSetChanged();
}

}

activity_main.xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context="com.xyz.videoviewengineersdream.MainActivity">

<VideoView
    android:id="@+id/videoView1"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:id="@+id/btn_play_pause"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Play"
    android:onClick="videoPlay"
    android:clickable="true"
    android:layout_below="@+id/videoView1"
    android:layout_centerHorizontal="true"
    android:background="@color/colorPrimary"
    android:padding="10dp"
    android:layout_marginTop="10dp"
    android:textColor="#FFF"
    android:textStyle="bold"
    android:textSize="16dp"/>





<View
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:layout_below="@+id/btn_play_pause"
    android:id="@+id/viewView"/>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerVideo"
    android:layout_below="@+id/viewView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

you have already created VideDetail class

private ArrayList<VideoDetail> videoDetailArrayList = new ArrayList<>()

just Add videourl fields to the VideoDetail class then you can directly get url on button click like below in onBindViewHolder

 final VideDetail videodetail = videolist.get(position);

        holder.yourbtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            String url = videdetail.getvideourl();
                           // play video with the url you got 
                        }
                });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM