MediaPlayer的seekTo不一致从播放歌曲开始 [英] MediaPlayer seekTo inconsistently plays songs from beginning

查看:972
本文介绍了MediaPlayer的seekTo不一致从播放歌曲开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Android的MediaPlayer的班,开始从一些偏移播放歌曲。指定在此之后在code偏移,我播放歌曲期望从偏移位置听到它,而是我听到它从一开始就玩。请注意以下code:

I'm using Android's MediaPlayer class to start playing a song from some offset. After specifying this offset in code, I play the song expecting to hear it from that offset position but rather I hear it play from the beginning. Please note the following code:

    MediaPlayer mplayer = new MediaPlayer();
    mplayer.reset();
    try {
        // For example's sake, specify some known song path
        mplayer.setDataSource("//media/external/audio/media/1");
        // call blocking prepare()
        mplayer.prepare();
    } catch (Exception e) {
    // Keep try/catch simple for example's sake
        e.printStackTrace();
        return;
    }

    // seekTo is an asynchronous operation. Set it's complete callback to play the song once seekTo has completed
    mplayer.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
        @Override
        public void onSeekComplete(MediaPlayer mp) {
            // Start the song 30 seconds in
            mp.start();
        }
    });
    // Seek to 30 seconds into the song
    mplayer.seekTo(30000);

这code样品运行,而无需在模拟器中的错误(针对Android 1.6的,2.1的测试,和2.2);但是,当一些手机上运行,​​这首歌曲将从头开始播放。我知道肯定发生这种情况在我的Droid难以置信(Android 2.2的)。还要注意的是LogCat中总是有从MediaPlayer的类,说明这首歌的位置,一些输出确实已被设置为某个抵消。我已经调试这几个小时,冲刷论坛和仍然没有解决。请帮助。

This code sample runs without errors in the emulator (tested against Android 1.6, 2.1, and 2.2); however, when run on some handsets, the song will play from the beginning. I know for sure this happens on my Droid Incredible (android 2.2). Also note that LogCat will always have some output from the MediaPlayer class stating the song's position has indeed been set to some offset. I've debugged this for hours and scoured forums and still have no solution. Please help.

推荐答案

如果你现在还没有发现它在你自己,这是一个缓冲的问题(的问题记录在这里)。寻求一个非缓冲位置的结果在从开头开始播放,而不是。提出的解决方案是<一个href=\"http://stackoverflow.com/questions/3212688/mediaplayer-seekto-does-not-work-for-unbuffered-position\">found这里(不是最优雅,但对所有有做)。

In case you haven't found it on your own by now, this is a buffering problem (issue documented here). Seeking to a non-buffered position results in playing from the beginning instead. The proposed solutions are found here (not the most elegant, but about all there is to do).

这篇关于MediaPlayer的seekTo不一致从播放歌曲开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