안드로이드 개발 팁 #44 - 유튜브 URL로부터 플레이 시작 위치 읽는 방법steemCreated with Sketch.

유튜브 URL로부터 플레이 시작 위치 읽는 방법

No. 44
23.10.01 (일) | Written by @dorian-mobileapp

시작하며…

스팀잇 포스트의 내용을 보여주는 화면을 개발하면서 유튜브 동영상을 보여주는 기능도 구현해야 합니다. 여기서 필요한 것이 유튜브 URL, 비디오 ID 외에 플레이 시작 위치도 옵션으로 필요합니다. 이것은 동영상을 맨처음이 아닌 원하는 중간 위치에서부터 재생할 수 있음을 의미합니다. 이번 포스트에서는 유튜브 URL로부터 플레이 시작 위치를 읽는 방법에 대해 정리해 봅니다.


비디오 ID, 플레이 시작 위치를 포함한 유튜브 URL의 포맷

  • 유형 1: https://{유튜브 호스트}/embed/{비디오 ID}?start={0보다 큰 정수}
  • 유형 2: https://{유튜브 호스트}/watch?v={비디오 ID}&t={0보다 큰 정수}s
  • 유튜브 호스트: 다음 셋 중 하나
    • youtube.com
    • m.youtube.com
    • www.youtube.com

유튜브 URL로부터 플레이 시작 위치 읽는 방법

  • URL 포맷이 위 유형 1인 경우
    • URL이 "https://{유튜브 호스트}/embed/"로 시작하는지 확인
    • 쿼리 파라메터 start 읽기
  • URL 포맷이 위 유형 2인 경우
    • URL이 "https://{유튜브 호스트}/watch"로 시작하는지 확인
    • 쿼리 파라메터 t 읽기: 읽은 값의 끝에 달린 s는 제거

유튜브 URL로부터 플레이 시작 위치를 읽는 Uri 클래스의 확장 메소드

편리하게 플레이 시작 위치를 읽도록 Uri 클래스의 확장 메소드를 작성해 보았습니다. 참고로 Uri.isYoutubeSite() 메소드는 이전 포스트에서 정의하였습니다.

private const val youtubeSiteEmbedTypeA = "https://youtube.com/embed/"
private const val youtubeSiteEmbedTypeB = "https://m.youtube.com/embed/"
private const val youtubeSiteEmbedTypeC = "https://www.youtube.com/embed/"

private const val youtubeSiteWatchTypeA = "https://youtube.com/watch"
private const val youtubeSiteWatchTypeB = "https://m.youtube.com/watch"
private const val youtubeSiteWatchTypeC = "https://www.youtube.com/watch"

fun Uri.getStartTimeOfYoutubeVideo(): String {
    if (!isYoutubeSite()) {
        return ""
    }

    val addresss = this.toString()
    return when {
        addresss.startsWithOneOf(youtubeSiteEmbedTypeA, youtubeSiteEmbedTypeB, youtubeSiteEmbedTypeC) -> this.getQueryParameter("start") ?: "0"
        addresss.startsWithOneOf(youtubeSiteWatchTypeA, youtubeSiteWatchTypeB, youtubeSiteWatchTypeC) -> this.getQueryParameter("t")?.replace("s", "") ?: "0"
        else -> ""
    }
}

참고 링크


지난 안드로이드 개발 팁


Layout provided by Steemit Enhancer hommage by ayogom


Posted through the ECble app (https://ecble.etain.club)


Posted through the ECble app (https://ecble.etain.club)
Sort:  

[광고] STEEM 개발자 커뮤니티에 참여 하시면, 다양한 혜택을 받을 수 있습니다.

Thank you, friend!
I'm @steem.history, who is steem witness.
Thank you for witnessvoting for me.
image.png
please click it!
image.png
(Go to https://steemit.com/~witnesses and type fbslo at the bottom of the page)

The weight is reduced because of the lack of Voting Power. If you vote for me as a witness, you can get my little vote.

안녕하세요.
SteemitKorea팀에서 제공하는 'steemit-enhancer'를 사용해 주셔서 감사합니다. 개선 사항이 있으면 언제나 저에게 연락을 주시면 되고, 관심이 있으신 분들은 https://cafe.naver.com/steemitkorea/425 에서 받아보실 수 있습니다. 사용시 @응원해 가 포함이 되며, 악용시에는 모든 서비스에서 제외될 수 있음을 알려드립니다.


안녕하세요.
이 글은 SteemitKorea팀(@ayogom)님께서 저자이신 @dorian-mobileapp님을 응원하는 글입니다.
소정의 보팅을 해드렸습니다 ^^ 항상 좋은글 부탁드립니다
SteemitKorea팀에서는 보다 즐거운 steemit 생활을 위해 노력하고 있습니다.
이 글은 다음날 다시 한번 포스팅을 통해 소개 될 예정입니다. 감사합니다!

Upvoted! Thank you for supporting witness @jswit.

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.078
BTC 63096.27
ETH 1678.94
USDT 1.00
SBD 0.39