媒体播放器对象返回 null
我正在尝试创建一个音频播放器对象,如下所示。在这里,我获得了路径的值,但是当我检查完整路径时,我得到的是 null,即,alertPlayer 为我返回了 null。
这是我的代码,
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();
}
}
我得到了这个值, Uri.fromfile(new File(path)) 正如预期的那样,但是 MediaPlayer.create(Audio.this, Uri.fromfile(new File(path)));返回空值。
对此有什么答案吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MediaPlayer 为 null - 就这么简单。您已经描述了该类型,因此对该类型的实例有一个空的、未实例化的“引用”,然后您可以使用该引用来调用 create 方法。您需要先创建 MediaPlayer 的实例,然后才能使用它。
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.
我解决了这个问题,alertPlayer.prepare() 导致了非法的状态异常。不过,现在还不清楚为什么会这样。
I solved the issue, alertPlayer.prepare() was causing the illegal stateexception. However, now also am not clear why is it happening like that.