This tutorial is about creating online TV application where we can watch live tv channels using the internet.
Please follow the steps,
Step 1: Create a new Android application
Step 2: Download youtube player API from below link
https://developers.google.com/youtube/android/player/downloads/
- Unzip the downloaded file and copy YouTubeAndroidPlayerApi.jar file from libs folder and paste it in libs folder of your project
- Sync your project
Step 3: Modify activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="alabs.tvapp.MainActivity"
tools:layout_editor_absoluteY="81dp">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="220dp"
android:text="CLick here to Watch"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
Step 4: Create new layout yt_player.xml and add following code, here we are using Youtube player view
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/youTubePlayerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.google.android.youtube.player.YouTubePlayerView>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="152dp"
android:text="Full Screen"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/youTubePlayerView" />
</android.support.constraint.ConstraintLayout>
Step 5: Update MainActivity.java file
package alabs.tvapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button watch ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
watch = (Button) findViewById(R.id.button);
watch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),PlayerActivity.class);
startActivity(intent);
}
});
}
}
Step 6: Create a new java file PlayerActivity.java file
/**
* Insert your code here
*/package alabs.tvapp;
/**
* Created by ADJ on 3/21/2018.
*/
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;
public class PlayerActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
YouTubePlayer player;
Button fullScreen;
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.yt_player);
YouTubePlayerView playerView = (YouTubePlayerView)findViewById(R.id.youTubePlayerView);
playerView.initialize(DeveloperKey.DEVELOPER_KEY,this);
fullScreen = (Button)findViewById(R.id.button2);
fullScreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
player.setFullscreen(true);
}
});
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean b) {
this.player = player;
if(!b){
player.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
// You can change the ID.. of the video to be played
player.loadVideo("WNna4W4jLek");
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
}
Step 7: Create DeveloperKey.java file to hold developer API Key.
Get the Developer Key from https://console.developers.google.com/
and also Enable Youtube Data API
Follow video tutorial on getting API Key.
package alabs.tvapp;
/**
* Created by ADJ on 3/21/2018.
*/
public class DeveloperKey {
public static final String DEVELOPER_KEY = "Your Developer Key Here";
}
Step 8: Update manifest.xml file with new activities and internet permission
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="alabs.tvapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PlayerActivity"
android:screenOrientation="nosensor"
android:configChanges="keyboardHidden|orientation|screenSize"/> />
</application>
</manifest>
Step 9: Build the project and run the app,
