Giter VIP home page Giter VIP logo

rn-youtube-android's Introduction

rn-youtube-android npm version

Important: Android only

Play videos from YouTube using YouTube's API, in React Native.

Known Issues

⚠️ Works reliably on version 0.29 >= <= 0.34 of react-native ⚠️ Due to react-native's high change pace, I cannot guarentee that this package will work on other versions. Plays fullscreen videos only, in landscape mode only. PRs are welcome :)

Notice

  • Since it is a React Native project, it was developed and tested on OSX only, I apologize for the linux folks.
  • Intended only for existing Android apps that are integrating React Native - Facebook's instructions.
  • Make sure you have the latest React Native/Babel/random cool stuff in your project.
  • Using YouTubeAndroidPlayerApi-1.2.2.zip - current version

Getting Started

  • Add to settings.gradle:
include ':RNYouTubePlayer', ':app'
project(':RNYouTubePlayer').projectDir = new File(rootProject.projectDir, '<your node_modules path>/rn-youtube-android/android')
  • Add to build.gradle:
dependencies {
    ...
    compile project(':RNYouTubePlayer')
}
  • In the activity that starts React Native, add the package to the ReactInstanceManager builder:
import com.applicaster.RNYouTubePlayer.YoutubePlayerReactPackage;

...

Builder reactManagerBuilder = ReactInstanceManager.builder()
    .setApplication(getApplication())
    .addPackage(new MainReactPackage())
    .addPackage(new YoutubePlayerReactPackage())
    ...
  • Important: In order to get callbacks from the YouToube player, which runs on a separate activity, add this to the activity that holds the ReactInstanceManager:
private ReactInstanceManager reactInstanceManager; // Initialize it following React Native instructions, above

...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    reactInstanceManager.onActivityResult(requestCode, resultCode, data);
}

Usage

  • Set up your <YouTube api token>: Google is your friend
  • Choose a video from YouTube, like this shameless plug: KawcajJGX-w
  • Add to your index.android.js or anywhere inside the project:
import RNYouTubePlayer from 'rn-youtube-android';
RNYouTubePlayer.play(<youtube api token>, <youtube video url - hash only>);
  • It is highly recommended to surround the player with await / async in order to reap the future benefits of callbacks:
playVideo = async function(callback) { // example: token and video url exists in props
  try {
    let {
      state,
    } = await RNYouTubePlayer.play('<< YouTube API Token >>', 'KawcajJGX-w');

    if (state === 'stopped') {
      callback(); // stopped watching video, do something
    }
  } catch (e) {
    console.error(e);
  }
};

YouTubeAndroidPlayerApi.jar belongs to Google, Inc. - no copyright infringement intended.

rn-youtube-android's People

Contributors

maxim-c avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.