Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add boundary time observer similar to iOS AVPlayer SDK #95

Closed
velocitysystems opened this issue Mar 14, 2018 · 6 comments
Closed

Add boundary time observer similar to iOS AVPlayer SDK #95

velocitysystems opened this issue Mar 14, 2018 · 6 comments

Comments

@velocitysystems
Copy link

We love MediaPlayerExtended and use it extensively in our app!
Recently, we have a new use case where we wish to play clips or segments from full-length media files.

One of the challenges however, is detecting when the end of the clip occurs.
MediaPlayer has OnCompletionListener but this only fires at the end of full-length playback.

What is the feasibility of adding something similar to addBoundaryTimeObserver on iOS?
Apple SDK Documentation

ie. While MPE is decoding frames, you can set a time point at which you want the player to fire an event. You could then use this to listen for the end time for a video clip and respond accordingly.

@protyposis
Copy link
Owner

This is a great idea, will be added to the next version :)

@velocitysystems
Copy link
Author

Thanks Mario - that's brilliant! Along a similar line is the ability to play clips within a media file.
ie. Specify a custom start time and duration/end point.

iOS and UWP both support this with the native players, but not Android afraid unfortunately.
Will give this some further thought and perhaps suggest in a separate issue.

@protyposis
Copy link
Owner

protyposis commented Mar 16, 2018

Yes setting the start and end point is not explicitly implemented as a feature but will easily be achievable with the Cue API in the upcoming version:

// Start at the 5 second mark
final int clipStartTime = 5000;
// End at the 10 second mark
final int clipEndTime = 10000;

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        // Seek to the start time when player is ready
        mp.seekTo(clipStartTime);
        
        // Add a cue marker for the end time
        mp.addCue(clipEndTime);
        
        mp.setOnCueListener(new MediaPlayer.OnCueListener() {
            @Override
            public void onCue(MediaPlayer mp, Cue cue) {
                // Once the end time cue is reached we stop playback.
                // We don't need to use the cue object in this case because we know that we only 
                // added a single cue so when the event is triggered we can be sure that this is 
                // the end time cue.
                mp.stop();
            }
        });

      // Start playback
      mp.start();
    }
});

@protyposis
Copy link
Owner

Working with multiple cues could look like this:

Cue clipEndTimeCue = mp.addCue(clipEndTime);
Cue someOtherCue = mp.addCue(someOtherTime);

mp.setOnCueListener(new MediaPlayer.OnCueListener() {
    @Override
    public void onCue(MediaPlayer mp, Cue cue) {
        // We make use of the Cue object returned by addCue
        if (cue == clipEndTimeCue) {
            mp.stop();
        } else {
            // Do something else with other cues
            ...
        }
    }
});

Or like this:

Object clipEndTimeMarkerData = ...;

mp.addCue(clipEndTime, clipEndTimeMetadata);
mp.addCue(someOtherTime);

mp.setOnCueListener(new MediaPlayer.OnCueListener() {
    @Override
    public void onCue(MediaPlayer mp, Cue cue) {
        // We make use of the optional data carried by the Cue object
        if (cue.getData() == clipEndTimeMarkerData) {
            mp.stop();
        } else {
            // Do something else with other cues
            ...
        }
    }
});

@protyposis
Copy link
Owner

Released in v4.4.0.

@velocitysystems
Copy link
Author

@protyposis Brilliant work! We can't wait to test this out next week.
Thank you so much for implementing this so quickly. Look forward to working with the modified API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants