how to play a video using video view and intent - java

I want to play a video on my DetaiActivity which is coming through Intent from another page I have an Image and video image is showing properly but I dont know how to play video
this is my myAdapter:
foodViewHolder.mCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(mContext,DetailActivity.class);
intent.putExtra("Image",myFoodList.get(foodViewHolder.getAdapterPosition()).getItemImage());
intent.putExtra("Video",myFoodList.get(foodViewHolder.getAdapterPosition()).getItemVideo());
intent.putExtra("Name",myFoodList.get(foodViewHolder.getAdapterPosition()).getItemName());
intent.putExtra("Ingrediants",myFoodList.get(foodViewHolder.getAdapterPosition()).getItemIngrediants());//
intent.putExtra("Procedure",myFoodList.get(foodViewHolder.getAdapterPosition()).getItemProcedure());
mContext.startActivity(intent);
}
});
and this my DetailActivity where i want to show video
I have tried something but it is not working
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
foodName = (TextView)findViewById(R.id.txtName);
foodImage = (ImageView)findViewById(R.id.ivImage2);
foodVideo = (VideoView)findViewById(R.id.ivVideo2);
foodIngrediants = (TextView)findViewById(R.id.txt_Ingrediants);//
foodProcedure = (TextView)findViewById(R.id.txt_Procedure);
Bundle mBundle = getIntent().getExtras();
if(mBundle!=null){
foodName.setText(mBundle.getString("Name"));
foodIngrediants.setText(mBundle.getString("Ingrediants"));//
foodProcedure.setText(mBundle.getString("Procedure"));
foodImage.setImageResource(mBundle.getInt("Image"));
String videoURL = mBundle.getString("Video");
// Start the MediaController
MediaController mediacontroller = new MediaController(DetailActivity.this);
mediacontroller.setAnchorView(foodVideo);
// Get the URL from String VideoURL
Uri video = Uri.parse(videoURL);
foodVideo.setMediaController(mediacontroller);
foodVideo.setVideoURI(video);
foodVideo.start();
Glide.with(this)
.load(mBundle.getString("Image"))
.into(foodImage);
}
}
}

Try using MediaPlayer. Something like:
Player = MediaPlayer.create(activity, uri);
Player.seekTo(0);
Player.setVolume(0.5f,0.5f);
Player.start();

Related

How to play recorded video in another Activity by passing in intent in android studio?

I am recording a video and passing its path into VideoView Activity. But the video can't be opened. Can anyone please help?
RecordActivity - video path function-:
public static String getVideoFilePath() {
return getAndroidMoviesFolder().getAbsolutePath() + "/" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + "rangeela.mp4";
}
Its returning ->
file:///storage/emulated/0/DCIM/202209_02-231740rangeela.mp4
File mediaFile = new File(getVideoFilePath());
Uri uri= Uri.fromFile(mediaFile);
Intent intent=new Intent(this,VideoEditorActivity.class);
intent.putExtra("uri",uri.toString());
startActivity(intent);
VideoViewActivity
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
Uri uri=Uri.parse(bundle.getString("uri"));
Log.e("video path = ", String.valueOf(uri));
binding.videoView.setVideoURI(uri);
binding.videoView.start();
}
But I am getting an error
Can't play this video
How to pass the recorded video in intent and play in another activity?
Try this
String path = getAndroidMoviesFolder().getAbsolutePath() + "/" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + "rangeela.mp4";
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this,VideoEditorActivity.class);
intent.putExtra("video",path);
}
});
VideoViewActivity
VideoView view = findViewById(R.id.videoview);
String path = getIntent().getStringExtra("video");
view.setVideoPath(path);
view.start();

Why does the Camera Intent not returning the image to the ImageView?

What I am trying to achieve is on the map long press it brings up and custom dialog view with 3 buttons, one for photo, one for save and one for cancel.
So at the moment when tapping on photo the camera intent opens and I can take a photo. Upon clicking "ok" the intent returns to the custom dialog window but no image is displayed?
This is my code I am using at the moment:
public void onMapLongClick(LatLng point) {
LayoutInflater factory = LayoutInflater.from(MainActivity.this);
final View deleteDialogView = factory.inflate(R.layout.custom_dialog, null);
final AlertDialog deleteDialog = new AlertDialog.Builder(MainActivity.this).create();
deleteDialog.setView(deleteDialogView);
deleteDialogView.findViewById(R.id.btn_photo).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String f = System.currentTimeMillis()+".jpg"; // Designated name
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), f);
fileUri = FileProvider.getUriForFile(MainActivity.this, getPackageName() + ".fileprovider", file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(cameraIntent, TAKE_PICTURE);
}
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
switch (requestCode) {
case TAKE_PICTURE:
ImageView imgView = findViewById(R.id.a);
imgView.setImageURI(fileUri);
break;
}
}
});
Notice that you pass 'fileUri' to the intent, so you also need to get it from the returned intent.
Try replacing imgView.setImageURI(fileUri);
with imgView.setImageURI(data.getData());
or
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
which will give you the URI with the image.

