安卓媒体播放器

发布于 2024-12-22 14:42:58 字数 553 浏览 1 评论 0原文

我正在尝试使用 MediaPlayer 对象播放声音,但尽管我尽了最大努力,但似乎无法让它工作。声音只是拒绝播放。

这是一个短的声音,应该在触摸屏幕时播放,这意味着它必须重复很多次,而不会造成太多延迟。知道这一点后,我遵循了状态图, http://developer.android.com/reference /android/media/MediaPlayer.html。我似乎看不出我的方法调用顺序到底出了什么问题。

MediaPlayer mp = MediaPlayer.create(this.getContext(), R.raw.select2);
try {
    mp.prepare();
    mp.start();                 
    Log.e("debug","sound played");
    }
catch(Exception e) {}
mp.stop();

I am attempting to play a sound using a MediaPlayer object, but I cannot seem to get it to work despite my best efforts. The sound simply refuses to play.

It's a short sound, that is supposed to be played when the screen is touched, meaning it will have to be repeated many times without too much delay. Knowing this I followed the state diagram, http://developer.android.com/reference/android/media/MediaPlayer.html. I can't seem to see what exactly is wrong with my sequencing of method calls.

MediaPlayer mp = MediaPlayer.create(this.getContext(), R.raw.select2);
try {
    mp.prepare();
    mp.start();                 
    Log.e("debug","sound played");
    }
catch(Exception e) {}
mp.stop();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

会发光的星星闪亮亮i 2024-12-29 14:42:58

您在媒体播放器上调用 prepare() ,但是 create() 您使用的调用会自动准备播放器,当您尝试调用 prepare()< 时,这会导致 IllegalStateException /代码>再次,您将被发送到您的catch()(如果您以某种方式处理异常,即打印堆栈跟踪,您会注意到这一点)。

You call prepare() on the mediaplayer, but the create() call you use prepares the player automatically, this causes an IllegalStateException when you try to call prepare() again, and you are sent to your catch() (you would have noticed this if you handled the exception in some way, i.e. printing the stack trace).

草莓味的萝莉 2024-12-29 14:42:58
MediaPlayer player = MediaPlayer.create(this.getContext(), R.raw.select2);

我们通过设置音乐播放器的一些属性来配置音乐播放器,如下所示

player.setWakeMode(getApplicationContext()PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
try {
   player.prepare();
   player.start();                 
   Log.e("debug","sound played");
}  catch(Exception e) {}
  player.stop();
MediaPlayer player = MediaPlayer.create(this.getContext(), R.raw.select2);

we configure the music player by setting some of its properties as shown below

player.setWakeMode(getApplicationContext()PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
try {
   player.prepare();
   player.start();                 
   Log.e("debug","sound played");
}  catch(Exception e) {}
  player.stop();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文