While working on the project I faced the task - Open the added video in WebView .
Having found a wonderful sample, I decided to look at his example.
The essence of the project:
- The user adds the copied link (from youtube / vimeo) to our application (by clicking the ADD button)
- After the user has clicked "ADD" in our RecyclerView, we get a preview of our video.
Well, actually the task , after we added the preview of our video, is to add the ability to click on the list item (in this case, our video) to open it by clicking on the WebView (in the browser) or in the application itself.
Extras: I found a way to open videos beautifully in a new activit, but alas, I don’t know how to use them for links that will be added each time in different ways. That is why if I understand correctly, you need to save the links that the user enters - a guess .
String frameVideo = "<iframe width=\"100%\" height=\"415\" src=\"https://www.youtube.com/embed/"\" frameborder=\"0\" allowfullscreen></iframe>"; Well, actually the working code of the program itself: Adapter for RecyclerView
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> { private ArrayList<VideoPreview> mItems; private Context mContext; public DataAdapter(Context context) { mContext = context; mItems = new ArrayList<>(); } @Override public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_video_preview, viewGroup, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { viewHolder.mPreviewLinkTextView.setText(mItems.get(i).getVideoLink()); viewHolder.mPreviewLinkPreviewTextView.setText(mItems.get(i).getPreviewLink()); Picasso.with(mContext).load(mItems.get(i).getPreviewLink()).resize(120, 90).into(viewHolder.mPreviewImageView); } @Override public int getItemCount() { return mItems.size(); } public void addItem(VideoPreview videoPreview) { mItems.add(videoPreview); notifyDataSetChanged(); } public boolean contains(VideoPreview newVideoPreview) { for (VideoPreview videoPreview : mItems) { if (videoPreview.getVideoLink().equals(newVideoPreview.getVideoLink())) return true; } return false; } public class ViewHolder extends RecyclerView.ViewHolder{ ImageView mPreviewImageView; TextView mPreviewLinkTextView; TextView mPreviewLinkPreviewTextView; public ViewHolder(View view) { super(view); mPreviewImageView = (ImageView) view.findViewById(R.id.item_video_preview_image_view); mPreviewLinkTextView = (TextView) view.findViewById(R.id.item_video_preview_link_text_view); mPreviewLinkPreviewTextView = (TextView) view.findViewById(R.id.item_video_preview_link_preview_text_view); } } } Acitivity code
public class VideoPreviewActivity extends AppCompatActivity implements VideoTypes { private RecyclerView mVideoPreviewRecyclerView; private EditText mAddEditText; private Button mAddButton; private OkHttpClient httpClient; private DataAdapter mDataAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video_preview); mVideoPreviewRecyclerView = (RecyclerView) findViewById(R.id.video_preview_recycler_view); mAddEditText = (EditText) findViewById(R.id.add_edit_text); mAddButton = (Button) findViewById(R.id.add_button); mVideoPreviewRecyclerView = (RecyclerView)findViewById(R.id.video_preview_recycler_view); mVideoPreviewRecyclerView.setHasFixedSize(true); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext()); mVideoPreviewRecyclerView.setLayoutManager(layoutManager); mDataAdapter = new DataAdapter(getApplicationContext()); mVideoPreviewRecyclerView.setAdapter(mDataAdapter); httpClient = new OkHttpClient(); mAddButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mDataAdapter.getItemCount() == 10) { Toast.makeText(getApplicationContext(), "You can not add more than 10 videos!", Toast.LENGTH_SHORT).show(); return; } String videoLink = mAddEditText.getText().toString(); int videoType = VideoMethods.getLinkType(videoLink); if (videoType == TYPE_UNKNOWN) { Toast.makeText(getApplicationContext(), "Incorrect link!", Toast.LENGTH_SHORT).show(); return; } mAddEditText.getText().clear(); InputMethodManager inputMethodManager = (InputMethodManager) VideoPreviewActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE); inputMethodManager.hideSoftInputFromWindow(VideoPreviewActivity.this.getCurrentFocus().getWindowToken(), 0); String videoId = VideoMethods.getId(videoLink); switch (videoType) { case TYPE_YOUTUBE: VideoPreview videoPreview = new VideoPreview(videoLink, "http://img.youtube.com/vi/" + videoId + "/1.jpg"); if (!mDataAdapter.contains(videoPreview)) { mDataAdapter.addItem(videoPreview); mVideoPreviewRecyclerView.scrollToPosition(mDataAdapter.getItemCount()-1); } else { Toast.makeText(getApplicationContext(), "This video already exists!", Toast.LENGTH_SHORT).show(); } break; case TYPE_VIMEO: try { getVimeoPreview(videoLink, videoId); } catch (Exception e) { Toast.makeText(getApplicationContext(), "Something going wrong!", Toast.LENGTH_SHORT).show(); } break; } } }); } public void getVimeoPreview(final String videoLink, final String videoId) throws Exception { Request request = new Request.Builder() .url("http://vimeo.com/api/v2/video/" + videoId + ".xml") .build(); httpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Toast.makeText(getApplicationContext(), "Something going wrong!", Toast.LENGTH_SHORT).show(); } @Override public void onResponse(Call call, Response response) throws IOException { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); String s = response.body().string(); Pattern p = Pattern.compile("<thumbnail_small>(.*)<\\/thumbnail_small>"); final Matcher m = p.matcher(s); m.find(); runOnUiThread(new Runnable() { @Override public void run() { VideoPreview videoPreview = new VideoPreview(videoLink, m.group(1)); if (!mDataAdapter.contains(videoPreview)) { mDataAdapter.addItem(videoPreview); mVideoPreviewRecyclerView.scrollToPosition(mDataAdapter.getItemCount()-1); } else { Toast.makeText(getApplicationContext(), "This video already exists!", Toast.LENGTH_SHORT).show(); } } }); } }); } } Perhaps the VideoMethods class will also be useful for handling video links:
public static int getLinkType(String link) { Pattern pattern = Pattern.compile("^https:\\/\\/www\\.(youtube\\.com)\\/watch\\?v=[\\w-]{11}$|^https:\\/\\/(youtu\\.be)\\/[\\w-]{11}$|^https:\\/\\/(vimeo\\.com)\\/[0-9]*$"); Matcher matcher = pattern.matcher(link); if (!matcher.matches()) return TYPE_UNKNOWN; for (int i=1; i<=matcher.groupCount(); i++) { if (matcher.group(i) != null) { switch (i) { case 1: case 2: return TYPE_YOUTUBE; case 3: return TYPE_VIMEO; default: return TYPE_UNKNOWN; } } } return TYPE_UNKNOWN; } public static String getId(String link) { Pattern pattern = Pattern.compile("^https:\\/\\/www\\.youtube\\.com\\/watch\\?v=([\\w-]{11})$|^https:\\/\\/youtu\\.be\\/([\\w-]{11})$|^https:\\/\\/vimeo\\.com\\/([0-9]*)$"); Matcher matcher = pattern.matcher(link); matcher.matches(); for (int i=1; i<=matcher.groupCount(); i++) { if (matcher.group(i) != null) { return matcher.group(i); } } return null; } }