In some applications, there is a video playback function without going to the official YouTube application. How to implement it? Are there any special libraries? If the implementation is very complicated, how easy is it to insert a video from YouTube into the application, even with the condition that it will be played in the official application?
2 answers
[ http://www.sitepoint.com/using-the-youtube-api-to-embed-video-in-an-android-app/ ]
Here you will find an example of using such a YouTube window.
There you will find a link to the whole project. The link is called
download the completed project here
.
In order for the project to work, you need an api key that you need to create in console.developers.google.com
, you only need a Google account.
There create a virtual project, click on the button.
Enable APIs and get credentials like keys
YouTube Data API
- Enable
seems button on top. Click it, then left tab
Credentials
, then on the blue button of the credentials
, there will be a submenu, there android key
package name
- the name of your project, type com.example.zaynulabid.myprojectname
. I have a SHA-1
39:A2:AB:DF:C9:A4:24:BB:19:0F:73:40:FC:99:08:4E:36:6B:FC:6D
, you will have another.
How do you get it: In the android studio at the bottom there is a terminal window, write a command for Mac OS X
keytool -list -v -keystore ~/.android/debug.keystore
or
keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android -keypass android
for Windows7:
keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore -alias androiddebugkey -storepass android -keypass android
BUT! This is a DEBUG CERTIFICATE for testing. For unloading to the market, it will not work. To upload to the market, it seems to be "RELEASE CERTIFICATE".
If there is a line
Enter keystore password:
android
There will be a bunch of lines. Find "SHA1:" and copy the contents and paste them into the box when creating the key.
When you create this api key, shove it into the project in the class
Config.java
PS Browser API is needed to display a list of videos, and Youtube API is needed for the player itself in the application, and it should read the number of views. In general, the browser api is used when uploading data about the list of videos, YouTube api when playing through the player.
Also, the last. You need to download YouTube libraries. From here
[ https://developers.google.com/youtube/android/player/downloads/ ]
Unpack the archive in the lib folder of your project and in
build.gradle
in the block dependencies
enter the line
compile files('libs/YouTubeAndroidPlayerApi.jar')
To expand the functionality, I searched and added a couple more files. You should not need them for this project. But if that, look in the internet and download, too, from the off site google
compile files('libs/google-api-client-1.21.0.jar') compile files('libs/google-api-client-android-1.21.0.jar') compile files('libs/google-http-client-1.21.0.jar') compile files('libs/google-http-client-android-1.21.0.jar') compile files('libs/google-http-client-jackson2-1.21.0.jar') compile files('libs/google-oauth-client-1.21.0.jar') compile files('libs/jackson-core-2.1.3.jar') compile files('libs/jsr305-1.3.9.jar')
If you work with the youtube channel, you will need a browser api
, you can also create it in the Credentials, where youtube api key was created. When creating a key, the address field for access is left blank (these are delimiters for the key).
Here is an example of the address to the video
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=[ID плейлиста]&key=[ключ browser api]
I have so:
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=UUIQ6UDqSoWIEoGZ5tLJd1QA&key=AIzaSyDLMSSM79_......................BQ
If you work with the project to which I referred, then there will be a player class in which there will be a line of code youTubeView.initialize(Config.YOUTUBE_API_KEY, this);
Here in the YOUTUBE_API_KEY class Config and you need to insert your YouTube-api key
- I apologize, unknowingly wrote, but it turned out that you need an already installed youtube player to work with the youtube library. I understood this when I installed my application on Android TV - zayn1991
You can try through VideoView
:
VideoView videoView = (VideoView)findViewById(R.id.video_view); String pathToVide = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4"; Uri videoUri = Uri.parse(pathToVide); videoView.setVideoURI(videoUri); videoView.start();
Markup:
<VideoView android:id="@+id/video_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" />
Alternatively, you can use MediaPlayer
. Most likely you prefer it to use. Under the link you can see the implementation.
An example is taken from here.
- Thank you very much. Today I will try both - Oleg Aleev