Media player object is returning null
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?
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.
I solved the issue, alertPlayer.prepare() was causing the illegal stateexception. However, now also am not clear why is it happening like that.
精彩评论