@Test @LargeTest public void testGetTimestamp() throws Exception { final int toleranceUs = 100000; final float playbackRate = 1.0f; if (!checkLoadResource( R.raw.video_480x360_mp4_h264_1000kbps_30fps_aac_stereo_128kbps_44100hz)) { return; } final Monitor onPauseCalled = new Monitor(); MediaPlayer2.MediaPlayer2EventCallback ecb = new MediaPlayer2.MediaPlayer2EventCallback() { @Override public void onInfo(MediaPlayer2 mp, DataSourceDesc dsd, int what, int extra) { if (what == MediaPlayer2.MEDIA_INFO_PREPARED) { mOnPrepareCalled.signal(); } } @Override public void onCallCompleted(MediaPlayer2 mp, DataSourceDesc dsd, int what, int status) { if (what == MediaPlayer2.CALL_COMPLETED_PAUSE) { onPauseCalled.signal(); } } }; synchronized (mEventCbLock) { mEventCallbacks.add(ecb); } mPlayer.setSurface(mActivity.getSurfaceHolder().getSurface()); mOnPrepareCalled.reset(); mPlayer.prepare(); mOnPrepareCalled.waitForSignal(); mPlayer.play(); mPlayer.setPlaybackParams(new PlaybackParams().setSpeed(playbackRate)); Thread.sleep(SLEEP_TIME); long nt1 = System.nanoTime(); MediaTimestamp ts1 = mPlayer.getTimestamp(); long nt2 = System.nanoTime(); assertTrue("Media player should return a valid time stamp", ts1 != null); assertEquals("MediaPlayer2 had error in clockRate " + ts1.getMediaClockRate(), playbackRate, ts1.getMediaClockRate(), 0.001f); assertTrue("The nanoTime of Media timestamp should be taken when getTimestamp is called.", nt1 <= ts1.getAnchorSytemNanoTime() && ts1.getAnchorSytemNanoTime() <= nt2); onPauseCalled.reset(); mPlayer.pause(); onPauseCalled.waitForSignal(); ts1 = mPlayer.getTimestamp(); assertTrue("Media player should return a valid time stamp", ts1 != null); assertTrue("Media player should have play rate of 0.0f when paused", ts1.getMediaClockRate() == 0.0f); mPlayer.seekTo(0, MediaPlayer2.SEEK_PREVIOUS_SYNC); mPlayer.play(); Thread.sleep(SLEEP_TIME); int playTime = 4000; ts1 = mPlayer.getTimestamp(); assertTrue("Media player should return a valid time stamp", ts1 != null); Thread.sleep(playTime); MediaTimestamp ts2 = mPlayer.getTimestamp(); assertTrue("Media player should return a valid time stamp", ts2 != null); assertTrue("The clockRate should not be changed.", ts1.getMediaClockRate() == ts2.getMediaClockRate()); assertEquals("MediaPlayer2 had error in timestamp.", ts1.getAnchorMediaTimeUs() + (long) (playTime * ts1.getMediaClockRate() * 1000), ts2.getAnchorMediaTimeUs(), toleranceUs); mPlayer.reset(); }
@Nullable public abstract MediaTimestamp getTimestamp();
MediaPlayer2 { @Nullable public abstract MediaTimestamp getTimestamp(); }
MediaPlayer2 { @Nullable public abstract MediaTimestamp getTimestamp(); @RestrictTo(LIBRARY_GROUP)  MediaPlayer2(); }
MediaPlayer2 { @Nullable public abstract MediaTimestamp getTimestamp(); @RestrictTo(LIBRARY_GROUP)  MediaPlayer2(); static final MediaPlayer2 create(); abstract MediaPlayerInterface getMediaPlayerInterface(); abstract void close(); abstract void play(); abstract void prepare(); abstract void pause(); abstract void skipToNext(); void seekTo(long msec); abstract long getCurrentPosition(); abstract long getDuration(); abstract long getBufferedPosition(); abstract @MediaPlayer2State int getMediaPlayer2State(); abstract void setAudioAttributes(@NonNull AudioAttributesCompat attributes); abstract @Nullable AudioAttributesCompat getAudioAttributes(); abstract void setDataSource(@NonNull DataSourceDesc dsd); abstract void setNextDataSource(@NonNull DataSourceDesc dsd); abstract void setNextDataSources(@NonNull List<DataSourceDesc> dsds); abstract @NonNull DataSourceDesc getCurrentDataSource(); abstract void loopCurrent(boolean loop); abstract void setPlaybackSpeed(float speed); float getPlaybackSpeed(); boolean isReversePlaybackSupported(); abstract void setPlayerVolume(float volume); abstract float getPlayerVolume(); float getMaxPlayerVolume(); void notifyWhenCommandLabelReached(@NonNull Object label); abstract void setSurface(Surface surface); abstract void clearPendingCommands(); abstract int getVideoWidth(); abstract int getVideoHeight(); abstract PersistableBundle getMetrics(); abstract void setPlaybackParams(@NonNull PlaybackParams params); @NonNull abstract PlaybackParams getPlaybackParams(); abstract void setSyncParams(@NonNull SyncParams params); @NonNull abstract SyncParams getSyncParams(); abstract void seekTo(long msec, @SeekMode int mode); @Nullable abstract MediaTimestamp getTimestamp(); abstract void reset(); abstract void setAudioSessionId(int sessionId); abstract int getAudioSessionId(); abstract void attachAuxEffect(int effectId); abstract void setAuxEffectSendLevel(float level); abstract List<TrackInfo> getTrackInfo(); abstract int getSelectedTrack(int trackType); abstract void selectTrack(int index); abstract void deselectTrack(int index); abstract void setMediaPlayer2EventCallback(
            @NonNull Executor executor, @NonNull MediaPlayer2EventCallback eventCallback); abstract void clearMediaPlayer2EventCallback(); abstract void setOnDrmConfigHelper(OnDrmConfigHelper listener); abstract void setDrmEventCallback(@NonNull Executor executor,
                                             @NonNull DrmEventCallback eventCallback); abstract void clearDrmEventCallback(); abstract DrmInfo getDrmInfo(); abstract void prepareDrm(@NonNull UUID uuid); abstract void releaseDrm(); @NonNull abstract MediaDrm.KeyRequest getDrmKeyRequest(
            @Nullable byte[] keySetId, @Nullable byte[] initData,
            @Nullable String mimeType, int keyType,
            @Nullable Map<String, String> optionalParameters); abstract byte[] provideDrmKeyResponse(
            @Nullable byte[] keySetId, @NonNull byte[] response); abstract void restoreDrmKeys(@NonNull byte[] keySetId); @NonNull abstract String getDrmPropertyString(
            @NonNull String propertyName); abstract void setDrmPropertyString(
            @NonNull String propertyName, @NonNull String value); }
