Get the start position before a seek on Youtube android player - android

I want to get the start and end position in Millisecond after à seek.
Actually, I get:
mYouTubePlayer.setPlaybackEventListener(new YouTubePlayer.PlaybackEventListener() {
#Override
public void onSeekTo(int endPositionMillis) {
Log.i("SEEK CURRENT MILLIS", String.valueOf(mYouTubePlayer.getCurrentTimeMillis()));
Log.i("SEEK ENDPOS MILLIS", String.valueOf(endPositionMillis));
}
}
The problem is when the user move the cursor from the timeline, an event onSeekTo is launch, and when I'm inside my onSeekTo methode, I only got the endPositionMillis, but there is no way to get the timer/position before the seek.

If you are referring to the endTime of the video, use the getDurationMillis(). For getting the current time use getCurrentTimeMillis() which you are already using then to go to a specific time of the video use either seekRelativeMillis() or seekToMillis(). With regard to your video not pausing before seeking to a specified time, it will still depend on your implementation if you want to pause the video before going to seeking to that time then playing it.
You can also check this related SO post (using web).
Hope this helps.

Related

Play single track spotify sdk

I am using the Spotify android SDK, and I am trying to get a single song to play, and I would like to potentially play a song after the current one is completed. The issue is that after the song completes Spotify plays a random song afterwards. Is it possible to play the song and not have anything else automatically play after it?
I am simply calling the app remotes player API play function,
mSpotifyAppRemote?.getPlayerApi()?.play(uri)
#Mikee you are doing nothing wrong, Spotify messes around with track replay if you are only using the Free version, if you use Premium it will work correctly. Funny enough if you try playing by an alblum or artist that works with the Free version.
I'm not an expert but from their documentation it looks like you could watch the PlayerState. I'm also not sure when a PlayerState event would trigger but if it's coming back relatively often you could check the track value and see if it's gone to null, or another value and work using that.
Here's a Java example from their website:
// Subscribe to PlayerState
mSpotifyAppRemote.getPlayerApi()
.subscribeToPlayerState()
.setEventCallback(new Subscription.EventCallback<PlayerState>() {
public void onEvent(PlayerState playerState) {
// See what values are in playerState, might be able to determine
// if it's now randomly playing?
final Track track = playerState.track;
if (track != null) {
Log.d("MainActivity", track.name + " by " + track.artist.name);
// If the track is now different, your song has finished, stop it?
}
}
});
I've put a few extra comments in the code above that might yield some results!

How to properly track playback position? [Android Media Broadcast Notifications Spotify API]

After reading the documentation on Spotify's Android Media Notifications API, https://beta.developer.spotify.com/documentation/android-sdk/guides/android-media-notifications/, I successfully managed to receive the notifications metadata and it is displayed properly on my app.
However, the notifications metadata is only updated when the queue changes, when the track changes, and when playback is changed, so unless one of these three actions happens, the "positionInMs" intent extra isn't sent.
As of right now as a workaround I am simply starting a timer using the time the intent was sent, the last known playback position, and the track duration to track current playback position.
This seemed to work at first, but after further testing I've realized that the timer I set can go out of sync, if the track the user is listening to freezes because of a slow internet connection.
Any ideas to properly track the playback position, while accounting for a slow internet connection? Or are there any alternatives I should look into?
I understand that this question is rather old, but I am going to answer anyway if anyone else comes across it.
I recommend constantly querying Spotify to get the playback position. One way you can do this is by using a timer and querying Spotify every given time frame. The below example queries Spotify every 100ms. If you want to reduce/increase the numbers of queries, you can simply use stopwatch.setClockDelay() and provide your required time
For instance, you can use this timer library
implementation 'com.yashovardhan99.timeit:timeit:1.2.0'
Then use the following code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spotify);
Stopwatch stopwatch = new Stopwatch();
stopwatch.setOnTickListener(this);
stopwatch.start();
}
#Override
public void onTick(Stopwatch stopwatch) {
Data.getAndroidSpotifyAppRemote().getPlayerApi().getPlayerState().setResultCallback(new CallResult.ResultCallback<PlayerState>() {
#Override
public void onResult(PlayerState playerState) {
Log.d("TAG", playerState.playbackPosition);
}
});
}
Don't forget to add the following code at the top of your class:
implements Stopwatch.OnTickListener

