Vimeo-Player Using Exoplayer
This sample demonstrates how to use the vimeo video with exoplayer
Prerequisites
- Vimeo Developer Create app on developer account
- Vimeo Networking API Java networking library used for interacting with the Vimeo API
- ExoPlayer Create media player for Android
Source Code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.logiclab.vimeo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ui.PlayerView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.vimeo.networking.Configuration;
import com.vimeo.networking.VimeoClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import cz.msebera.android.httpclient.Header;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String VIMEO_ACCESS_TOKEN = "access token";
private static final String VIMDEO_ID = "Your video id";
private PlayerView playerView;
private SimpleExoPlayer player;
//Release references
private boolean playWhenReady = false; //If true the player auto play the media
private int currentWindow = 0;
private long playbackPosition = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Reference exoplayer view
playerView = findViewById(R.id.video_view);
Button playBtn = findViewById(R.id.button2);
playBtn.setOnClickListener(this);
//Build vimeo configuration
configVimeoClient();
} //onCreate
private void createMediaItem(String url) {
MediaItem mediaItem = MediaItem.fromUri(url);
player.setMediaItem(mediaItem);
}
private void initializePlayer() {
//To play streaming media, you need an ExoPlayer object.
//SimpleExoPlayer is a convenient, all-purpose implementation of the ExoPlayer interface.
player = new SimpleExoPlayer.Builder(this).build();
playerView.setPlayer(player);
callVimeoAPIRequest();
//Supply the state information you saved in releasePlayer to your player during initialization.
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow, playbackPosition);
player.prepare();
}
private void callVimeoAPIRequest() {
String videolink = "https://player.vimeo.com/video/" + VIMDEO_ID +"/config";
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
//params.put("key", "value");
// params.put("more", "data");
client.get(videolink, params, new TextHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, String res) {
// called when response HTTP status is "200 OK"
try {
JSONObject jsonObject = new JSONObject(res);
JSONObject req = jsonObject.getJSONObject("request");
JSONObject files = req.getJSONObject("files");
JSONArray progressive = files.getJSONArray("progressive");
JSONObject array1 = progressive.getJSONObject(1);
String v_url=array1.getString("url");
Log.d("URLL ",v_url);
createMediaItem(v_url);
} catch (JSONException e){
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
}
}
);
}
private void configVimeoClient() {
Configuration.Builder configBuilder =
new Configuration.Builder(MainActivity.VIMEO_ACCESS_TOKEN) //Pass app access token
.setCacheDirectory(this.getCacheDir());
VimeoClient.initialize(configBuilder.build());
}
@Override
public void onClick(View v) {
player.setPlayWhenReady(true);
}
@Override
public void onStart() {
super.onStart();
initializePlayer();
}
}
Android Manifest.xml
set permissions
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
build.gradle(:app)
add
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}
dependencies
//Vimeo Network
implementation "com.vimeo.networking:vimeo-networking:1.1.3"
//Full ExoPlayer library
implementation 'com.google.android.exoplayer:exoplayer:2.12.0'
//Android Async Http Client
implementation 'com.loopj.android:android-async-http:1.4.9'
https://guides.codepath.com/android/Using-Android-Async-Http-Client
Sending a Network Request
Now, we just create an AsyncHttpClient
, and then execute a request specifying an anonymous class as a callback:
import com.loopj.android.http.*;
import cz.msebera.android.httpclient.Header;
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("key", "value");
params.put("more", "data");
client.get("http://www.google.com", params, new TextHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, String res) {
// called when response HTTP status is "200 OK"
}
@Override
public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
}
}
);
This will automatically execute the request asynchronously and fire the onSuccess
when the response returns a success code and onFailure
if the response does not.
No comments:
Post a Comment