Notice
Recent Posts
Recent Comments
07-11 02:59
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Archives
Today
Total
관리 메뉴

YESHTML5

React-Native 용 Youtube Player 본문

React,React-Native

React-Native 용 Youtube Player

슬/도/아/밤/ 2022. 5. 17. 22:24
반응형

현재 진행중인 프로젝트에 Youtube로 포스팅하는 기능이 있어서,  Youtube Player

정확히는 Webview 기반으로 Youtube의 embeded로 된 코드를 컴퍼넌트화 시켰다. 

풀코드는 다음과 같다. 

 

 

/**
 * @name Youtube_videoPlayer
 * @description
 * @guide
 */
import React from 'react'
import {Dimensions, StyleSheet, ViewStyle} from 'react-native'
import WebView from 'react-native-webview'

// type
type Props = {
  url: string
  width?: number
  height?: number
  style?: ViewStyle | ViewStyle[]
}
export default function Content({url, width, height, style}: Props) {
  // hooks
  const _width = width || Dimensions.get('screen').width
  const _height = height || (_width * 9) / 16
  const _url = url
  const html = `
<!DOCTYPE html>
<html>
  <head>
  <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
  <title></title>
    <meta HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <!--스타일시트-->
    <style type="text/css">
      html,body { margin: 0 !important; padding: 0 !important; }
      .iframe100 { display: block; border: none; height: 100vh; width: 100vw; }
    </style>
    <!--//스타일시트-->
  </head>
  <body>
    <iframe src="${_url}" title="YouTube video player" class="iframe100"
    frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen >
  </iframe>
  <body>
<html>
`

  return (
    <WebView
      allowsInlineMediaPlayback={true} // ios인라인재생
      allowsFullscreenVideo={false}
      // mediaPlaybackRequiresUserAction // used to stop auto playing of video in webview as soon as it loads in iOS.
      style={[{width: _width, height: _height}, styles.container, style]}
      originWhitelist={['*']}
      javaScriptEnabled={true}
      source={{html: html}}
      onMessage={() => {}}
    />
  )
}
// styles
const styles = StyleSheet.create({
  /**@content */
  container: {minHeight: 200, alignSelf: 'center'},
})

/********************************************************
[사용법]
@src는 주소는 https://www.youtube.com/embed/INAoJ2yNVNk 형태로 되어야한다<div className=""></div>

  <View style={[layout.row, { justifyContent: 'center' }]}>
    <Button label="봄이여오라"
      style={styles.button}
      onPress={() => {
        setInfo('https://www.youtube.com/embed/6AZiZOWcC8A')
      }}
    />
    <Button label="BK Love"
      style={styles.button}
      onPress={() => {
        setInfo('https://www.youtube.com/embed/INAoJ2yNVNk')
      }}
    />
    <Button label="Dynamite"
      style={styles.button}
      onPress={() => {
        setInfo('https://www.youtube.com/embed/q8WM16VksmA')
      }}
    />
  </View>
  <VideoYoutue url={info} height={300} />

*********************************************************/

반응형
Comments