Android MediaRecorder Fragment how to keep the same instance - java

I have a MediaRecorder class, that extends Fragment. Inside that class I have created methods to set up the MediaRecorder and also methods to start and stop recording.
I have tried creating onTouch withing the Fragment and using start/stop inside the fragment on button presses and it worked. But when I removed onTouch and tried to use the start/stop methods from another class I got NullPointerException. To be more precise, it gave no error when pressing start but gave an error when I pressed stop!
After debugging startRecordingVideo all 3 -> (mCameraDevice, mTextureView, mPreviewSize) are null so it does not even start recording and during stopRecordingVideo the mMediaRecorder is also null.
Here is the part where I try to use the MediaRecorderFragment inside another class:
private Camera2VideoFragment camera2VideoFragment;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song_player);
//Camera2Video
camera2VideoFragment = new Camera2VideoFragment();
//Start Camera Preview
getFragmentManager().beginTransaction()
.replace(R.id.container, Camera2VideoFragment.newInstance())
.commit();
playRecordButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionevent) {
final int action = motionevent.getAction();
if (action == MotionEvent.ACTION_DOWN) {
//Start recording video
startRecordingVideo();
} else if (action == MotionEvent.ACTION_UP) {
//Stop Recording Video
stopRecordingVideo();
}
return false;
}
});
}
private void startRecordingVideo(){
camera2VideoFragment.startVideoRecording();
}
private void stopRecordingVideo(){
camera2VideoFragment.stopVideoRecording();
}
The MediaRecorderFragment can be found here. (This class is too long to post here).
My question is, why do I get the NullPointerException and how can I make that fragment work from another class.

Related

Drag a view onLongClick and start activity onClick

I'm banging my head with this problem which probably is simple but since I'm new to this topic I somehow haven't been able to figure it out yet.
I've successfully implemented dragging a view with onTouch method. I've also successfully implemented onLongClick and onClick methods. But both of these functionalities were implemented separately.
The problem, like the title says is when I want to join these functionalities. I want the onTouch method to be called when a user long clicks a view and I want a new activity to start when a user clicks a view.
Here is the pseudo code:
public class Website extends AppCompatActivity implements View.OnTouchListener{
TextView longpress;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_website);
longpress = (TextView) findViewById(R.id.longpress);
longpress.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view){
//I don't really know how to do this part
onTouch(View view, Motion Event event);
return true;
}
});
longpress.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Code for new activity comes here (I know how to do this part)
}
});
}
public boolean onTouch(View view, MotionEvent event) {
switch(event.getAction(){
case MotionEvent.ACTION_DOWN:
//Save initial coordinates of view <-- view.getX(), view.getY()
break;
case MotionEvent.ACTION_MOVE:
//Calculate dX and dY and setX and Y of the view (move view)
break;
case MotionEvent.ACTION_UP:
//If view is certain distance away from initial points do something
break;
}
}
}
Like I said, onTouch works on itself if I don't try to call it from onLongClick method. If I try to call onTouch(View view, MotionEvent event) from onLongClick method the problem occurs because onLongClick only receives one out of two arguments onTouch method should receive (onLongClick only receives view argument but it should also receive event argument).
Maybe I'm trying to do this in a totally wrong way but I have been looking at some documentation e.g. https://developer.android.com/training/gestures/ but still won't get an idea what to do.
(I would like to have a similar functionality to notifications on android phones)
So I've come to a solution which might or might not be a good one but for now it serves my functionality. If someone has a better solution and thinks mine is bad in some way please say so.
Here is the code:
boolean flag;
public boolean onTouch(View view, MotionEvent event){
int action = event.getAction() & MotionEvent.ACTION_MASK;
if(action == MotionEvent.ACTION_DOWN){
//do something on a down press
flag = true;
return true;
}
if(action == MotionEvent.ACTION_UP && flag == true){
//do something if we move finger away from screen we
//didn't move the view first
return true;
}
if(action == MotionEvent.ACTION_UP && flag == false){
//do something if we move finger away from screen and we moved
//the view before we moved the finger away from screen
}
if(action == MotionEvent.ACTION_MOVE){
//do something when moving the view
flag = false;
}

Use capacitive Back button to goBack(); in fragment activity in eclipse

i have build a new app and in that app i have 3 fragments with webview in it. basically i want to implement goBack(); on capacitive back button. i did tried
#Override
public void onBackPressed() {
webView.goBack();
return;
}
and also
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
webView.goBack();
return false;
}
return super.onKeyDown(keyCode, event);
}
but i end up with errors. so can anyone help me doing this. Thank you
and it is a
public class AdminM extends FragmentActivity implements ActionBar.TabListener
You get "The method onBackPressed() of type AdminM.Fragment2 must override or implement a supertype method" because you added Override annotation, but there is no onBackPressed method in Fragment. You can override onBackPressed only in an Activity.
You can look at the Activity and Fragment docs to see what methods you can override and what methods you can not override.
The right way would be to retrieve the Fragment from FragmentManager every time before accessing it. This way you can be sure you're using the right instance. If the Fragment is null, it's not attached.
In Activity
#Override
public void onBackPressed() {
final WebViewFragment fragment = (WebViewFragment) fragmentManager.findFragmentById(fragmentId);
if (fragment == null || !fragment.onBackPressed()) {
super.onBackPressed();
}
}
In Fragment, create a method that returns true if WebView got back and false if not.
Check every time if webView is null, because fragmen has it's own lifecycle mechanism.
public boolean onBackPressed() {
if (webView != null && webView.canGoBack()) {
webView.goBack();
return true;
}
return false;
}

