Play video Intent in android - java

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);
}
});
}
}

Related

how to play a video using video view and intent

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();

How to avoid complete action using dialog to read a contact or to do a call phone

In my app i have a button to select a contact from contacts phone and a button to start a call phone to this number. So when i click on the button to select the contact, the complete action using dialog appears with more apps to choose as well as when i click on the button to star the call phone. How can i avoid the dialog to access contacts and to do a call phone directly?
Partial code of my activity:
contacts.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 0);
}
});
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String numeroDiTelefono = dati.getString("numeroDiTelefono");
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + numeroDiTelefono));
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
}
});
Make sure you have added the permission in the Manifest file:
<uses-permission android:name="android.permission.CALL_PHONE" />
And all you should need for the Intent is:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+numeroDiTelefono));
startActivity(callIntent);
Basically that dialog means you have more than one contacts app installed on your phone. This is a default Android system behavior when you call any kind of common intent actions.
What you can do is make the intent more specific to the app you're looking for.
By specifing
a) the specific data uri
b) the package name
c) set the content type, etc
Also try this.
Intent i = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(i, PICK_CONTACT);
public void onClick(View view) {
String number = String.valueOf(bundle.getLong("phone"));
Uri call = Uri.parse("tel:" + number);
Intent intent = new Intent(Intent.ACTION_DIAL, call);
startActivity(intent);
}

Switch Screens in Android not working

I have a "Play Now" button for a simple android game. When I click the button it calls start, but it doesn't do anything.
Here is start():
public void start(View view) {
Intent myIntent = new Intent(this, Game.class);
startActivity(myIntent);
}
and Game.java:
public class Game extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
}
Also, I didn't forget to put it into the manifest
<activity android:name=".Game"></activity>
I'm new to android and this is all very confusing. I tried putting an intent filter although I probably did it wrong.
I looked at this How to switch between screens? but it didn't work for me.
You are finishing the activity just when you create it (onCreate). Try deleting or commenting finish(); and good luck!
remove following lines, we use them with startActivityForResult , after removing it should work other than this everything is fine
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
Actually,your start function is working fine.But the problem is with onCreate() method in Game activity.You are calling finish() method in this which is killing the activity.Get rid of this method and then check.One thing more,I don't understand what is the purpose of setResult in your context.It is actually used for startActivityForResult() method.Refer to this link for further information:
https://developer.android.com/training/basics/intents/result.html
public class Game extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
//Intent intent = new Intent();
//setResult(RESULT_OK, intent);
//finish();
}
}

Android Implicit Intent for Viewing a Video File

In my Android app, I have a button that when clicked, launches the external application of my choice to play a video (I gather that this is called an "implicit intent"). Here is the relevant Java code from my onCreate method.
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener
(
new Button.OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(i);
}
}
);
I expected this to work, since I've followed tutorials and the Android developers documentation pretty closely, but when I test my app in the AVD, instead of prompting a menu of external applications where I can view my video, the app crashes.
What is causing my app to crash?
Change your onClick method to below code. You should give the option to choose the external player.
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
startActivity(Intent.createChooser(intent, "Complete action using"));
}
Change your code to add this check:
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("https://youtu.be/jxoG_Y6dvU8"), "video/*");
// Check there is an activity that can handle this intent
if (i.resolveActivity(getPackageManager()) == null) {
// TODO No activity available. Do something else.
} else {
startActivity(i);
}

Android: Set Text View's text from another activity Not working

I have 2 activities and 2 classes.
In my Main class, when i click submit button it will start another activity. here is the code.
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, NewActivity.class));
newActivity.setViewValues(fNameET.getText().toString(), lNameET.getText().toString(), mInitialET.getText().toString(), "Female", "birthday", addressET.getText().toString(), cNumberET.getText().toString());
}
The newActivity is an object of the other activity and the setViewValues is the method of it.
This doesn't work, this is how i do it in java gui. Maybe something is missing.
Could anyone help me with this?
You should pass the data like this.
MainActivity.java
Intent intent = new Intent(MainActivity.this, NewActivity.class));
intent.putExtra("firstName",fvalue);
intent.putExtra("lastname",lname);
......
startActivity(intent);
NewActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutname);
Intent intent = getIntent();
String firstName = intent.getStringExtra("firstName");
String lastName = intent.getStringExtra("lastname");
}

Categories

Resources