Java Question Android-YouTube-Player

warwound

Expert
Licensed User
Longtime User
I'm trying to get Android-YouTube-Player to work in a B4A project.

It's an Activity that plays You Tube videos - handling many tasks automatically.
It chooses a video quality that matches the device's internet connection speed for example.

You can download a compiled JAR library and/or the source code.
So i thought it'd be easy enough to download the compiled JAR and get it working...

I created a new B4A project:

B4X:
'Activity module
Sub Process_Globals
End Sub

Sub Globals
   Dim Button1 As Button
   Dim YouTubePlayerActivity As B4AOpenYouTubeActivity
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Button1.Initialize("Button1")
   Button1.Text="Start You Tube player"
   
   Activity.AddView(Button1, 25%x, 25%y, 50%x, 50%y)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub Button1_Click
   StartActivity(YouTubePlayerActivity.CreateIntent)
End Sub

Updated the manifest:

B4X:
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddApplicationText(<activity android:name="com.keyes.youtube/.OpenYouTubePlayerActivity" />)

And created a simple library class to generate an Intent to test with:

B4X:
@ActivityObject
@Author("Me")
@DependsOn(values = { "OpenYouTubeActivity" })
@ShortName("B4AOpenYouTubeActivity")
@Version(1.00f)
public class B4AOpenYouTubeActivity {
   public Intent CreateIntent(BA pBA){
      Intent intent1 = new Intent(null, Uri.parse("ytv://t56cHZymmvs"), pBA.context, OpenYouTubePlayerActivity.class);
      return intent1;
   }
}

The downloaded JAR is OpenYouTubeActivity.jar and is in my additional libraries folder.

Everything compiles with no errors but a click on Button1 raises an exception

android.content.ActivityNotFoundException: Unable to find explicit activity class {uk.co.martinpearman.b4a.youtubeplayeractivitydemo/com.keyes.youtube.OpenYouTubePlayerActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
at android.app.Activity.startActivityForResult(Activity.java:2827)
at android.app.Activity.startActivity(Activity.java:2933)
at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:665)
at uk.co.martinpearman.b4a.youtubeplayeractivitydemo.main._button1_click(main.java:231)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:143)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:131)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:127)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:49)
at android.view.View.performClick(View.java:2506)
at android.view.View$PerformClick.run(View.java:9112)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
android.content.ActivityNotFoundException: Unable to find explicit activity class {uk.co.martinpearman.b4a.youtubeplayeractivitydemo/com.keyes.youtube.OpenYouTubePlayerActivity}; have you declared this activity in your AndroidManifest.xml?

uk.co.martinpearman.b4a.youtubeplayeractivitydemo/com.keyes.youtube.OpenYouTubePlayerActivity cannot be found.

I've also downloaded the source code and built the library so that it contains all the OpenYouTubeActivity classes - no need for DependsOn and the separate JAR file.
The error is the same - activity not found.

Any ideas where i'm going wrong?

Martin.
 
Last edited:

thedesolatesoul

Expert
Licensed User
Longtime User
That is the problem with this library.
You need to some how include the activity in the library to your manifest.

As you can see the path looks weird? uk.co.martinpearman.b4a.youtubeplayeractivitydemo/com.keyes.youtube.OpenYouTubePlayerActivity

Shouldnt it be something like:

uk.co.martinpearman.b4a.youtubeplayeractivitydemo/OpenYouTubePlayerActivity

Maybe look at this thread for some hints:
http://www.b4x.com/forum/basic4andr...-how-start-activity-defined-java-library.html
 

warwound

Expert
Licensed User
Longtime User
:sign0060:

The manifest should be:

B4X:
AddPermission(android.permission.INTERNET)
AddPermission(android.permission.ACCESS_WIFI_STATE)
AddApplicationText(<activity android:name="com.keyes.youtube.OpenYouTubePlayerActivity" />)

Now it works perfectly!
I'll play with it a bit to implement all possible features and get it uploaded as time allows.

Martin.
 

warwound

Expert
Licensed User
Longtime User
To get it working i just downloaded the JAR file and renamed it to OpenYouTubeActivity.jar then placed it in my additional libraries folder.

In Eclipse i added that JAR to the build path and created a helper class which simply returns a test Intent that can be used in B4A to start the OpenYouTubeActivity:

B4X:
@ActivityObject
@Author("Martin Pearman")
@DependsOn(values = { "OpenYouTubeActivity" })
@ShortName("B4AOpenYouTubePlayerActivity")
@Version(1.00f)
public class B4AOpenYouTubePlayerActivity {
   public Intent CreateIntent(BA pBA){
      Intent intent1 = new Intent(null, Uri.parse("ytv://t56cHZymmvs"), pBA.context, OpenYouTubePlayerActivity.class);
      return intent1;
   }
}

Martin.
 

warwound

Expert
Licensed User
Longtime User
Great. Will you be releasing it soon?

I'll work on it over the next couple of days and if all goes well will upload it.

There's methods to play a video and play a playlist to implement.
And it looks as though you can get the player activity to return to your B4A activity when playback has finished - rather than leaving the user to manually click back.

Martin.
 

Inman

Well-Known Member
Licensed User
Longtime User
Sounds fantastic. Is there an option for the user to switch to full screen for playback? Also does it have the usual components like play/pause button and position seeker or should we implement it ourselves?
 

warwound

Expert
Licensed User
Longtime User
The player activity displays full screen with no title by default.
I'm not sure if that can be configured or whether it is hardcoded.

It has the usual media controls and timeline too.

Screengrab attached.

Martin.
 

Attachments

  • screengrab.jpg
    screengrab.jpg
    41 KB · Views: 381

warwound

Expert
Licensed User
Longtime User
I've been too busy to look at this again.

After a bit of discussion with another forum member it looks as though the only real reason to go ahead and make this into a library is so that your B4A application has access to high quality You Tube videos.

The AndroidYouTubeActivity can display YouTube playlists i think, other than that everything else it does can be accomplished with a B4A VideoView.

YouTube make it easy to get a URL to a low quality 3GP video but offer no official access to high quality streams.

So what i have done is to extract the code from AndroidYouTubeActivity that gets the available streaming URLs for a video and made that into a B4A library.
You pass this library a You Tube video id and it finds out what streaming URLs are available, your B4A Activity can get the available formats as an Array of Strings and then you can choose which format to load (or try to load) in a B4A VideoView.

These high quality streams are protected by You Tube - you are required to pass a 'token' in the URL when requesting a high quality stream.
The library handles this - the URL to a high quality stream will contain the token required.

I'm not ready to publish the library yet but as you're keen i'll PM you a link where you can get the library and a sample B4A application.

With a bit of feedback i can decide whether to just publish the library that gets the high quality URLs or whether there still exists users that want the AndroidYouTubeActivity as a library.

Martin.
 
Top