How can I make a piece of code loop while a Button is pressed and stop it once the Button is no longer Pressed?

I am using the android developer tools in Eclipse, programming in Java, and I need to make an object move across the screen as long as a button is pressed. I've been doing research for hours, and I cannot find any methods to accomplish this. I've tried running threads, which often crash or seemingly don't execute. I've also tried an onClickListener which reads the button state and uses it to determine whether or not the button is still pressed. I'm currently using a while loop, but this just freezes the program. I believe that this is the best method, and I've tried to use Thread.sleep in order to limit the number of iterations per second, as I believe that this is the reason it is freezing. Am I on the right track or am I way off in left field? Here is a snippet of code:
rightButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
while(arg0.isPressed())
{
mover.updateCoordinates(1, 0);
}
}
});
Would you try this another method?
Firstly declare your button as class variable, declare a Handler and a Runnable:
private Button rightButton; // You will assign this in onCreate() method
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
if(rightButton.isPressed())
{
// If press state is pressed, move your item and recall the runnable in 100 milliseconds.
mover.updateCoordinates(1, 0);
mHandler.postDelayed(mRunnable, 100);
}
}
};
Then your button's onClickListener will looks like this:
rightButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
// Instead of performing a loop here, just call a runnable, do simple press state checking there.
mHandler.postDelayed(mRunnable, 100);
}
});
Create a loop that updates the views, waits, and calls itself after it finishes waiting. Within the loop, have an animation if statement with a boolean field that move on true and does not move on false. Have the onClick method toggle the boolean field's value.
Try:
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
// Start your animation here
} else if (e.getAction() == MotionEvent.ACTION_UP) {
// Stop your animation here
}
return false;
}
});
References:
MotionEvent
OnTouchListener
Alternatively, override onKeyUp and onKeyDown of the View class and check for the KEYCODE_ENTER KeyEvent

Android: How to stop this music already?

I have created an application and with some background music.
Result = SUCCESS
Problem is.. -.- if app receives a call, music is still playing and plays instead of the user's original ringtone.
Plus if "Home" button is selected on the phone. Activity exits but the music is still running?
I managed to quit the music if the "Back" button on the phone is selected.
other than that music runs 24/7.
What I have used is.
A menu option in the application that when the user selected "Menu" a list of options pop up and an option to choose "Settings..."
If the user selects "Settings..." a preference dialogue pops up to check or uncheck music (ON/OFF)
Result = SUCCESS
The music plays throughout the application, from activity to activity.
This is my media player code in my application.
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
Music.play(this, R.raw.bgmusic);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
finish();
Music.stop(this);
return true;
}
return super.onKeyDown(keyCode, event);
}
This is my music.java
public class Music {
private static MediaPlayer mp = null;
/** Stop old song and start new one */
public static void play(Context context, int resource) {
stop(context);
// Start music only if not disabled in preferences
if (Prefs.getMusic(context)) {
mp = MediaPlayer.create(context, resource);
mp.setLooping(true);
mp.start();
}
}
/** Stop the music */
public static void stop(Context context) {
if (mp != null) {
mp.stop();
mp.pause();
mp.release();
mp = null;
}
}
}
anyone have a clue?
You need to call
Music.stop(this);
in the onPause method. This may have the effect of an instant glitch, when switching from one of yours activities to another.
Read also this question and the accepted answer.

i want call MotionEvent.ACTION_DOWN in one activity and MotionEvent.ACTION_UP in another activity

ImageView ii = (ImageView)v.findViewById(R.id.picture);
ii.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent e) {
ImageView i2 = (ImageView)findViewById(R.id.btn_imagetest);
if(e.getAction() == MotionEvent.ACTION_DOWN){
//call activity one
}
if (e.getAction() == MotionEvent.ACTION_UP)
{
//call activity 2
}
return true;
}
});
when i touch on image 'ii' i should go to imgview(i2)(can be activity too) and when i leave the imgview should go away(back to same activitiy) ..(Note:imgview is covers full screen in phone)
i have no idea..how to proceed.
If I understand correctly, you're asking for information on how to start another Activity from the current Activity. For information on that, take a look at Android - finishing activity from another activity
Also, take a look at the Notepad Tutorial, for a complete example.

Categories