Can't play this video error - java

public class MainActivity extends AppCompatActivity {
Button clk;
VideoView videov;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clk=(Button) findViewById(R.id.video);
videov=(VideoView)findViewById(R.id.videoView);
}
public void videoplay(View v){
String videopath = "android.resource://"+getPackageName()+"+R.raw.movie";
Uri uri =Uri.parse(videopath);
videov.setVideoURI(uri);
videov.requestFocus();
videov.start();
}
}
Can't play this video error...!! see the picture Screen Shot
What to do ?
After pressing play button it says cant play this video..!!
Need solution of this problem.

Hi day before yesterday i had same problem and tried almost everything but didn't get any success. After that i used this library and it work fine. Just follow few steps:
Step1. Add it to your gradle
compile "fm.jiecao:jiecaovideoplayer:4.7.0"
Step2. Add it as your video play in xml layout.
<fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard
android:id="#+id/videoPlayer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Step 3. Check from here how to use this library in your class,
public class PlayVideoActivity extends BaseActivity {
#BindView(R.id.videoPlayer)
JCVideoPlayerStandard mVideoPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
restoreFromIntent(getIntent());
}
#Override
public int getLayout() {
return R.layout.activity_play_video;
}
//create intent for this activity with all the necessary params
public static Intent createIntent(Context context, String videoUrl) {
Intent intent = new Intent(context, PlayVideoActivity.class);
intent.putExtra(ValueConstants.VIDEO_URL, videoUrl);
return intent;
}
// get video path from intent and play the video here
private void restoreFromIntent(Intent intent) {
String videoPath = intent.getExtras().getString(ValueConstants.VIDEO_URL);
mVideoPlayer.setUp(videoPath
, JCVideoPlayerStandard.SCREEN_LAYOUT_LIST, "");
}
#Override
public void onBackPressed() {
if (JCVideoPlayer.backPress()) {
return;
}
super.onBackPressed();
}
#Override
protected void onPause() {
super.onPause();
JCVideoPlayer.releaseAllVideos();
}
}
One more bonus thing from my side. You can do video cache also by using this library. Yesterday i found this also.One time play from internet.After it play without internet also.
Updated answer:
Above example i have provided for playing online videos from url but this question have problem related to video path problem.
Just Changed this path:
String videopath = "android.resource://"+getPackageName()+"+R.raw.movie";
Uri uri =Uri.parse(videopath);
To this,
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.yourvideo);
Thanks hope this will help you.

Related

How to close/exit outside settings intent created by the application

I'm currently working on a simple application that is used to force stop some applications with the help of Accessibility permission. It automatically opens the Application Info Settings page and presses the Force Stop button (The main part is working fine).
But After the process, I can't minimize or close the settings page. I used onAppStopped() method (starts the MainActivity intent), but it repeats the process recursively. I think it is because the settings page is not a part of the main application.
Is there any way to override this issue?
MainActivity
protected void onCreate(Bundle savedInstanceState) {
FSAccessibilityService.setClient(this);
forceStopApp("package name");
}
public void forceStopApp(String packageName) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.parse("package:" + packageName);
intent.setData(uri);
startActivity(intent);
}
#Override
public void onAppStopped() {
startActivity(new Intent(this, MainActivity.class);
}
FSAccessibilityService extends AccessibilityService
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
performForceStop(event);
}
private void performForceStop(AccessibilityEvent event) {
AccessibilityNodeInfo source = event.getSource();
performClickButtonByText(source, "FORCE STOP");
performClickButtonByText(source, "OK");
getClient().onAppStopped();
}
public interface FSClient {
void onAppStopped();
}

Cannot resolve symbol 'x'

