Tuesday, November 22, 2022

Playing a YouTube video in the Android application

We  want to play Youtube video by streaming it directly from YouTube without downloading. Also, while playing videos, I want provide menu options. I don't want to play video using default intent. How can we do this?

There are multiple ways to play the youtube video in your android application. One of the easiest way is to start the activity View by parsing the URI. Eg as below:

    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("youtube URL")));

that's 
"startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.youtube.com/watch?v=4TmBlhfJVnc")));



if this should happen on click of a button, you can add this inside onclick of the button:

btnvideo.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("https://www.youtube.com/watch?v=4TmBlhfJVnc")));
Log.i("Video", "Video Playing...."); } });

Now if  the video names are shown as the list of items. 
And on click of the list item, we need to fetch the URL from mysql database and 
pass it to the URL Parse.


BhagvadgeetaViewModel slideshowViewModel =
new ViewModelProvider(this).get(BhagvadgeetaViewModel.class);


binding = FragmentSlideshowBinding.inflate(inflater, container, false);
View root = binding.getRoot();

final TextView textView = binding.textSlideshow;
slideshowViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
ProgressBar progressBar = binding.progress;
ListView listview =binding.list;
final String[] videoName = new String[1];
progressBar.setVisibility(View.VISIBLE);

slideshowViewModel.getVideoList().observe(getViewLifecycleOwner(), videoList->{
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_list_item_2, android.R.id.text1, videoList);
listview.setAdapter(adapter);
progressBar.setVisibility(View.GONE);
});

listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView textView1 = view.findViewById(android.R.id.text1);
String selectedItemname = (String) textView1.getText();
slideshowViewModel.getVideoPath(selectedItemname).observe(getViewLifecycleOwner(), S-> {
String videoPath = S;

Log.i("Video", "Video Playing....: "+videoPath);
// Toast.makeText(getActivity(),"Selected Path: "+videoPath,Toast.LENGTH_SHORT).show();
if(videoPath !=null) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(videoPath)));
}

});

}
});

return root;
}




And in the viewmodel, you need to write the code to get the video path:
You need to have a api which returns the youtube url by sending the youtube video name.
I have that in getpath.php file in my server.


Here we go:

LiveData<String> getVideoPath(String videoName){
mVideoPath = new MutableLiveData<>();
mVideoPath.setValue(loadVideoPath(videoName));
if(mVideoPath == null){
mVideoPath.setValue(loadVideoPath(videoName));
}
return mVideoPath;
}
private String loadVideoPath(String videoName) {
Handler myHandlerVideoPath = new Handler();

myHandlerVideoPath.post(new Runnable() {
@Override
public void run() {
String[] field = new String[1];
field[0] = "name";
String[] data = new String[1];
data[0] = videoName;
PutData putData = new PutData(SERVER+"getPath.php", "POST", field, data);
if (putData.startPut()) {
if (putData.onComplete()) {

String result = putData.getResult();
if(result.equals("All fields are required")){
//Toast.makeText(getActivity(), result,Toast.LENGTH_SHORT).show();
mText.setValue(result);
}
else {
videoPath = result ;

}
mVideoPath.setValue(videoPath);


}

}
}

});
return videoPath;
}


No comments: