媒体播放器对象返回 null

发布于 2024-12-04 17:11:53 字数 573 浏览 0 评论 0原文

我正在尝试创建一个音频播放器对象,如下所示。在这里,我获得了路径的值,但是当我检查完整路径时,我得到的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春如此纠结 2024-12-11 17:11:53

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.

記憶穿過時間隧道 2024-12-11 17:11:53

我解决了这个问题,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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文