Hello I'm starting in Android Studio and I wanted to know how to pass from one activity to another. I've tried different methods which I've seen in youtube tutorials but all of them show me the same error:
Cannot resolve symbol 'activity_menu'.
Does anyone know how to solve it?
Here is my code:
public class Menu extends AppCompatActivity {
Button boton_start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
boton_start=(Button) findViewById(R.id.boton_menu);
boton_start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(activity_menu.this,activity_dishes.class);
startActivity(in);
}
});
}
}
The same happens with the other activity, but I suppose that the solution is the same for both.
You've mentioned the layout file names instead of name of the activities
Your code:
Intent in = new Intent(activity_menu.this,activity_dishes.class);
It should be:
Intent in = new Intent(Menu.this,Dishes.class);
Mention the name of the activity, not layout files.
Try getting activity_menu like this: R.layout.activity_menu

Displaying captured image in another activity

I am working on a android app and part of it requires the user to take a picture, the moment the user takes the picture it saves it to the file system and start a new activity to display the recent captured picture. The activity starts with no problem except it's blank!
When I hard-code the path into a string by typing the path manually, the image appears, so what's the problem?
This is the method of the saved file and creating the intent;
capturePicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
File f = capturePicture();
Intent intent = new
Intent(postSharingActivity.this, settingPostParam.class);
intent.putExtra("picture", f);
startActivity(intent);
}
});
This is the other activity and retrieving the image file;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting_post_param);
savedImage = findViewById(R.id.savedImage);
File pic = (File) getIntent().getExtras().get("picture");
Uri uri = Uri.fromFile(pic);
Toast.makeText(settingPostParam.this, "+uri.toString(),Toast.LENGTH_LONG)
.show();
if (isStoragePermissionGranted()) {
Picasso.get().load(uri).into(savedImage);
} else {
Toast.makeText(settingPostParam.this, "error", Toast.LENGTH_LONG).show();
}
}
UPDATE: I found the problem, it execute the intent before the capturePicture() complete. It takes the path before even the file is saved!
So i changed back to my original way, which is changing views not activities.
And added the AsyncTask.
private class myAsyncTask extends AsyncTask<Void,Void,Void>{
File f;
#Override
protected Void doInBackground(Void... voids) {
f= capturePicture();
return null;
}
#Override
protected void onPostExecute(Void result){
String rr = f.getAbsolutePath();
File g=new File(rr);
Toast.makeText(postSharingActivity.this,"onBloodyClick: "+g.toString(),Toast.LENGTH_LONG).show();
Uri uri = Uri.fromFile(g);
Toast.makeText(postSharingActivity.this,"URI :"+uri.toString(),Toast.LENGTH_LONG).show();
if(isStoragePermissionGranted()){
savedImage.setImageURI(uri);
cameraPreviewView.setVisibility(View.GONE);
postCaptureView.setVisibility(View.VISIBLE);
} else{
Toast.makeText(postSharingActivity.this,"error",Toast.LENGTH_LONG).show();
}
}
}
And here is the onClickListener
capturePicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new myAsyncTask().execute();
}
});
I've tested it and it is the same problem, the Toasts are in this order:
onBloodyClick: /storage/0/emulated/0/DCIM/Camera/IMG_20180604_104845.jpg
URI:file:////storage/0/emulated/0/DCIM/Camera/IMG_20180604_104845.jpg
Saved: /storage/0/emulated/0/DCIM/Camera/IMG_20180604_104845.jpg
The third one is in the capturePicture() method.
What am i doing wrong here? And thanks for replying.
Don't pass file object through intent, pass the path(String) of the file.
intent.putExtra("picture",f.getAbsolutePath());
And then, receive file path form this code
File pic = new File( getIntent().getStringExtras("picture"));
I found it.
The problem is in the calling of the methods and returning types.
The returning type of the method doInBackground and the params type of the two methods, the onPostExecute is going to use the resulting file of the doInBackground method
private class myAsyncTask extends AsyncTask<File,Void,File>{
#Override
protected File doInBackground(File... voids) {
File f = capturePicture();
return f;
}
#Override
protected void onPostExecute(File f){
String rr = f.getAbsolutePath();
File g=new File(rr);
Toast.makeText(postSharingActivity.this,"onBloodyClick: "+g.toString(),Toast.LENGTH_LONG).show();
Uri uri = Uri.fromFile(g);
Toast.makeText(postSharingActivity.this,"URI :"+uri.toString(),Toast.LENGTH_LONG).show();
if(isStoragePermissionGranted()){
savedImage.setImageURI(uri);
cameraPreviewView.setVisibility(View.GONE);
postCaptureView.setVisibility(View.VISIBLE);
} else{
Toast.makeText(postSharingActivity.this,"error",Toast.LENGTH_LONG).show();
}
}
}
And then on the onClickListener, you specify the order
capturePicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myAsyncTask task=new myAsyncTask();
task.doInBackground();
task.execute();
}
});

