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 setSource method #153

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Add setSource method #153

wants to merge 3 commits into from

Conversation

rtritto
Copy link
Contributor

@rtritto rtritto commented Feb 18, 2025

Fix #151

This PR contains a:

  • bugfix
  • new feature
  • code refactor
  • test update
  • typo fix
  • metadata update

Note

To be tested and reviewed

Copy link

vercel bot commented Feb 18, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
vlite ✅ Ready (Inspect) Visit Preview Feb 22, 2025 10:48pm

* Set the media source
* @param source New media source URL
*/
setSource(source: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the method should be here, because the Player class is used by all providers with inheritance (Youtube, Dailymotion, HTML5, etc.) and this is a feature only for HTML5 video and audio. This is the first time this case has arisen I believe.

This must also be documented. I will check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Video.js method is setSource

Copy link
Contributor Author

@rtritto rtritto Feb 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, if this is a feature only for HTML, it can not be added but the doc should at least be updated with related use cases on how can be done.
I wrote below my use case with HTML and where I have some problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be added, but the player file is a bridge between all provider. If the method is for only one provider, it should be is the dedicated provider (html5)

Copy link
Contributor Author

@rtritto rtritto Feb 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the method is for only one provider, it should be is the dedicated provider (html5)

Agree.

Can the providers be used as modular plugin? (new feature)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a commit to move setSource method from player core to html5 provider.

@rtritto
Copy link
Contributor Author

rtritto commented Feb 19, 2025

In my test, I have the videoReference (HTMLVideoElement).
Having an initial source, to change the source, I use:

videoReference.src = 'new-src.mp4'  // prefer src method over videoReference.setAttribute('src', 'new-src.mp4'); both trigger the DOM but src method also triggers a reload of the video content
videoReference.load()  // force video reload

The issue is that the player doesn't reload time (current time and time duration) and current progress bar.

2025-02-19.012843.mp4

There is any method of player that I should call?

@yoriiis
Copy link
Member

yoriiis commented Feb 20, 2025

Have you tested with audio and video HTML5?

@@ -296,6 +296,7 @@ The player instance exposes the following methods, accessible when the player is
| `mute()` | - | - | Mute the volume |
| `unMute()` | - | - | Unmute the volume |
| `seekTo(time)` | `Number` | - | Seek to a current time in seconds |
| `setSource(source)` | `String` | - | Set the media source |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be added a mention to specify that it is only available for HTML5 video (and audio?)

You can get inspiration from similar examples in the README

https://github.com/vlitejs/vlite/blob/main/README.md?plain=1#L266-L270

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be implemented also for other non-HTML5 providers?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've check and other providers have method to dynamically change the video source. Good news! And I prefer have common use case between providers.

The change can finally be in Player class but the same method needs to work with all providers. You can inspired from other method for the logic. Their method seems to not be a Promise.

The commun parameter seems to be videoId which contains the new video ID.

Example of implementation:

diff --git a/README.md b/README.md
index 669135a..aa3497d 100644
--- a/README.md
+++ b/README.md
@@ -295,6 +295,7 @@ The player instance exposes the following methods, accessible when the player is
 | `getDuration()`        |         -          | `Promise` | Get the duration                  |
 | `mute()`               |         -          |     -     | Mute the volume                   |
 | `unMute()`             |         -          |     -     | Unmute the volume                 |
+| `setSource()`          |      `String`      |     -     | Set the new source from video ID  |
 | `seekTo(time)`         |      `Number`      |     -     | Seek to a current time in seconds |
 | `requestFullscreen()`  |         -          |     -     | Request the fullscreen            |
 | `exitFullscreen()`     |         -          |     -     | Exit the fullscreen               |
diff --git a/src/core/player.ts b/src/core/player.ts
index 82598ab..e715ff5 100644
--- a/src/core/player.ts
+++ b/src/core/player.ts
@@ -168,6 +168,13 @@ export default abstract class Player {
 	 */
 	public abstract methodUnMute(): void
 
+	/**
+	 * methodSetSource
+	 * Extends by the provider
+	 * @abstract
+	 */
+	public abstract methodSetSource(videoId: string): void
+
 	/**
 	 * On the player is ready
 	 */
@@ -455,6 +462,14 @@ export default abstract class Player {
 		this.dispatchEvent('volumechange')
 	}
 
+	/**
+	 * Set the new source of the player
+	 * @param videoId Video ID
+	 */
+	setSource(videoId: string) {
+		this.methodSetSource(videoId)
+	}
+
 	/**
 	 * Update the current time of the media element
 	 * @param newTime New current time of the media element
diff --git a/src/providers/vimeo/vimeo.ts b/src/providers/vimeo/vimeo.ts
index ba59476..8c2ea7d 100644
--- a/src/providers/vimeo/vimeo.ts
+++ b/src/providers/vimeo/vimeo.ts
@@ -147,6 +147,10 @@ export default function VimeoProvider(Player: any) {
 			})
 		}
 
+		methodSetSource(id: string) {
+			this.instance.loadVideo(id)
+		}
+
 		/**
 		 * Play method of the player
 		 */

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

videoId parameter name is okay for Youtube, Vimeo and Dailymotion but not for HTML5 which have a video url. Needs to adapt

@rtritto rtritto marked this pull request as draft February 22, 2025 00:27
@rtritto
Copy link
Contributor Author

rtritto commented Feb 22, 2025

Have you tested with audio and video HTML5?

Yes, the change works also with audio: the source is updated but the player doesn't reload time (current time and time duration) and current progress bar:

2025-02-22.233921.mp4

@yoriiis
Copy link
Member

yoriiis commented Feb 23, 2025

the source is updated but the player doesn't reload time (current time and time duration) and current progress bar:

The action to be taken after loading the new media is not so simple. The player needs to be “reset”

  • Time updates
  • Progress bar reset
  • Poster and big play button displayed
  • Plugin sync (for example subtitle needs to be hide/reload) etc.

@rtritto
Copy link
Contributor Author

rtritto commented Feb 23, 2025

The reset logic of the player should be added as event every time the source is changed by src attribute or by setSource method

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

Successfully merging this pull request may close these issues.

Method to set source
2 participants