play mp4 from sdcard of emulator 2.2 - android-emulator

public class MainActivity extends Activity{
MediaController mediaController;
MediaPlayer mp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// Uri uri = Uri.parse("android.resource//org.me.vid_sample/raw/lahainaharbor");
File path = new File("/sdcard/Waileasuntest1.mp4");
VideoView videoView = (VideoView) findViewById(R.id.surface);
videoView.setVideoPath(path.getAbsolutePath());
mediaController = new MediaController(this);
mediaController.setMediaPlayer(videoView);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.requestFocus();
videoView.start();
}

Check in device. I am sure it works.

Related

popup not open in webview really

webview working well but pop-up does not open
public class MainActivity extends AppCompatActivity {
TextView txtMarquee;
private CardView btnDollar, btnFlexiload, btnNewAccount, btnContact, btnOffers, btnNotices, btnYT, btnFB;
private WebView dollarLoad;
public MainActivity() {
}
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMarquee = findViewById(R.id.marqueeText);
txtMarquee.setSelected(true);
//Dollar
CardView buttonDollar = findViewById(R.id.btnDollar);
buttonDollar.setOnClickListener(view -> {
setContentView(R.layout.activity_doller);
dollarLoad = findViewById(R.id.dollarLoad);
dollarLoad.setWebViewClient(new WebViewClient());
dollarLoad.loadUrl("https://iteldollar.com");
WebSettings webSettings = dollarLoad.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setAllowContentAccess(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setDatabaseEnabled(true);
webSettings.setMinimumFontSize(1);
webSettings.setMinimumLogicalFontSize(1);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setSupportMultipleWindows(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
});

In flutter, how can I register if the headset is suddenly unplugged

Android java offers to register a BroadcastReceiver checking for AudioManager.ACTION_AUDIO_BECOMING_NOISY, to listen to the system broadcasting an ACTION_AUDIO_BECOMING_NOISY message, when a sound is played but then the headset is unplugged or a Bluetooth device disconnected.
Is there a way to do this in flutter, to respond to the event that e.g. a headset is unplugged while playing sound?
With the hint in the answer below, I got this going, but only in DEBUG mode, not in a release ready APK. This is what I did:
Java:
public class MainActivity extends FlutterActivity {
public static final String STREAM = "XXX";
public static String TAG = "player/java file";
private IntentFilter intentFilter = new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
private BecomingNoisyReceiver myNoisyAudioStreamReceiver = null;
private class BecomingNoisyReceiver extends BroadcastReceiver {
final EventChannel.EventSink eventSink;
BecomingNoisyReceiver(EventChannel.EventSink eventSink){
super();
this.eventSink = eventSink;
}
#Override
public void onReceive(Context context, Intent intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
Log.w(TAG, "Noisy Receiver activated");
eventSink.success("success");
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new EventChannel(getFlutterView(), STREAM).setStreamHandler(
new EventChannel.StreamHandler() {
#Override
public void onListen(Object args, final EventChannel.EventSink events) {
Log.w(TAG, "adding listener");
myNoisyAudioStreamReceiver = new BecomingNoisyReceiver(events);
registerReceiver(myNoisyAudioStreamReceiver, intentFilter);
}
#Override
public void onCancel(Object args) {
Log.w(TAG, "cancelling listener");
unregisterReceiver(myNoisyAudioStreamReceiver);
}
}
);
}
}
And in Dart, in the class extending State:
static const platform = const EventChannel('XXX');
StreamSubscription _noisySubscription;
in initState():
_noisySubscription = null;
Whenever I need it:
if(_noisySubscription == null){
_noisySubscription = platform.receiveBroadcastStream().listen(_handleNoisy);
}
Whenever it needs to stop:
_noisySubscription.cancel().then((_){_noisySubscription = null;});
Any hint, how to fix this problem?
You can use EventChannel to communicate an event on native part (Android) to Dart part. Ref: https://medium.com/#svenasse/flutter-event-channels-89623ce6c017
(Pseudo) Sample Code (it's not in runable state, but hopefully it gives you idea):
Dart part
void _handleNoisy(noisyEvent) {
debugPrint("noisyEvent $noisyEvent");
}
static const stream =
const EventChannel('com.yourcompany.yourapp/ACTION_AUDIO_BECOMING_NOISY');
StreamSubscription _noisySubscription = stream.receiveBroadcastStream().listen(_handleNoisy);
Java part
public class MainActivity extends FlutterActivity {
public static final String STREAM = "com.yourcompany.yourapp/ACTION_AUDIO_BECOMING_NOISY";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new EventChannel(getFlutterView(), STREAM).setStreamHandler(
new EventChannel.StreamHandler() {
#Override
public void onListen(Object args, final EventChannel.EventSink dartEvents) {
Log.w(TAG, "adding listener");
noisyEvent = ... // Register
private class BecomingNoisyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (AudioManager.ACTION_AUDIO_BECOMING_NOISY.equals(intent.getAction())) {
dartEvents.success("the payload you want to pass to dart")
}
}
}
}
#Override
public void onCancel(Object args) {
Log.w(TAG, "cancelling listener");
}
}
);
}
}

ToggleButton code in Android Studio unexpectedly crashes the App

My project is a music player that has a ToggleButton for play/pause.
I tried to run a code in Android Studio, but it unexpectedly crashes the App.
I am trying to follow up some tutorials on the internet and YouTube guides, but nothing works so far.
Here is the code that I'm running in the MainActivity:
package com.example.hamzeh.playpausestop;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
MediaPlayer Sound;
int pause;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void stop(View view)
{
Sound.release();
}
public void onToggleClicked(View view)
{
boolean checked = ((ToggleButton)view).isChecked();
if (checked)
{
Sound.start();
//Play
}
else
{
Sound.pause();
pause = Sound.getCurrentPosition();
//Pause
}
}
}
Post your logcat result i will give better answer your class have no any initialization of sound object and also check in xml onClick tag is onToggleClicked and stop is defined or not.
public class MainActivity extends AppCompatActivity {
MediaPlayer Sound;
int pause;
//
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize Mediaplayer here for single sound
Sound= MediaPlayer.create(MainActivity.this, R.raw.UrSoundFileInRawFolder);
}
public void stop(View view){
Sound.release();
}
public void onToggleClicked(View view){
boolean checked = ((ToggleButton)view).isChecked();
if (checked && !Sound.isPlaying() && Sound!=null){
Sound.start();
}
else if(Sound.isPlaying()){
Sound.pause();
pause = Sound.getCurrentPosition();
} esle{
Toast.makeText(MainActivity.this, "SomeThingWrong", Toast.LENGTH_SHORT).show();
}
}
}