While using the IntentIntegrator from the ZXing library, can I add a flash button to the barcode scanner via the Intent?

I am scanning barcodes and QR codes from an Android app via Intent using the ZXing Library and its port of the Android Application. I added the following two lines in my Gradle dependencies to use the android-integration code without modification:
compile 'com.journeyapps:zxing-android-embedded:3.2.0#aar'
compile 'com.google.zxing:core:3.2.1'
And I am using IntentIntegrator in my Activity to scan a barcode in the onCreate() like this:
integrator = new IntentIntegrator(this);
integrator.setOrientationLocked(false);
integrator.setPrompt(getString(R.string.scanner_text)); // Set the text of the scanner
integrator.setCameraId(0); // Use a specific camera of the device
integrator.setBeepEnabled(true); // Enable beep in the scanner
integrator.setBarcodeImageEnabled(false); // Do not fetch image from the camera
integrator.initiateScan();
Everything works and I get correct scanned result, but I want a flash button in the lower right corner of the scanner like this:
I can already control the flash using the volume up and down keys because I override the CaptureActivity.
Is a flash button like the one above already there in the barcode scanner which can switch between AUTO, ON and OFF mode? If there is, can I use the addExtra() method of the IntentIntegrator to activate it? Or is the only way to implement this would be to modify the entire code according to my needs?
I had overlooked this page on Embedding BarcodeView and these sample activities which show how to customise the Barcode Scanner according to your needs. The example activity that helped me was CustomScannerActivity.
There isn't a option in the IntentIntegrator class to implement a flash button natively. Instead I should make a custom layout for the Barcode Scanner, use it in a custom activity and call this activity from the IntentIntegrator.
I have two activities. One is the ScannerActivity and other one is the CallingActivity. A mistake that confused me for a while was that I created an instance of IntentIntegrator in the onCreate() method of ScannerActivity. It should be in the CallingActivity.
In the example given a Button is used and the text of the Button is changed according to the flash. I created a new Android Layout called activity_custom_scanner where I replaced the Button with a ToggleButton and used images for the button instead to get my desired Flash On/Off Button.
So my ScannerActivity looks like this:
public class CustomScannerActivity extends Activity implements
CompoundBarcodeView.TorchListener {
private static final int BarCodeScannerViewControllerUserCanceledErrorCode = 99991;
private static final String TAG = CustomScannerActivity.class.getSimpleName();
private CaptureManager capture;
private CompoundBarcodeView barcodeScannerView;
private ToggleButton switchFlashlightButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_scanner);
barcodeScannerView = (CompoundBarcodeView)findViewById(R.id.zxing_barcode_scanner);
barcodeScannerView.setTorchListener(this);
switchFlashlightButton = (ToggleButton)findViewById(R.id.switch_flashlight);
switchFlashlightButton.setText(null);
switchFlashlightButton.setTextOn(null);
switchFlashlightButton.setTextOff(null);
// if the device does not have flashlight in its camera,
// then remove the switch flashlight button...
if (!hasFlash()) {
switchFlashlightButton.setVisibility(View.GONE);
}
switchFlashlightButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Save the state here
if (isChecked) {
barcodeScannerView.setTorchOn();
} else {
barcodeScannerView.setTorchOff();
}
}
});
capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.decode();
}
#Override
protected void onResume() {
super.onResume();
capture.onResume();
}
#Override
protected void onPause() {
super.onPause();
capture.onPause();
}
#Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
/**
* Check if the device's camera has a Flashlight.
* #return true if there is Flashlight, otherwise false.
*/
private boolean hasFlash() {
return getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
#Override
public void onTorchOn() {
// necessary override..
}
#Override
public void onTorchOff() {
// necessary override..
}
}
And the CallingActivity looks like this:
public class CallingActivity extends Activity {
private static final String TAG = CallingActivity.class.getSimpleName();
private static final int BarCodeScannerViewControllerUserCanceledErrorCode = 99991;
String uuid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uuid = getIntent().getStringExtra("uuid");
new IntentIntegrator(this).setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class).initiateScan();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
if (scanResult != null) {
// handle scan result
Log.i(TAG, "Text from Barcode Scanner: " + scanResult.getContents());
getIntent().putExtra("data", scanResult.getContents());
getIntent().putExtra("uuid", uuid);
}
}
else if (resultCode == RESULT_CANCELED) {
getIntent().putExtra("error", "User canceled");
getIntent().putExtra("error_code", BarCodeScannerViewControllerUserCanceledErrorCode);
}
else
{
getIntent().putExtra("error", getString(R.string.scanner_error));
getIntent().putExtra("error_code", BarCodeScannerViewControllerUserCanceledErrorCode);
}
setResult(resultCode, this.getIntent());
this.finish();
}
}
I am not sure if it's the perfect way, but that's how I did it.
Hope it helps someone!
There is a setTorchOn method in CompoundBarcodeView so you can check that method out and try to implement it for your needs. Hope it helps.
This question is probably too old, but you can use the Volume buttons to turn on/off the torch while scanning.

