Skip to content

Commit

Permalink
Update 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFinestArtist committed Jun 27, 2015
1 parent 3de607b commit ab9c7f3
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 103 deletions.
51 changes: 29 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Easily reference the library in your Android projects using this dependency in y

```Gradle
dependencies {
compile 'com.thefinestartist:ytpa:1.1.1'
compile 'com.thefinestartist:ytpa:1.2.0'
}
```

Expand All @@ -51,30 +51,22 @@ It supports Android API 7+.

## Features
* Orientation Support

AUTO

AUTO_START_WITH_LANDSCAPE

ONLY_LANDSCAPE

ONLY_PORTRAIT
* AUTO
* AUTO_START_WITH_LANDSCAPE
* ONLY_LANDSCAPE
* ONLY_PORTRAIT

* Media Volume Support

While watching YouTube Player, users should be able to set media volume!!!
* While watching YouTube Player, users should be able to set media volume!!!

* Video Play Error Support

If the video is not playable, it send to youtube app or other browser which will might play it.
* If the video is not playable, it send to youtube app or other browser which will might play it.

* Animation Support

Activity closing animation can be customized.
* Activity closing animation can be customized.

* Status Bar Support

On screen portrait mode, it removed status bar automatically.
* On screen portrait mode, it removed status bar automatically.


## Set Up AndroidManifest.xml
Expand All @@ -97,9 +89,8 @@ It supports Android API 7+.
```java
Intent intent = new Intent(MainActivity.this, YouTubePlayerActivity.class);

// Youtube video ID or Url (Required)
// Youtube video ID (Required, You can use YouTubeUrlParser to parse Video Id from url)
intent.putExtra(YouTubePlayerActivity.EXTRA_VIDEO_ID, "iS1g8G_njx8");
intent.putExtra(YouTubePlayerActivity.EXTRA_VIDEO_URL, "https://youtu.be/iS1g8G_njx8");

// Youtube player style (DEFAULT as default)
intent.putExtra(YouTubePlayerActivity.EXTRA_PLAYER_STYLE, YouTubePlayer.PlayerStyle.DEFAULT);
Expand All @@ -123,13 +114,29 @@ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
```

## YoutubeUrlParser
## YouTubeUrlParser