MediaPlayer2 { @Nullable public abstract MediaTimestamp getTimestamp(); @RestrictTo(LIBRARY_GROUP)  MediaPlayer2(); static final MediaPlayer2 create(); abstract MediaPlayerInterface getMediaPlayerInterface(); abstract void close(); abstract void play(); abstract void prepare(); abstract void pause(); abstract void skipToNext(); void seekTo(long msec); abstract long getCurrentPosition(); abstract long getDuration(); abstract long getBufferedPosition(); abstract @MediaPlayer2State int getMediaPlayer2State(); abstract void setAudioAttributes(@NonNull AudioAttributesCompat attributes); abstract @Nullable AudioAttributesCompat getAudioAttributes(); abstract void setDataSource(@NonNull DataSourceDesc dsd); abstract void setNextDataSource(@NonNull DataSourceDesc dsd); abstract void setNextDataSources(@NonNull List<DataSourceDesc> dsds); abstract @NonNull DataSourceDesc getCurrentDataSource(); abstract void loopCurrent(boolean loop); abstract void setPlaybackSpeed(float speed); float getPlaybackSpeed(); boolean isReversePlaybackSupported(); abstract void setPlayerVolume(float volume); abstract float getPlayerVolume(); float getMaxPlayerVolume(); void notifyWhenCommandLabelReached(@NonNull Object label); abstract void setSurface(Surface surface); abstract void clearPendingCommands(); abstract int getVideoWidth(); abstract int getVideoHeight(); abstract PersistableBundle getMetrics(); abstract void setPlaybackParams(@NonNull PlaybackParams params); @NonNull abstract PlaybackParams getPlaybackParams(); abstract void setSyncParams(@NonNull SyncParams params); @NonNull abstract SyncParams getSyncParams(); abstract void seekTo(long msec, @SeekMode int mode); @Nullable abstract MediaTimestamp getTimestamp(); abstract void reset(); abstract void setAudioSessionId(int sessionId); abstract int getAudioSessionId(); abstract void attachAuxEffect(int effectId); abstract void setAuxEffectSendLevel(float level); abstract List<TrackInfo> getTrackInfo(); abstract int getSelectedTrack(int trackType); abstract void selectTrack(int index); abstract void deselectTrack(int index); abstract void setMediaPlayer2EventCallback(
            @NonNull Executor executor, @NonNull MediaPlayer2EventCallback eventCallback); abstract void clearMediaPlayer2EventCallback(); abstract void setOnDrmConfigHelper(OnDrmConfigHelper listener); abstract void setDrmEventCallback(@NonNull Executor executor,
                                             @NonNull DrmEventCallback eventCallback); abstract void clearDrmEventCallback(); abstract DrmInfo getDrmInfo(); abstract void prepareDrm(@NonNull UUID uuid); abstract void releaseDrm(); @NonNull abstract MediaDrm.KeyRequest getDrmKeyRequest(
            @Nullable byte[] keySetId, @Nullable byte[] initData,
            @Nullable String mimeType, int keyType,
            @Nullable Map<String, String> optionalParameters); abstract byte[] provideDrmKeyResponse(
            @Nullable byte[] keySetId, @NonNull byte[] response); abstract void restoreDrmKeys(@NonNull byte[] keySetId); @NonNull abstract String getDrmPropertyString(
            @NonNull String propertyName); abstract void setDrmPropertyString(
            @NonNull String propertyName, @NonNull String value); static final int VIDEO_SCALING_MODE_SCALE_TO_FIT; static final int SEEK_PREVIOUS_SYNC; static final int SEEK_NEXT_SYNC; static final int SEEK_CLOSEST_SYNC; static final int SEEK_CLOSEST; static final int MEDIAPLAYER2_STATE_IDLE; static final int MEDIAPLAYER2_STATE_PREPARED; static final int MEDIAPLAYER2_STATE_PAUSED; static final int MEDIAPLAYER2_STATE_PLAYING; static final int MEDIAPLAYER2_STATE_ERROR; static final int MEDIA_ERROR_UNKNOWN; static final int MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK; static final int MEDIA_ERROR_IO; static final int MEDIA_ERROR_MALFORMED; static final int MEDIA_ERROR_UNSUPPORTED; static final int MEDIA_ERROR_TIMED_OUT; @RestrictTo(LIBRARY_GROUP)
    static final int MEDIA_ERROR_SYSTEM; static final int MEDIA_INFO_UNKNOWN; @RestrictTo(LIBRARY_GROUP)
    static final int MEDIA_INFO_STARTED_AS_NEXT; static final int MEDIA_INFO_VIDEO_RENDERING_START; static final int MEDIA_INFO_AUDIO_RENDERING_START; static final int MEDIA_INFO_PLAYBACK_COMPLETE; static final int MEDIA_INFO_PLAYLIST_END; static final int MEDIA_INFO_PREPARED; static final int MEDIA_INFO_VIDEO_TRACK_LAGGING; static final int MEDIA_INFO_BUFFERING_START; static final int MEDIA_INFO_BUFFERING_END; @RestrictTo(LIBRARY_GROUP)
    static final int MEDIA_INFO_NETWORK_BANDWIDTH; static final int MEDIA_INFO_BUFFERING_UPDATE; static final int MEDIA_INFO_BAD_INTERLEAVING; static final int MEDIA_INFO_NOT_SEEKABLE; static final int MEDIA_INFO_METADATA_UPDATE; @RestrictTo(LIBRARY_GROUP)
    static final int MEDIA_INFO_EXTERNAL_METADATA_UPDATE; static final int MEDIA_INFO_AUDIO_NOT_PLAYING; static final int MEDIA_INFO_VIDEO_NOT_PLAYING; @RestrictTo(LIBRARY_GROUP)
    static final int MEDIA_INFO_TIMED_TEXT_ERROR; static final int MEDIA_INFO_UNSUPPORTED_SUBTITLE; static final int MEDIA_INFO_SUBTITLE_TIMED_OUT; static final int CALL_COMPLETED_ATTACH_AUX_EFFECT; static final int CALL_COMPLETED_DESELECT_TRACK; static final int CALL_COMPLETED_LOOP_CURRENT; static final int CALL_COMPLETED_PAUSE; static final int CALL_COMPLETED_PLAY; static final int CALL_COMPLETED_PREPARE; static final int CALL_COMPLETED_SEEK_TO; static final int CALL_COMPLETED_SELECT_TRACK; static final int CALL_COMPLETED_SET_AUDIO_ATTRIBUTES; static final int CALL_COMPLETED_SET_AUDIO_SESSION_ID; static final int CALL_COMPLETED_SET_AUX_EFFECT_SEND_LEVEL; static final int CALL_COMPLETED_SET_DATA_SOURCE; static final int CALL_COMPLETED_SET_NEXT_DATA_SOURCE; static final int CALL_COMPLETED_SET_NEXT_DATA_SOURCES; static final int CALL_COMPLETED_SET_PLAYBACK_PARAMS; static final int CALL_COMPLETED_SET_PLAYBACK_SPEED; static final int CALL_COMPLETED_SET_PLAYER_VOLUME; static final int CALL_COMPLETED_SET_SURFACE; static final int CALL_COMPLETED_SET_SYNC_PARAMS; static final int CALL_COMPLETED_SKIP_TO_NEXT; @RestrictTo(LIBRARY_GROUP)
    static final int CALL_COMPLETED_NOTIFY_WHEN_COMMAND_LABEL_REACHED; static final int CALL_STATUS_NO_ERROR; static final int CALL_STATUS_ERROR_UNKNOWN; static final int CALL_STATUS_INVALID_OPERATION; static final int CALL_STATUS_BAD_VALUE; static final int CALL_STATUS_PERMISSION_DENIED; static final int CALL_STATUS_ERROR_IO; static final int PREPARE_DRM_STATUS_SUCCESS; static final int PREPARE_DRM_STATUS_PROVISIONING_NETWORK_ERROR; static final int PREPARE_DRM_STATUS_PROVISIONING_SERVER_ERROR; static final int PREPARE_DRM_STATUS_PREPARATION_ERROR; }