Use methods of the same name in one class Java Android - java

I have an app that shows video streaming using video views.
Since the video formats I want to show are not supported by Android Versions <2.0 I am using the vitamio library to show the videos on older devices.
However Vitamio is way slower than Android video view libraries and i don't want to use it on all devices; i just want to use it on older ones.
However the names of the libraries and the methods are the same:
i.e.
import android.media.MediaController;
import io.vov.vitamio.MediaController;
In the class I only import the android Media controller and i access the vitamio one like this:
io.vov.vitamio.widget.MediaController mediacontroller = new io.vov.vitamio.widget.MediaController(parentActivity);
which works fine until I want to access one of the methods of the vitamio library. For example:
videoView.io.vov.vitamio.widget.VideoView.setVideoURI(video);
This does not work as the correct use is:
videoView.setVideoURI(video);
If I do that then the Android media player is accessed and the code isn't correct.
How can I access a method with the same name in the same class. I want to be able to use both methods based on the device of the user.
Thanks in advance.

videoView.io.vov.vitamio.widget.VideoView.setVideoURI(video);
By doing that you are trying to call the method in a static way.
You should be declaring your videoView variable as an instance of videoView.io.vov.vitamio.widget.VideoView and then call the appropriate method :
// declaration of variable as an instance of the correct class
videoView.io.vov.vitamio.widget.VideoView videoView;
// now, use the method
videoView.setVideoURI(video);

Check your class VideoView is from type "videoView.io.vov.vitamio.widget.VideoView" and not "android.widget.VideoView".

You don't need videoView.io.vov.vitamio.widget.VideoView.setVideoURI(video), it should be videoView.setVideoURI(video) where videoView is of type io.vov.vitamio.widget.VideoView.
If it's an Object, you can cast explicitly: ((io.vov.vitamio.widget.VideoView)myObject).setVideoURI(video).

Related

How to use spécific User-Agent with (Magical) ExoPlayer in android?

I'm using Android Studio and i need to add ExoPlayer in my app and i need to use a specific user-agent, so i found this MagicalExoPlayer :
https://github.com/HamidrezaAmz/MagicalExoPlayer
This "version" of ExoPlayer is very easy to use and in the readme we can see it's easy to put specific header.
So i tried to put a specific user-agent like this :
HashMap<String , String> extraHeaders = new HashMap<>();
extraHeaders.put("User-Agent","My User Agent");
andExoPlayerView.setSource("STREAM_URL", extraHeaders);
But the user-agent don't change, i have the default user agent of ExoPlayer.
So how to use a specific user-agent with ExoPlayer ? (it's Better if i can use MagicalExoPlayer).
The User Agent is provided to the Exoplayer while preparing the datasource for the player. Example:
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(context,"MY_USER_AGENT");
Since MagicalExoPlayer is a wrapper over Exoplayer, it has set the field to a constant. See the PublicValues.java class in the github repository. You can ask the author to expose a public setter to the field. Another way would be copy the library module to your project and change the value of User Agent in the PublicValues class. You'll need to change the build.gradle to compile the module. I would strongly advise to ask the author and not copy over the module unless it is necessary because you'll have to manually update the module afterwards.

How to use the twitter object in multiple fragments?

I'm using 'twitter4j' to make a twitter client app. and I want to bypass the Twitter object from the MainActivity.java to different fragments in my app to be used by them. what is the best way to do that?
I don't know if i can do it with 'Bundle' ?!
and when I tried to make a constructor for my fragment for example:
public Fragment_a(Twitter twitter)
{
//
}
i got a warning from eclipse saying that:
"Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle)"
thanks
You could implement the twitter object in your activity, and then just let your fragments grab it
twitterObject = ((MyActivity)getActivity()).getTwitterObject();
if (twitterObject != null)
{
// do something
}

Why is context passed to the constructor of Intent?

I am trying to understand why we pass a Context instance to the Intent constructor? Why isn't it enough to do new Intent(SomeActivity.class)? Does Android enforce some restrictions or what?
I was trying to look at the code but all I find is that it gets the package name.
Intent documentation
To identify an Activity in an Android application unambiguously, you need to have both, the name of the application (aka Android application package), and the full name of the activity (java package name + class name of activity class). These are exactly those two parameters you give in constructor. Context is used to get Android application package name, and the class to get full class name.
An Activity with the same full name can be used in two applications. If you do not provide Context, then Android won't know which application an activity belongs to.
As you guessed, the Context in the constructor is used to get the package name of the application.
Inferring the package name from the class as in SomeActivity.class.getPackage().getName() does not work in every case, as it could be different from the application's package name.

How do you tell the reference path when loading a class?

I'm trying to use JavaLoader to load a java (HttpAsyncClient) class into ColdFusion.
client = loader.create("org.apache.commons.HttpAsyncClient")
How do we know the reference that is org.apache.commons.HttpAsyncClient? I thought if you open the jar file and follow the directory structure, it will give you the reference path. But I don't think this is true.
I'm trying to use the HttpAsyncClient but I'm unable to load it:
client = loader.create("org.apache.commons.HttpAsyncClient") returns a class not found error.
Loader is a reference to JavaLoader, which loads Java classes into your CF server.
Rather than reinvent the wheel, why not try an existing tool like Mark Mandel's AsyncHTTP library?
Update: From the comments, that tool is ACF only. So you might try using the concrete class DefaultHttpAsyncClient as shown in the Asynchronous HTTP Exchange example.
I don't know ColdFusion. You probably have to specify the full path to the class, not just the package containing the class.
According to an example I found the full package and class name is this: org.apache.http.nio.client.HttpAsyncClient
You can also use the javadoc to find out the package and class names: http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/index.html
Getting something async going with an interface like this will probably be brutal. I would suggest trying the sync version first.
EDIT
I would try adapting this sync example to CF: http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientWithResponseHandler.java
When you instantiate HttpGet you have to pass extra parameters to init() as they do in this example: http://www.coldfusionjedi.com/index.cfm/2009/5/29/Generating-Speech-with-ColdFusion-and-Java

Context in android

i am a newbie.Please explain what all things are passed through a context.Like when constructing an object for the following class..
public class myclass{
public myclass (Context context){....}
}
You get a lot of possibilities to check for or change System or application properties.
You will find a detailed version of all the functions that are available with the context in the api documentation of android:
http://developer.android.com/reference/android/content/Context.html
So you will be able for example to start a service (to run part of the application in the background) through context.startService(Intent service). You'll need to pass an Intent (if you don't know what an intent is I would read the Dev Guide: http:**developer.android.com/guide/topics/fundamentals.html first. You could do that anyway, there are plenty of good descriptions and examples.).
Sorry for the crippled link, I'm not allowed to post more than one link per post...

Categories

Resources