This util helps to retrieve youtube video id from youtube url or vice versa. [Reference](https://gist.github.com/afeld/1254889)

```java
String vidoeId = YoutubeUrlParser.getVideoId(videoUrl);
String vidoeUrl = YoutubeUrlParser.getVideoId(videoId);
String vidoeId = YouTubeUrlParser.getVideoId(videoUrl);
String vidoeUrl = YouTubeUrlParser.getVideoUrl(videoId);
```

## YouTubeApp

This util helps to open Youtube App and play specific video.

```java
YouTubeApp.startVideo(context, videoId);
```

## YouTubeThumbnail

This util returns Youtube thumbnail image url.

```java
YouTubeThumbnail.getUrlFromVideoId(videoId, Quality.HIGH);
```


Expand Down
13 changes: 9 additions & 4 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

defaultConfig {
minSdkVersion 7
targetSdkVersion 22
versionCode 2
versionName "1.1.1"
versionCode 3
versionName "1.2.0"
}
lintOptions {
abortOnError false
Expand All @@ -18,14 +23,14 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.+'
compile 'com.android.support:support-annotations:22.+'
}

publish {
userOrg = 'thefinestartist'
groupId = 'com.thefinestartist'
artifactId = 'ytpa'
version = '1.1.1'
version = '1.2.0'
description = 'Simply pass a url to play youtube video in new activity. It supports screen orientation, media volume control and etc.'
website = 'https://github.com/TheFinestArtist/YouTubePlayerActivity'
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.FrameLayout.LayoutParams;
Expand All @@ -21,8 +22,7 @@
import com.thefinestartist.ytpa.enums.Orientation;
import com.thefinestartist.ytpa.utils.AudioUtil;
import com.thefinestartist.ytpa.utils.StatusBarUtil;
import com.thefinestartist.ytpa.utils.YouTubeUrlParser;
import com.thefinestartist.ytpa.utils.YouTubeUtil;
import com.thefinestartist.ytpa.utils.YouTubeApp;

public class YouTubePlayerActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
Expand All @@ -31,10 +31,9 @@ public class YouTubePlayerActivity extends YouTubeBaseActivity implements

private static final int RECOVERY_DIALOG_REQUEST = 1;

public static final String META_DATA_NAME = "com.thefinestartist.ytpa.YouTubePlayerActivity.ApiKey";
private static final String META_DATA_NAME = "com.thefinestartist.ytpa.YouTubePlayerActivity.ApiKey";

public static final String EXTRA_VIDEO_ID = "video_id";
public static final String EXTRA_VIDEO_URL = "video_url";

public static final String EXTRA_PLAYER_STYLE = "player_style";

Expand Down Expand Up @@ -88,13 +87,7 @@ private void initialize() {
if (googleApiKey == null)
throw new NullPointerException("Google API key must not be null. Set your api key as meta data in AndroidManifest.xml file.");

String videoUrl = getIntent().getStringExtra(EXTRA_VIDEO_URL);
if (videoUrl != null)
videoId = YouTubeUrlParser.getVideoId(videoUrl);

if (videoId == null)
videoId = getIntent().getStringExtra(EXTRA_VIDEO_ID);

videoId = getIntent().getStringExtra(EXTRA_VIDEO_ID);
if (videoId == null)
throw new NullPointerException("Video ID must not be null");

Expand Down Expand Up @@ -238,7 +231,7 @@ public void onFullscreen(boolean fullScreen) {
public void onError(ErrorReason reason) {
Log.e("onError", "onError : " + reason.name());
if (handleError && ErrorReason.NOT_PLAYABLE.equals(reason))
YouTubeUtil.startVideo(this, videoId);
YouTubeApp.startVideo(this, videoId);
}

@Override
Expand All @@ -264,7 +257,7 @@ public void onVideoStarted() {

// Audio Managing
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
AudioUtil.adjustMusicVolume(getApplicationContext(), true, showAudioUi);
StatusBarUtil.hide(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.thefinestartist.ytpa.enums;

/**
* Created by TheFinestArtist on 6/27/15.
*/
public enum Quality {
FIRST, SECOND, THIRD, FOURTH, MAXIMUM, STANDARD_DEFINITION, MEDIUM, HIGH, DEFAULT
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,17 @@
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.support.annotation.NonNull;

import java.util.List;

/**
* Created by TheFinestArtist on 2/16/15.
*/
public class YouTubeUtil {
public class YouTubeApp {

public static void startVideo(Context context, String videoId) {
if (context == null || videoId == null)
return;

Uri video_uri = Uri.parse(YouTubeUrlParser.getVidoeUrl(videoId));
public static void startVideo(@NonNull Context context, @NonNull String videoId) {
Uri video_uri = Uri.parse(YouTubeUrlParser.getVideoUrl(videoId));
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + videoId));
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(
intent,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.thefinestartist.ytpa.utils;

import android.support.annotation.NonNull;

import com.thefinestartist.ytpa.enums.Quality;

/**
* Created by TheFinestArtist on 6/27/15.
*/
public class YouTubeThumbnail {
// Each YouTube video has 4 generated images. They are predictably formatted as follows:
//
// http://img.youtube.com/vi/<insert-youtube-video-id-here>/0.jpg
// http://img.youtube.com/vi/<insert-youtube-video-id-here>/1.jpg
// http://img.youtube.com/vi/<insert-youtube-video-id-here>/2.jpg
// http://img.youtube.com/vi/<insert-youtube-video-id-here>/3.jpg
//
// The first one in the list is a full size image and others are thumbnail images. The default thumbnail image (ie. one of `1.jpg`, `2.jpg`, `3.jpg`) is:
//
// http://img.youtube.com/vi/<insert-youtube-video-id-here>/default.jpg
//
// For the high quality version of the thumbnail use a url similar to this:
//
// http://img.youtube.com/vi/<insert-youtube-video-id-here>/hqdefault.jpg
//
// There is also a medium quality version of the thumbnail, using a url similar to the HQ:
//
// http://img.youtube.com/vi/<insert-youtube-video-id-here>/mqdefault.jpg
//
// For the standard definition version of the thumbnail, use a url similar to this:
//
// http://img.youtube.com/vi/<insert-youtube-video-id-here>/sddefault.jpg
//
// For the maximum resolution version of the thumbnail use a url similar to this:
//
// http://img.youtube.com/vi/<insert-youtube-video-id-here>/maxresdefault.jpg
//
// All of the above urls are available over https too. Just change `http` to `https` in any of the above urls. Additionally, the slightly shorter hostname `i3.ytimg.com` works in place of `img.youtube.com` in the example urls above.
//
// Alternatively, you can use the [YouTube Data API (v3)][1] or the older [YouTube API v2.0][2] to get thumbnail images.
//
//
// [1]: https://developers.google.com/youtube/v3/
// [2]: http://code.google.com/apis/youtube/2.0/developers_guide_php.html

public static String getUrlFromVideoId(@NonNull String videoId, @NonNull Quality quality) {
switch (quality) {
case FIRST:
return "http://img.youtube.com/vi/" + videoId + "/0.jpg";
case SECOND:
return "http://img.youtube.com/vi/" + videoId + "/1.jpg";
case THIRD:
return "http://img.youtube.com/vi/" + videoId + "/2.jpg";
case FOURTH:
return "http://img.youtube.com/vi/" + videoId + "/3.jpg";
case MAXIMUM:
return "http://img.youtube.com/vi/" + videoId + "/maxresdefault.jpg";
case STANDARD_DEFINITION:
return "http://img.youtube.com/vi/" + videoId + "/sddefault.jpg";
case MEDIUM:
return "http://img.youtube.com/vi/" + videoId + "/mqdefault.jpg";
case HIGH:
return "http://img.youtube.com/vi/" + videoId + "/hqdefault.jpg";
case DEFAULT:
default:
return "http://img.youtube.com/vi/" + videoId + "/default.jpg";
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.thefinestartist.ytpa.utils;

import android.support.annotation.NonNull;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -11,10 +13,7 @@ public class YouTubeUrlParser {
// (?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})
final static String reg = "(?:youtube(?:-nocookie)?\\.com\\/(?:[^\\/\\n\\s]+\\/\\S+\\/|(?:v|e(?:mbed)?)\\/|\\S*?[?&]v=)|youtu\\.be\\/)([a-zA-Z0-9_-]{11})";

public static String getVideoId(String videoUrl) {
if (videoUrl == null || videoUrl.trim().length() <= 0)
return null;

public static String getVideoId(@NonNull String videoUrl) {
Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(videoUrl);

Expand All @@ -23,7 +22,7 @@ public static String getVideoId(String videoUrl) {
return null;
}

public static String getVidoeUrl(String videoId) {
public static String getVideoUrl(@NonNull String videoId) {
return "http://youtu.be/" + videoId;
}
}
27 changes: 18 additions & 9 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
buildscript {
repositories {
maven { url 'http://download.crashlytics.com/maven' }
maven { url 'https://maven.fabric.io/public' }
}

dependencies {
classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1.+'
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'crashlytics'
apply plugin: 'io.fabric'

repositories {
maven { url 'http://download.crashlytics.com/maven' }
maven { url 'https://maven.fabric.io/public' }
}


android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}

defaultConfig {
applicationId "com.thefinestartist.ytpa.sample"
minSdkVersion 8
minSdkVersion 9
targetSdkVersion 22
versionCode 5
versionName "1.4.0"
versionCode 6
versionName "1.5.0"
}
buildTypes {
release {
Expand All @@ -38,7 +43,11 @@ dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':library')
compile 'com.android.support:appcompat-v7:22.0.+'
compile 'com.jakewharton:butterknife:5.1.2'
compile 'com.jakewharton:butterknife:6.+'
compile 'com.afollestad:material-dialogs:0.7.3.+'
compile 'com.crashlytics.android:crashlytics:1.+'
compile 'com.google.android.gms:play-services-ads:7.+'
compile 'com.squareup.picasso:picasso:2.+'
compile('com.crashlytics.sdk.android:crashlytics:2.4.0@aar') {
transitive = true;
}
}
3 changes: 3 additions & 0 deletions sample/fabric.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#Contains API Secret used to validate your application. Commit to internal source control; avoid making secret public.
#Sat Jun 27 18:03:28 KST 2015
apiSecret=ed8e20e7d4101d02885f3a2839ee4d5f9045819d88301d58f8e88d734f3d603e
2 changes: 1 addition & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
<meta-data
android:name="com.crashlytics.ApiKey"
android:value="286eb9845a463270ba6a058f160c64043f7639e9" />
</application>

</application>
</manifest>
Loading

0 comments on commit ab9c7f3

Please sign in to comment.