Android Check for Wireless connection upon Returning to Activity from Settings

My Android application is mainly a Webview (labeles WebViewActivity) that navigates a mobile website. I have a connection detector that opens another activity on every URL load if there is no wireless connection. From this next activity there is a button that opens up the Wireless Settings. Upon pressing the back button I would like for the second activity (labeled MainActivity) to refresh itself and continue back to whichever page my Webview had loaded. What do I need to change in the MainActivity.java to execute that?
My MainActivity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
#Override
protected void onRestart(){
if(checkConnection()){
Intent intent= new Intent(this, WebViewActivity.class);
startActivity(intent);
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
super.onBackPressed();
}
public void openSettings(View view){
Intent intent= new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
startActivity(intent);
}
public boolean checkConnection(){
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
//Get Internet Status
isInternetPresent = cd.isConnectingToInternet();
if(!isInternetPresent)
return false;
return true;
}
}
My WebviewActivity:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends Activity {
private WebView mWebView;
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// TODO Auto-generated method stub
if (checkConnection()) {
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
mWebView.loadUrl("http://my.fellowshipnwa.org/?publicapp");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new myWebClient());
} else {
openSplash();
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
// Let the system handle the back button
new AlertDialog.Builder(this)
.setTitle("Exit myFellowship App?")
.setMessage("Are you sure you want to exit?")
.setNegativeButton(android.R.string.no, null)
.setPositiveButton(android.R.string.yes, new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
WebViewActivity.super.onBackPressed();
}
}).create().show();
}
}
public class myWebClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(!checkConnection()){
openSplash();
return true;
}else{
if( url.startsWith("tel:")){
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
startActivity(intent);
return true;
}
else if( url.startsWith("mailto:")){
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
startActivity(intent);
return true;
}
}
return false;
}
}
public void openSplash(){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public boolean checkConnection(){
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
//Get Internet Status
isInternetPresent = cd.isConnectingToInternet();
if(!isInternetPresent)
return false;
return true;
}
}
Any help is Greatly Appreciated! Even if it means telling me I goofed up on creating this question.
Thank you friends.
I played with it for a while and used a few other examples to piece together something that is working. One small hitch is if the user turns on a connection, but it is not quite connected when they return to the app it will stay on the ConnectorActivity. So I intercept the back button to check for a connection. If present, it will finish the current activity. Then, upon returning to the webview it will reload.
ConnectorActivity.java:
#Override
protected void onRestart(){
super.onRestart();
if(checkConnection()){
finish();
}
}
#Override
// Detect when the back button is pressed
public void onBackPressed() {
if(checkConnection()){
super.onBackPressed();
}
}
Piece from WebViewActivity.java:
#Override
protected void onRestart(){
super.onRestart();
mWebView.reload();
}

Capture 5 images after a button click in android

I am developing android application. The aim is to capture the 5 images after click a capture button. I am able to capture a single image by using the following code. But how can I take 5 images after a single button click. Please Can someone help me to do this?
Code snippet:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.camera.facedetectionexample.R;
public class FaceDetectionExample extends Activity {
private static final int TAKE_PICTURE_CODE = 100;
private Bitmap cameraBitmap = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button)findViewById(R.id.take_picture)).setOnClickListener(btnClick);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(TAKE_PICTURE_CODE == requestCode){
processCameraImage(data);
}
}
private void openCamera(){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE_CODE);
}
private void processCameraImage(Intent intent){
setContentView(R.layout.detectlayout);
((Button)findViewById(R.id.detect_face)).setOnClickListener(btnClick);
ImageView imageView = (ImageView)findViewById(R.id.image_view);
cameraBitmap = (Bitmap)intent.getExtras().get("data");
imageView.setImageBitmap(cameraBitmap);
}
private View.OnClickListener btnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.take_picture:
openCamera();
break;
}
}
};
}
you are going to want to use burst mode, have a look at
How to make burst mode available to Camera
should make it pretty clear.