How to determine media completion in VideoCastControllerActivity?

In my app, I am using the CastCompanionLibrary to cast videos to a chromecast device. I want to perform some action when the video has finished playing. How do I determine as to when the video has completed playing? I did not find any suitable callback for this.
Register a VideoCastConsumer that implements onRemoteMediaPlayerStatusUpdated(). This will inform you when the playback status changes.
In that callback, get the mediaStatus = mCastManager.getMediaStatus(); this will give you the updated status.
If mediaStatus == MediaStatus.PLAYER_STATE_IDLE and mCastManager.getIdleReason() == MediaStatus.IDLE_REASON_FINISHED, then it means the currently playing item is just finished.
Note that a version of this is already being done in the associated fragment so please tell us what you are doing exactly; what I suggested allows you to generically find out when the playback of a single media items reaches its end.

Android Brightcove onVideo does not get called sometimes and hence video does not play

I have integrated Brightcove android-sdk-4.2.7 and trying to play video using videoID.
Here is the code snippet :
Catalog catalog = new Catalog(<**MEDIA_READ_TOKEN**>);
catalog.findVideoByID(videoId, new VideoListener() {
#Override
public void onError(String error) {
Log.e(TAG, "onError : " + error);
}
#Override
public void onVideo(Video video) {
Log.i(TAG, "onVideo called" );
brightcoveVideoView.add(video);
brightcoveVideoView.start();
}
});
When I launch video_player_activity from other activity for the first time video plays perfectly fine, but when I go back to second activity and again try to play the same/other video, It does not play. onVideo does not get called. Sometimes it gets called after a long time.
Following are some more details :
activity is getting destroyed properly when I go back
onError is not getting called
video id is getting set properly
Tried emitter events and READY_TO_PLAY event occurs but DID_PLAY does not
Android version I tested on is 4.4.x
What might be the possible issues? Any help/pointer around this will be really appreciated.
Thanks!
Repeated calls to findVideoByID(), should work fine. For each one, you should see a request in the logcat, something like:
D/HttpService(27297): issuing GET request: http://api.brightcove.com/services/library?command=find_video_by_id&video_id=3603631382001...
and a response, something like:
D/HttpService(27297): response: {"id":36036...
If that is not the case, please file a bug with Brightcove Support. If that is working, there isn't enough information here to debug further. You could try to compare what you are doing with the public sample apps available on Github:
https://github.com/BrightcoveOS/android-player-samples
These apps don't have multiple activities like your scenario, but they do handle the app going into the background and then being brought back to the foreground. Extending the BrightcovePlayer or BrightcovePlayerFragment enables the lifecycle handling. If you don't extend one of those classes, you have to write your own lifecycle handling, as if you were using the Android VideoView. For example, calling pause() in onPause() and calling stopPlayback() in onStop().

Android Chromecast: Call messageStream.playFrom(position) does not work in MediaProtocolCommand onCompleted

I had a media player, when user connect to chromecast in the middle of the video, I would like to cast the video to chromecast at the same position in the video where the user left off.
mediaProtocolCommand.setListener(new MediaProtocolCommand.Listener() {
public void onCompleted(MediaProtocolCommand mPCommand) {
messageStream.playFrom(currentVideoPosition);
}
});
Using the code snippet above I manage to get the video playing but it always start at the beginning of the video
Works fine for me using the Cast Android sample app. Make sure the parameter value for playFrom is the time in seconds.

Categories

Resources