Ionic 4 Embed YouTube Video Player Example – Using Native Plugin

In our previous post, we have already discussed example application to implement a YouTube video player, but some of the steps are depreciated by the release of stable version 4 of Ionic.

In this article, we will go through the latest steps and methods to implement a YouTube video layer in the Ionic 4 Application.

Also See: Add YouTube Player in Ionic 3 Application

Create a new Ionic Application

Install latest version of Ionic and Cordova CLI.

$ npm install -g ionic cordova

Also, install the latest version of NPM using this command $ npm i npm

Now  create a new blank application

$ ionic start Ionic4YouTubeVideoPlayer blank

After the application is created, go to the root using the CD command

$ cd Ionic4YouTubeVideoPlayer

Install the Cordova and Ionic Native Plugin

We will install YouTube Video Player Cordova plugin and Ionic Native Wrapper to support plugin in our Angular application.

$ ionic cordova plugin add cordova-plugin-youtube-video-player
$ npm install @ionic-native/youtube-video-player

Add preference in config.xml with YouTube Data V3 API

In config.xml we need to add a preference configuration variable with the API key

<preference name="YouTubeDataApiKey" value="[YOUR YOUTUBE API]" />

Click here to know more on How to get API key?

Import Ionic Native Plugin in the Main Module

Now we will import YouTube Video Player native plugin in app.module.ts and add in providers array

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';


import { YoutubeVideoPlayer } from '@ionic-native/youtube-video-player/ngx';


import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';

@NgModule({
  declarations: [AppComponent],
  entryComponents: [],
  imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
  providers: [
    StatusBar,
    SplashScreen,
    YoutubeVideoPlayer,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

Add method in Home Component

Import plugin then add in contractor list to use in the home component. Replace below code in home.page.ts

import { Component } from '@angular/core';

import { YoutubeVideoPlayer } from '@ionic-native/youtube-video-player/ngx';


@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {

  constructor(
    private youtube: YoutubeVideoPlayer
    ) {}


    openMyVideo(id){
      this.youtube.openVideo(id);
    }
}

Add Button in Home HTML to Open Video

In home.html, we will add a button to call openVideo method which we added above in home.page.html

<ion-header>
  <ion-toolbar>
    <ion-title>
      Ionic 4 YouTube Player
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content padding>


  <p>Click button to open YouTube Video Player<br>
    <ion-button (click)="openMyVideo('rhQmy93LZH8')">Open Video</ion-button>
    </p>
  

</ion-content>

That’s it now you will be able to play YouTube video in a real device following the above tutorial.

 

1 thought on “Ionic 4 Embed YouTube Video Player Example – Using Native Plugin”

Leave a Comment

Your email address will not be published. Required fields are marked *