How do i get a .wav sound to play?

Im making an app, and i want it to make a sound when a activity is opened , the sound file is in R.raw.sound_file , if someone could do some example code to make my app play a sound that would be great.
doesn't the android.media.MediaPlayer class do this?
Reference: http://developer.android.com/reference/android/media/MediaPlayer.html
Example: http://developer.android.com/guide/topics/media/index.html
Step 2 of the example says:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
In your case, I'd use the onStart() inside your Activity class:
public class MyActivity extends Activity {
...
protected void onStart() {
super.onStart();
MediaPlayer mp = MediaPlayer.create(this, R.raw.sound_file_1);
mp.start();
}
...
}
I have experience using the MediaPlayer object for an android app I created, and I discovered the following:
Wav files have problems in MediaPlayer if they have a bitrate of 32kbps, but wav files of higher bit rates seem to play ok, even if it is a large wav file it will play ok as long as it has the higher bitrate.
If at all possible, use mp3 files for your audio, I encountered no problems whatsoever with mp3 audio files using the MediaPlayer object, so that is the best way to go, using google there are lots of different kinds of mp3 sounds available free from rings and dings, to dog barks, and cat meows, or whatever kind of sound you are looking for.
Try with my code, It's works perfectly. Also you need to have the sound file .wav en res/raw
public class PianoActivity extends Activity {
private MediaPlayer mediaPlayer = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_piano);
setupUI();
}
#Override
protected void onPause() {
super.onPause();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
private void setupUI() {
findViewById(R.id.doo).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
managerOfSound();
}
});
}
private void managerOfSound() {
mediaPlayer = MediaPlayer.create(this, R.raw.doo);
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
} else {
mediaPlayer.stop();
}
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
}
});
}
}
I had the same problem.
this worked for me by using the application context as follows:
public class MyActivity extends Activity {
...
protected void onStart() {
super.onStart();
Context appContext = getApplicationContext();
MediaPlayer mp = MediaPlayer.create(appContext , R.raw.sound_file_1);
mp.start();
}
...
}
Also, don't forget to call mp.release() once you're done
Another,preferred option is to use the SoundPool class

Categories

Resources