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;
}
}
});
}
}
Related
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();
I am using onesignal sdk to start activity on notification click:
public class MainActivity extends AppCompatActivity {
// adding webview variable
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// OneSignal Initialization
OneSignal.startInit(this)
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.unsubscribeWhenNotificationsAreDisabled(true)
.init();
// initializing webview by matching with id in activity_main.xml
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Setting up webview & webchrome client
mWebView.setWebViewClient(new WebViewClient());
mWebView.setWebChromeClient(new WebChromeClient());
// if url is passed via onesignal notification as via putextra, open that url else open homepage
String url = getIntent().getStringExtra("url");
if (url != null) {
mWebView.loadUrl(url);
} else {
mWebView.loadUrl("https://www.google.com");
}
OneSignal.startInit(this).setNotificationOpenedHandler(new OneSignal.NotificationOpenedHandler() {
#Override
public void notificationOpened(OSNotificationOpenResult result) {
JSONObject data = result.notification.payload.additionalData;
String launchURL;
if (data != null) {
launchURL = data.optString("launchURL", null);
// Log.d("Debug", "Launch URL: " + launchURL);
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.putExtra("url", launchURL);
startActivity(intent);
} else {
Log.d("Debug", result.notification.payload.toJSONObject().toString());
}
}
}).init();
}
}
On clicking notification, Mainactivity of my app starts and then the activity initiated by code startActivity(intent) with extra data put using intent.putExtra("url", launchURL) starts, causing 2 instances.
I have tried adding FLAG_ACTIVITY_REORDER_TO_FRONT & FLAG_ACTIVITY_CLEAR_TOP flags but they cause only the initial main activity to start, the activity initiated by startActivity(intent) is not started with these flags.
How to make only 1 instance of activity to start (the one which is initiated by startActivity(intent)) on notification click?
I am writing a code which should return NFC tag value on the next activity ( page ) when NFC get detected during scan. What happens here is that when I launch the app for the first time it the first page shows for a fraction of a second and moves to second page directly ( activity ).
Here is the piece of code for the first activity ( which is just for asking user to tap to scan )
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
askPermissions();
mtxtViewNfcContent = (TextView) findViewById(R.id.text);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
Toast.makeText(this, "No NFC", Toast.LENGTH_SHORT).show();
finish();
return;
}
else {
Intent in = new Intent(Main2Activity.this, MainActivity.class);
startActivity(in);
}
What I want is the the to show the first page at launch and when user tap to nfc scan, show the output on the next page ( MainActivity).
PS : I am new to android, please excuse with my codes.
what are you doing until now is to exit the app when the device doesnot support nfc or to start another activity when the device supports nfc.
you are actually not listening at all to any tag.
here you have two possibilities:
first : read an nfc tag in the first activity and then creat a new intent with and put the result of tag reading as extra bundel.
two : listen to tag existance in the first activity and then send the tag to second one and read it in the second activity.
I would prefer the first secinario.
on firstActivity:
public class MainActivity extends AppCompatActivity {
private PendingIntent pendingIntent;
private IntentFilter[] writeTagFilters;
private NfcAdapter nfcAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.AppTheme);
setContentView(R.layout.activity_main);
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (nfcAdapter == null) {
Toast.makeText(this, "No NFC", Toast.LENGTH_SHORT).show();
finish();
return;
}
setForeground();
}
private void setForeground() {
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
writeTagFilters = new IntentFilter[]{tagDetected};
}
#Override
protected void onResume() {
super.onResume();
if (nfcAdapter != null) {
nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
}
processNfcTag(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}
#Override
protected void onPause() {
super.onPause();
if (nfcAdapter != null) {
nfcAdapter.disableForegroundDispatch(this);
}
}
private void processNfcTag(Intent intent) {
//TODO: here you should to check if this intent is an NFC Intent, in case it is an nfc intent you could read it according of tag tech you have
// for example MifareUltralight.
MifareUltralight mfu = MifareUltralight.get(intent.getParcelableExtra(NfcAdapter.EXTRA_TAG));
try {
mfu.connect();
byte [] bytes = mfu.readPages(pageNumber);
mfu.close();
} catch (IOException e) {
e.printStackTrace();
}
// then you could get this bytes and send it to the other activity
}
please check this link to know how to send data between activities.
p.s: you should to check the code I have wrote it quickly.
You can use intent.putExtra (key,value) while calling the intent and use bundle on the result activity to fetch the variable data
use this while calling the intent
`Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("some_key", value);
intent.putExtra("some_other_key", "a value");
startActivity(intent);`
use this on result activity
`Bundle bundle = getIntent().getExtras();
int valueText = bundle.getInt("some_key");
String valueString = bundle.getString("some_other_key");
TextView textone =(TextView)findVeiwById(R.id.textone);
textone.setText(valueText);
TextView stringTextView = (TextView)FindViewById(R.id.stringTextView)
stringTextView.setText(valueString)`
I have built a WebView application in java.
Basically what i did was Created a WebView loaded a url, but what happened is when i click on the Uri tel:, the app crashes i have no idea what to do.
I have tried to override the Web. I did override WebViewClient.
App always crashing if uri = tel something by this code:
public class MyAppWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("html5rocks.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
Try this:
if (url.startsWith("tel:")) {
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url));
startActivity(intent);
}
You need ACTION_DIAL and not ACTION_VIEW.
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);
}
});
}
}