Android MediaPlayer getCurrentPosition()返回错误的值

时间:2016-02-07 10:30:08

标签: android android-mediaplayer

我有一个服务,我在其中创建了一个MediaPlayer对象。然后我绑定到服务以获取有关正在播放的媒体的信息,如当前搜索位置。出于测试目的,创建了一个按钮,该按钮调用getCurrentPosition的{​​{1}}()方法。但是每次它返回一些随机值。 (我只在调用start()并播放媒体后调用MediaPlayer)。 getCurrentPosition()正在返回歌曲的正确持续时间。

getDuration
public class SongPlayService extends Service {

    private static final String SERVICE_INTENT_KEY = "com.example.strehan.galleryapp.SERVICE_INTENT_KEY";

    private SongPlayThread mSongPlayThread;
    private String mSongData;
    private SongPlayServiceHandler mHandler;
    private MediaPlayer mMediaPlayer;

    public class SongPlayServiceBinder extends Binder {

        public  SongPlayService getSongPlayService() {
            return SongPlayService.this;
        }
    }

    public void onCreate() {
        super.onCreate();
        Log.d("SongPlayService", "onCreate()");
        mHandler = new SongPlayServiceHandler();
        mSongPlayThread = new SongPlayThread("SongPlay Thread", mHandler, getApplicationContext());
        mSongPlayThread.start();
        mSongPlayThread.initHandler(mSongPlayThread.getLooper());
        mMediaPlayer = mSongPlayThread.createMediaPlayer();
    }

    public IBinder onBind(Intent intent) {
        Log.d("SongPlayService", "onBind()");
        return  new SongPlayServiceBinder();
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("SongPlayService", "onStartCommand()");
        mSongData  = intent.getStringExtra(SERVICE_INTENT_KEY);
        Log.d("SongPlayService", "Song data rcvd at service: " + mSongData);
        if (mSongData != null) {
            mSongPlayThread.resetMediaPlayer();
            mSongPlayThread.prepareSong(mSongData);
            playSong();
        }
        return START_NOT_STICKY;
    }

    public int getSeekInfo() {
        return mMediaPlayer.getCurrentPosition();
    }
}


public class SongPlayThread extends HandlerThread implements MediaPlayer.OnCompletionListener,
        MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener {


    private Handler mRequestHandler;
    private Handler mResponseHandler;
    private MediaPlayer mMediaPlayer;
    private Context mContext;

    public void initHandler(Looper looper) {

        mRequestHandler = new Handler(looper) {

            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_PREPARE_PLAYER:
                        Log.d("SongPlayThread", "Prepare song recvd: Song data:" + msg.obj);
                        Log.d("SongPlayThread", "Thread: " + Thread.currentThread().getName());
                        processPrepareMsg(msg.obj.toString());
                        break;

                    case  MSG_PLAY_SONG:
                        Log.d("SongPlayThread", "Play Song msg rcvd");
                        processPlayMsg();
                        break;
                }
            }
        };
    }

    private void processPlayMsg() {
        mMediaPlayer.start();
    }

    private void processPrepareMsg(String path) {
        try {

            Log.d("SongPlayThread", "Registering listeners: Thread: " + Thread.currentThread().getName());
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnErrorListener(this);

            Uri uri = Uri.fromFile(new File(path));
            mMediaPlayer.setDataSource(mContext, uri);
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.prepare();
        } catch (IOException ioException) {
                   ioException.printStackTrace();
        }
    }
}

0 个答案:

没有答案