Android 2.2 切断MediaPlayer声音播放?

发布于 2024-11-30 03:19:16 字数 1440 浏览 1 评论 0原文

经过一番谷歌搜索后,这似乎是一个常见问题,但我尚未找到实际的解决方案。我还没有在实际设备上进行过测试,但模拟器在完成大约 80% 时就会切断我的声音剪辑。我正在播放 .wav 文件。

有谁知道这些问题的编程解决方案?

编辑:

    public void play(Context context){
    if (soundPlayer != null){
        soundPlayer.release();
    }

    int rId = 0;

    switch(aIndex){
        case 0: rId = R.raw.c0; break;
        case 1: rId = R.raw.c1; break;
        case 2: rId = R.raw.c2; break;
        case 3: rId = R.raw.c3; break;
        case 4: rId = R.raw.c4; break;
        case 5: rId = R.raw.c5; break;
        case 6: rId = R.raw.c6; break;
        case 7: rId = R.raw.c7; break;
        case 8: rId = R.raw.c8; break;
        case 9: rId = R.raw.c9; break;
        case 10: rId = R.raw.c10; break;
        case 11: rId = R.raw.c11; break;
        case 12: rId = R.raw.c12; break;
        case 13: rId = R.raw.c13; break;
        case 14: rId = R.raw.v14; break;
        case 15: rId = R.raw.v15; break;
        case 16: rId = R.raw.v16; break;
        case 17: rId = R.raw.v17; break;
        case 18: rId = R.raw.v18; break;
        case 19: rId = R.raw.v19; break;
        case 20: rId = R.raw.v20; break;
        case 21: rId = R.raw.v21; break;
        case 22: rId = R.raw.v22; break;
        case 23: rId = R.raw.v23; break;

        default: rId = R.raw.error; break;
    }

    soundPlayer = MediaPlayer.create(context, rId);
    if (soundPlayer != null){
        soundPlayer.start();
    }
}

After some Googling it appears this is a common problem but I have yet to find an actual solution. I haven't tested on an actual device, but the emulator is cutting off my sound clips at around 80% done. I am playing back .wav files.

Does anyone know a programmatic solution to these problems?

edit:

    public void play(Context context){
    if (soundPlayer != null){
        soundPlayer.release();
    }

    int rId = 0;

    switch(aIndex){
        case 0: rId = R.raw.c0; break;
        case 1: rId = R.raw.c1; break;
        case 2: rId = R.raw.c2; break;
        case 3: rId = R.raw.c3; break;
        case 4: rId = R.raw.c4; break;
        case 5: rId = R.raw.c5; break;
        case 6: rId = R.raw.c6; break;
        case 7: rId = R.raw.c7; break;
        case 8: rId = R.raw.c8; break;
        case 9: rId = R.raw.c9; break;
        case 10: rId = R.raw.c10; break;
        case 11: rId = R.raw.c11; break;
        case 12: rId = R.raw.c12; break;
        case 13: rId = R.raw.c13; break;
        case 14: rId = R.raw.v14; break;
        case 15: rId = R.raw.v15; break;
        case 16: rId = R.raw.v16; break;
        case 17: rId = R.raw.v17; break;
        case 18: rId = R.raw.v18; break;
        case 19: rId = R.raw.v19; break;
        case 20: rId = R.raw.v20; break;
        case 21: rId = R.raw.v21; break;
        case 22: rId = R.raw.v22; break;
        case 23: rId = R.raw.v23; break;

        default: rId = R.raw.error; break;
    }

    soundPlayer = MediaPlayer.create(context, rId);
    if (soundPlayer != null){
        soundPlayer.start();
    }
}

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

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

发布评论

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

评论(4

满栀 2024-12-07 03:19:16

遇到同样的问题后才发现这个问题。在我的例子中,简单的答案是使 MediaPlayer 成为一个类变量。如果您将其设置为本地方法,垃圾收集器显然会将其清除并终止声音播放。

Just discovered this question after having the same problem. The simple answer in my case was to make MediaPlayer a class variable. If you make it local to your method the garbage collector apparently sweeps it up and kills the sound play.

攒一口袋星星 2024-12-07 03:19:16

对于初学者来说,我会尝试确保在 start() 之前调用 MediaPlayer.create() 函数。如果是游戏,则在开始新游戏时加载声音(为每个声音创建一个新的媒体播放器)。原因是 create 函数将有效地加载声音文件,然后在您调用 start 时准备好顺利播放。如果您每次在运行文件之前都加载该文件,那么您不仅会做超出需要的工作,而且可能会对模拟器产生不良影响。我不确定您是否注意到,与几乎任何实际的物理设备相比,模拟器并不是最快捷的工具。结果,我认为可能发生的情况是模拟器正在“播放”声音,并在声音实际播放之前认为它已经完成,这主要是由于模拟器的速度较慢。

在实际设备上尝试一下,我想您不会有任何问题。

Well for starters, I would try to make sure MediaPlayer.create() function is called prior to the start(). If it's a game, load the sounds when they start a new game (creating a new media player for each sound). The reason why is because the create function will effectively load the sound file there and then, ready for smooth playback when you call start. If you load the file every time before running it, not only are you doing more work than you need to, but it may have undesired effects on the emulator. I'm not sure if you've noticed, but the emulator isn't exact the quickest tool out the shed compared to virtually any actual physical device. As a result, what I think may be happening is the emulator is 'playing' the sound and thinks that it is complete before the sound actually plays, mainly due to the emulator's slow speed.

Try it out on an actual device and I think you won't have any problems.

纵性 2024-12-07 03:19:16

我认为你需要在开始之前发布......示例

    if( mPlayer!= null ) mPlayer.release();

     mPlayer = MediaPlayer.create(this, listaMP3[contador]);
     mPlayer.start();             

I think you need to do a release before to start ... example

    if( mPlayer!= null ) mPlayer.release();

     mPlayer = MediaPlayer.create(this, listaMP3[contador]);
     mPlayer.start();             
日暮斜阳 2024-12-07 03:19:16

出于某种原因,如果我在用于播放歌曲的方法之外定义 mediaPlayer,它就会起作用。

尽管我只调用 playSong() 方法一次;

  mediaPlayer = MediaPlayer.create(mContext, R.raw.overworld);
        mediaPlayer.setVolume(musicVolume, musicVolume);

  playSong();

播放歌曲方法:

   private void playSong() {

    if (!mediaPlayer.isPlaying())
                mediaPlayer.start(); // no need to call prepare(); create() does that for you
} 

for soem reason if I define the mediaPlayer outside of the method I use to play a song, it works.

Even though I only call the playSong() method once;

  mediaPlayer = MediaPlayer.create(mContext, R.raw.overworld);
        mediaPlayer.setVolume(musicVolume, musicVolume);

  playSong();

playSong Method:

   private void playSong() {

    if (!mediaPlayer.isPlaying())
                mediaPlayer.start(); // no need to call prepare(); create() does that for you
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文