Skip to content

Commit

Permalink
Updating Parser
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFinestArtist committed Feb 15, 2015
1 parent 9e6e857 commit 1417659
Show file tree
Hide file tree
Showing 21 changed files with 140 additions and 132 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 TheFinestArtist

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Moving Button

Simply pass a url to play youtube video on new activity. It supports screen orientation, media volume control and etc.

```xml
<uses-permission android:name="android.permission.INTERNET" />
<activity
android:name="com.thefinestartist.ytpa.YouTubePlayerActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="sensor"
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
</activity>
```

## YoutubeUrlParser

https://androidsnippets.wordpress.com/2012/10/11/how-to-get-extract-video-id-from-an-youtube-url-in-android-java/


## License

```
The MIT License (MIT)
Copyright (c) 2015 TheFinestArtist
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
```
Binary file added art/web_hi_res_512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ publish {
groupId = 'com.thefinestartist'
artifactId = 'ytpa'
version = '1.0.0'
description = 'YouTube Player Activity'
description = 'Simply pass a url to play youtube video on 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
@@ -1,19 +1,3 @@
/**
* Copyright 2013 The Finest Artist
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.thefinestartist.ytpa;

import android.annotation.SuppressLint;
Expand All @@ -40,16 +24,13 @@
import com.google.android.youtube.player.YouTubePlayerView;

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

public class YouTubePlayerActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnFullscreenListener,
YouTubePlayer.OnInitializedListener,
YouTubePlayer.PlayerStateChangeListener {

public static final String EXTRA_VIDEO_ID = "video_id";
private static final boolean TOAST = false;
private static final int RECOVERY_DIALOG_REQUEST = 1;
public static final String GOOGLE_API_KEY = "AIzaSyAOfxiG4aV66h3XmssCEkP3qCvCqMbDGDI";

Expand Down Expand Up @@ -137,23 +118,14 @@ public void onConfigurationChanged(Configuration newConfig) {
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
if (mPlayer != null)
mPlayer.setFullscreen(true);
if (TOAST)
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
if (mPlayer != null)
mPlayer.setFullscreen(false);
if (TOAST)
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
} else {
if (TOAST)
Toast.makeText(this, "configuration changed", Toast.LENGTH_SHORT).show();
}
}

@Override
public void onFullscreen(boolean fullsize) {
if (TOAST)
Toast.makeText(this, "full size change : " + fullsize, Toast.LENGTH_SHORT).show();
if (fullsize) {
setRequestedOrientation(LANDSCAPE_ORIENTATION);
} else {
Expand Down Expand Up @@ -183,44 +155,6 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
return super.onKeyDown(keyCode, event);
}

public static String getYouTubeVideoId(String video_url) {

if (video_url != null && video_url.length() > 0) {

Uri video_uri = Uri.parse(video_url);
String video_id = video_uri.getQueryParameter("v");

if (video_id == null)
video_id = parseYoutubeVideoId(video_url);

return video_id;
}
return null;
}

public static String parseYoutubeVideoId(String youtubeUrl) {
String video_id = null;
if (youtubeUrl != null && youtubeUrl.trim().length() > 0 &&
youtubeUrl.startsWith("http")) {
// ^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/
String expression = "^.*((youtu.be" + "\\/)"
+ "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*";
CharSequence input = youtubeUrl;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
// Regular expression some how doesn't work with id with "v" at
// prefix
String groupIndex1 = matcher.group(7);
if (groupIndex1 != null && groupIndex1.length() == 11)
video_id = groupIndex1;
else if (groupIndex1 != null && groupIndex1.length() == 10)
video_id = "v" + groupIndex1;
}
}
return video_id;
}

@Override
public void onError(ErrorReason reason) {
if (YouTubePlayer.ErrorReason.BLOCKED_FOR_APP.equals(reason)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.thefinestartist.ytpa.utils;

import android.net.Uri;

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

/**
* Created by TheFinestArtist on 2/15/15.
*/
public class YoutubeUrlParser {

// ^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/
final static String reg = "^.*((youtu.be\\/)|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*";

public static String parseVideoId(String url) {
String videoDd = null;
if (url != null && url.trim().length() > 0 && url.startsWith("http")) {
Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(url);

// Regular reg some how doesn't work with id with "v" at prefix
if (matcher.matches()) {
String groupIndex = matcher.group(7);
if (groupIndex != null && groupIndex.length() == 11)
videoDd = groupIndex;
else if (groupIndex != null && groupIndex.length() == 10)
videoDd = "v" + groupIndex;
}
}
return videoDd;
}

public static String getVideoId(String url) {
if (url == null)
return null;

Uri uri = Uri.parse(url);
String videoId = uri.getQueryParameter("v");

if (videoId == null)
videoId = parseVideoId(url);

return videoId;
}
}
8 changes: 4 additions & 4 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thefinestartist.ytpa.sample" >
package="com.thefinestartist.ytpa.sample">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {

Expand All @@ -13,27 +10,4 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 4 additions & 8 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

</RelativeLayout>
5 changes: 0 additions & 5 deletions sample/src/main/res/menu/menu_main.xml

This file was deleted.

Binary file removed sample/src/main/res/mipmap-hdpi/ic_launcher.png
Binary file not shown.
Binary file removed sample/src/main/res/mipmap-mdpi/ic_launcher.png
Binary file not shown.
Binary file removed sample/src/main/res/mipmap-xhdpi/ic_launcher.png
Binary file not shown.
Binary file removed sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
Binary file not shown.
5 changes: 0 additions & 5 deletions sample/src/main/res/values-v21/styles.xml

This file was deleted.

6 changes: 0 additions & 6 deletions sample/src/main/res/values-w820dp/dimens.xml

This file was deleted.

11 changes: 11 additions & 0 deletions sample/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primary">#F44336</color>
<color name="primary_dark">#D32F2F</color>
<color name="primary_light">#FFCDD2</color>
<color name="accent">#9E9E9E</color>
<color name="primary_text">#212121</color>
<color name="secondary_text">#727272</color>
<color name="icons">#FFFFFF</color>
<color name="divider">#B6B6B6</color>
</resources>
5 changes: 0 additions & 5 deletions sample/src/main/res/values/dimens.xml

This file was deleted.

5 changes: 1 addition & 4 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
<resources>
<string name="app_name">sample</string>

<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="app_name">Youtube Player</string>
</resources>
10 changes: 8 additions & 2 deletions sample/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
<!-- Customize your theme here. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowBackground">@android:color/white</item>
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:textColorPrimary">@color/primary_text</item>
<item name="android:textColorSecondary">@color/secondary_text</item>
<item name="windowActionBar">false</item>
</style>

</resources>

0 comments on commit 1417659

Please sign in to comment.