A simple library for parsing and playing links from YouTube, YouTube Music, Vimeo and Rutube and others in the WebView without the need to connect data API services.
Add it in your root build.gradle at the end of repositories:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } }
Add the dependency
dependencies { implementation 'com.github.TalbotGooday:Android-Oembed-Video:Tag' }
- Create your OkHttpClient and add it to the VideoService.Builder
val okHttpClient = OkHttpClient.Builder() .connectTimeout(15, TimeUnit.SECONDS) .readTimeout(15, TimeUnit.SECONDS) .build() val videoService = VideoService.build{ with(this@MainActivity) httpClient(okHttpClient) enableCache(true) enableLog(true) }
- Get VideoPreviewModel
videoService.loadVideoPreview( url, onSuccess = { video -> //handle a video model }, onError = { url, error -> //handle an error })
- Enable/disable caching
val videoService = VideoService.build { enableCache(true) }
- Enable/disable logging
val videoService = VideoService.build { enableLog(BuildConfig.DEBUG) }
The BottomVideoController allows to run any oembed video in WebView.
val host = model.videoHosting val linkToPlay = model.linkToPlay val title = model.videoTitle val initUrl = model.url BottomVideoController.build(this) { setListener(object : BottomVideoController.Listener() { override fun openLinkIn(link: String) { openLink(link) } override fun copyLink(link: String) { copyLinkToClipboard(link) } }) setHostText(host) setPlayLink(linkToPlay) setSize(model.width, model.height) setTitle(title) setVideoUrl(initUrl) setProgressView(TextView(this@MainActivity).apply { text = "Loading" }) show() }
- Add the
Gsonlibrary to your project - Create the
Gsondata class from the embed response of the video service. Make this class a subclass ofVideoInfoModel, implement thetoPreviewfunction, and override it:
override fun toPreview(url: String?, linkToPlay: String, hostingName: String, videoId: String): VideoPreviewModel { return VideoPreviewModel(url, linkToPlay, hostingName, videoId).apply { this.thumbnailUrl = this@UltimediaResponse.thumbnailUrl this.videoTitle = this@UltimediaResponse.authorName this.width = this@UltimediaResponse.width.toInt() this.height = this@UltimediaResponse.height.toInt() } }
- Create a subclass of
VideoInfoModel, implement members and override them:
class UltimediaVideoInfoModel: VideoInfoModel<UltimediaResponse>() { override val baseUrl: String get() = "https://www.ultimedia.com" //https://regex101.com/r/2AsrOc/1 override val pattern: String get() = "(?:http[s]?:\\/\\/)?(?:www)?\\.?ultimedia\\.com\\/(?:deliver|default|api)\\/.*\\/([_a-zA-Z0-9]+)\\S*" override val idPattern: String get() = pattern //or some another video id search pattern override val type: Class<UltimediaResponse> get() = UltimediaResponse::class.java override val hostingName: String get() = "Ultimedia" override fun getInfoUrl(incomingUrl: String?): String? { return "$baseUrl/api/search/oembed?$FORMAT=$FORMAT_JSON&$URL=$incomingUrl" } override fun getPlayLink(videoId: String): String { return "https://www.ultimedia.com/deliver/generic/iframe/src/$videoId/" } }
Note: By default, the index of the Regex group should be 1. If your idPattern does not fulfill this condition, then override the parseVideoId method:
override fun parseVideoId(url: String?): String? { url ?: return null return idPattern.toRegex().find(url)?.groups?.get(**someIndex**)?.value }
This project is licensed under the Apache License 2.0 - see the LICENSE file for details