Null point exception when loading a 'raw' MediaPlayer resource in two activities - java

I am struggling with loading a sound in my application. I'm receiving a null point exception when trying to create the same "sound" in two activities. I have a variable clickSound that i declare in both activities which accesses the same Raw file.
I have added a folder named 'raw' in the 'res' folder and the file clicksound.mp3 in it.
The thing is, for MainActivity is working perfect. for Second activity i just receive the exception.
Main Activity:
// Declare clickSound onCreate
final MediaPlayer clickSound = MediaPlayer.create(this, R.raw.click_button);
Second Activity:
private final MediaPlayer mClickSound = MediaPlayer.create(this, R.raw.click_button);
The strange thing is that it worked some time ago but now i don't understand why not anymore. Any suggestions what is happening?

This line:
private final MediaPlayer mClickSound = MediaPlayer.create(this, R.raw.click_button);`
for that moment the instace in not valid initialized, so this is not givin all that you need to create a media player.... move that to the onCreate method

I think this error is related when you don't initialize your class.
Try before something like this:
MediaPlayer mClickSound = new MediaPlayer();
Or full code:
private final MediaPlayer mClickSound;
//other things
MediaPlayer mClickSound = new MediaPlayer();
mClickSound = MediaPlayer.create(this, R.raw.click_button);
It could be also that you're using two different names "clickSound" and "mClickSound"

The problem was not with the MediaPlayer file. I had an error with my Json Reader that was not handling well an exception.

Related

Play audio with more than one file

How do i play audio
In more than one file
Where I can move between pages without stopping sounds
I see it a lot in games
I hope to get your answer
It depends on callback function onDestroys() because your App works as a process in the Androids OS when you change the Activity the stopService() kill the music playing in your current activity.
Do not call the stopService() method.
Create MediaPlayer object outside onCreate()
public static MediaPlayer mediaPlayer;
Then do create inside onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.dali); //R.raw.dali is directory from folder raw that i made and the file name is dali
mediaPlayer.start(); //start the song
mediaPlayer.setLooping(true); //set the song to loop if you want it play forever (true) and if you dont want to loop (false)
}
(let say this code is in MainActivity.class) whenever activity that you dont want to play the song just call
MainActivity.mediaPlayer.stop();

Android QR Scanner: How to quit ZXingScannerView.ResultHandler to get back to where I came from

In my first steps in exploring Android I now start with QR scanning.
Works all pretty well. But I am not able to come back from the ResultHandler after read the QR successfully to my MainActivity.
public class MainActivity extends AppCompatActivity implements
ZXingScannerView.ResultHandler
{
private ZXingScannerView mScannerView
....
#Override
public void handleResult(Result rawResult)
{
// my results are ok in rawResult
// the scanner does not scan anymore but it is still there
// how to go back to my main activity???
}
public void ClickButton (View view)
{
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
}
}
I tried
mScannerview.stopCameraPreview
mScannerView.stopCamera
this.finish
setContentView(R.layout.activity_main); // shows my activity_main
// but I can not click anything
Thanks!!
EDIT
I added some code to describe it a bit better. The idea is from
https://www.numetriclabz.com/android-qr-code-scanner-using-zxingscanner-library-tutorial/
Your question isn't clear but I'm assuming you want to restart the scan process. Normally, you'd have to restart the SurfaceHolder to be in preview mode. Luckily for you the ZXingScannerView already has a method to do that. Call mScannerView.resumeCameraPreview(this) to restart the scan process.
Otherwise can you clarify? You say you want to go back but you're already in MainActivity
If you want to go back into activities/fragments stack you can try Activity.onBackPressed()
if you are in a fragment you must call this method against attached Activity
What do you want is not going back to your activity. You want to restore activity's layout.
I think the better choice is to add ScannerView to your activity's layout file with android:visibility="gone". Then in on click you can get this view and change it's visibility to VISIBILE.
Then when you have handled scanning result, you can reset yuoir ScannerView to visibility = GONE
I too was stuck with this problem for an hour, just like you. And later realised..
To solve this problem DON NOT implementing the ZXingScannerView in the same activity or fragment. Instead start a new activity when you click the button and this activity is just for the ZXingScannerView
Once the Scan is done finish and pass the data back to your activity or fragment
Just restart your MainActivity before this.finish()
the code below will start your main activity through intent...
worked fine for me
startActivity(new Intent(this,MainActivity.class));
this.finish();
remove from onCreate method this line setContentView(your layout) and when you finish scan write it after you stoped the camera then you can use your layout after scan
I had a bit of a look into android concepts and activities.
I put the QR handling in a 2nd activity and it worked well with the finish ().
Thanks for help anyway!!
I think it's too late, but I came with the same problem and I had so find a solution by myself.
You were on the right way, you need two steps more.
I called the methods where I link and set the listener of any buttons
There are the methods
Basically you were right where you set the content view, but you need to give the buttons their functionality back.
(I know its late, but better late than never). Good luck!

I can't get the mediaplayer working in android studio

I'm trying to get some sound in my app, but I can't get the mediaplayer working! I got some errors like:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
with the following code:
private MediaPlayer mPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
mPlayer = MediaPlayer.create(this, R.raw.NAMEOFSOUND);
}
protected void playBtnClicked(){
mPlayer.start();
}
It just doesn't work, whatever I do ... Does someone got some tips/Can someone help me out? The ''NAMEOFSOUND'' is a .mp3 file, I don't know if it even matters? Thank you!
SOLVED: I just solved it by coverting the file with a converter. Just changing the name+.mp3 didn't solve it; NullPointerException in Java Android App MediaPlayer , comment of Berty did the trick!
As you can see in the supported Media formats .mav is not supported by android. you should convert it to mp3 or any of the supported file formats

Using Audio in Android, what do I do with "context"?

I am trying to play an audio file using the code below. I have the audio fileset p and everything is goo except for where it says context.context comes up in red so how do i `fix that or what am I suppoesed to put in there to make the snippet of code work. Any and all help is appreciated and thanks in advance. BTW i am using android studio if that helps.
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();
I figured it out all i have to replace context with is "this" so the code is like below. Leaving this up in case someone in the future needs it.
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.sound_file_1);
mediaPlayer.start();

Puzzling java.lang.NullPointerException in Android App

I'm currently working on an Android App and, almost every time I use it, I get a an error. Most of the time it doesn't crash the app, but it is still frustrating that I'm seeing this error. I thought I did a check for it, but I could be wrong. Thanks for the help! The relevant code is below. (It's relevant because the line outside the if statement is throwing the NullPointerException.)
Activity activity;
if(activity == null)
{
activity = new Activity();
}
Intent intent = new Intent(activity, Service.class);
You don't usually instantiate the Activity class in this manner. Please see the documentation on the Android Activity class here:
http://developer.android.com/reference/android/app/Activity.html
You should post some more of the surrounding code, but your problem is that creating new Intent requires a valid Context. Usually you create an Intent within an Activity (or Service or BroadcastReceiver) class so you can just do something like:
Intent intent = new Intent(this, Service.class);
Occasionally you'll create it somewhere else and pass it that valid Context, but you should pretty much never create an Activity by calling the constructor directly. There's a lot of other initialization necessary to make it useful.
As postet previously there is more to initiate for an activity than calling the constructor. Probably you get a null pointer exception deep within the Intent Constructer where it is trying to get some of the Activityinformation usually provided.
If you really want to create a Service, heres a link for starting a Service, but you should really read the whole article and probably some more of the activity lifecycle ressources.
http://developer.android.com/guide/topics/fundamentals/services.html#StartingAService

Categories

Resources