Problem receiving data from an Intent in Android Studio

I'm writing code that sends and receives data through Intent
However, the code is always having problems in:
summaryResult
Please, can anyone help me?
Code to send data:
``` nextPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent senderIntent = new Intent(getApplicationContext(), UserRegistration2.class);
Bundle sendlerBundler = new Bundle();
sendlerBlundler.putString("summaryResult", summaryResult);
senderIntent.putExtras(sendlerBundler);
startActivity(senderIntent);
Toast.makeText(getApplicationContext(), "It was sent: " + summaryResult, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), UserRegistration2.class);
startActivity(intent);
Bundle param = new Bundle();
param.putString("name", name);
param.putString("birthDate", birthDate);
parame.putString("city", city);
param.putString("summaryResult", summaryResult);
senderIntent.putExtras(param);
startActivity(senderIntent);
startActivity(intent);
}
});```
Code to receive data:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userRegistration);
Intent receiverIntent = getIntent();
Bundle receiverBundle = receiverIntent.getExtras();
String summaryResult =
receiverBundle.
getString
**("summaryResult");**
Toast.makeText(UserRegistration.this, "Receiving the Summary Result: " +summaryResult, Toast.LENGTH_LONG).show();```
you declared startActivity 4 times which is wrong , first you should put all your desired data in the bundle , add the bundle to the intent then start the activity only once. the code should look like this:
public void onClick(View view) {
Intent senderIntent = new Intent(getApplicationContext(), UserRegistration2.class);
Bundle sendlerBundler = new Bundle();
sendlerBlundler.putString("summaryResult", summaryResult);
senderIntent.putExtras(sendlerBundler);
startActivity(senderIntent);
}
code in receiving activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userRegistration);
Intent receiverIntent = getIntent();
Bundle receiverBundle = receiverIntent.getExtras();
String summaryResult = receiverBundle.getString("summaryResult");
}

android java endswith keeps crashing

I'm having a error with my code when i run it...All i want to do to make a webview in order to show a webpage in my app and to check the url if it ends with .m3u8 and then if it ends with .mp4 to open with the mxplayer(it's a media player app from the play store). I got the API of mxplayer from their site and added into my code but the problem is that when the link ends with .mp4 it crashes. Here's my code :
public class MainActivity extends AppCompatActivity {
private WebView myWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.web1);
myWebView.getSettings().setLoadsImagesAutomatically(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.loadUrl("url");
myWebView.setWebViewClient(new WebViewClient() {
// Api < 24
#Override
public boolean shouldOverrideUrlLoading(WebView myWebView, String url) {
if (url.endsWith(".mp4")) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri videoUri = Uri.parse("http://techslides.com/demos/sample-videos/small.mp4");
intent.setDataAndType( videoUri, "application/x-mpegURL" );
intent.setPackage( "com.mxtech.videoplayer.pro" );
startActivity( intent );
return true;
} else {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
}
});
}
}

Play video Intent in android

In my App I want that when user clicks a button, an intent should start and shows list of video players in the phone. By choosing one of them a video should start to play. I use this code:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(path)));
intent.setDataAndType(Uri.parse(String.valueOf(path)), "video/mp4");
startActivity(intent);
With this code when I click on the button, MXPlayer started directly and plays video. In the end of video, My App closes entirely! but I want to see a list of players and after end of video back to my App! How can I do this?
EDIT(Whole Code):
public class MenuActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
l_video.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String file_name="34743773";
File path = new File(Environment.getExternalStorageDirectory() + File.separator + "AAAAA"+ File.separator +file_name+".mp4");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(path)));
intent.setDataAndType(Uri.parse(String.valueOf(path)), "video/*");
startActivity(intent);
}
});
}
}

Categories

Resources