Reputation: 2445
I am trying to create an audio player object as shown below. Here I get the value for path, but when I inspect for the full, I am getting null, i.e., alertPlayer is returning null for me.
Here is my code,
public class Audio extends Activity {
private static MediaPlayer alertPlayer;
private static void AudioAlert(String alertPath) {
alertPlayer = MediaPlayer.create(Audio.this, Uri.fromfile(new File(path)));
alertPlayer.prepare();
alertPlayer.start();
alertPlayer.stop();
}
}
I am getting the value for this, Uri.fromfile(new File(path)) as it is expected, but MediaPlayer.create(Audio.this, Uri.fromfile(new File(path))); return null.
Any answer for this?
Upvotes: 0
Views: 1917
Reputation:
MediaPlayer is null - it's as simple as that. You've described the type, so you have a null, uninstantiated "reference" to an instance of that type, which you then use to invoke the create method. You need to create an instance of MediaPlayer before you can use it.
Upvotes: 1
Reputation: 2445
I solved the issue, alertPlayer.prepare() was causing the illegal stateexception. However, now also am not clear why is it happening like that.
Upvotes: 0