What is the easiest way to get the title of a single YouTube video in Android? I'm using the Android YouTube API to play video's but they don't return a title cause it only supports playback options. The only thing I need is to display the title of the video, I obviously have the video ID itself but I don't have a clue where to start.
Things I looked at:
YouTube Data API
JSOUP(I can see the tag but don't know how to get it)
For some reason I find it hard to understand how to use them and this seems like a really simple action to me. I'm curious if there is an easier option or if there is someone who can help me get on the right track with this.
You can use this code for this purpose.
public class SimpleYouTubeHelper {
public static String getTitleQuietly(String youtubeUrl) {
try {
if (youtubeUrl != null) {
URL embededURL = new URL("http://www.youtube.com/oembed?url=" +
youtubeUrl + "&format=json"
);
return new JSONObject(IOUtils.toString(embededURL)).getString("title");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
Source
Thank you #Castiblanco
To those who are lazy to find the jars and imports
http://commons.apache.org/proper/commons-io/
http://www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm
import java.net.URL;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
Related
I have a Xamarin Forms project in which I need the user to be able to "load" an image. I can already press a button and search for a file using FilePicker like this:
async void OnUpload(object sender, EventArgs e)
{
try
{
FileData filedata = await CrossFilePicker.Current.PickFile();
// the dataarray of the file will be found in filedata.DataArray
// file name will be found in filedata.FileName;
//etc etc.
}
catch (Exception ex)
{
}
}
What I would need now is to copy that "filedata" (the image) to the resource folder of the project in order to access to the file easily. I have tried:
await CrossFilePicker.Current.SaveFile(filedata.FileName);
but it doesn't save any file into the project folder.
Moreover, I only need it to work on UWP and Android.
The SaveFile method saves it in a very specific folder.
If you want to save it somewhere of your choosing you have to implement it with the DependencyService. IO operations are very specific to the OS, so are the filepaths. I will give you a simple example for you to build on.
Start with defining an interface in your shared code, like so:
public interface IFileManager
{
void SaveFile(Stream stream);
}
Of course, it can have other methods as well, or extra parameters if you would like to specify things like filename, that is up to you. You would also probably like some kind of return value to know what happened.
Now, per platform implement this interface. For example for Android, it could look like this:
[assembly: Xamarin.Forms.Dependency (typeof (FileManager_Android))]
public class FileManager_Android : IFileManager
{
public void SaveFile(Stream stream)
{
var dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);
string filename = System.DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg";
string filePath = System.IO.Path.Combine(dir, name);
try
{
System.IO.File.WriteAllBytes(filePath, imageData);
}
catch (Exception ex)
{
System.Console.WriteLine(e.ToString());
}
}
}
(Saving code inspired by this link)
This will take the stream and save it to a path of your choosing.
For UWP you will need to implement it as well, which is quite similar, except for the implementation of SaveFile. As far as I know there is no plugin yet which makes this easier for you. There is PCLStorage, but this plugin only seems to work with text files. You could still look into it for inspiration though.
I'd like to know if it's possible to play a youtube video inside an AIR app using AS3 code.
It seems that youtube API is deprecated...
If anybody knows a way or a good tutorial.
Thx
----------------EDIT
I've tried this code :
var wt:uint;
var ht:uint ;
var webview:StageWebView ;
var rect:Rectangle;
var url:String;
url = "https://www.youtube.com/watch?v=hFupHx5G44s";
trace(url);
wt = this.stage.stageWidth;
ht = this.stage.stageHeight;
webview = new StageWebView();
rect = new Rectangle(0,300,wt,ht/2);
webview.stage = this.stage;
webview.viewPort = rect;
webview.loadURL(url);
But it's loading the all page of youtube (youtube video + the recommended videos..) with a scroller on the right.
I'd like to load only the video (not all the youtube page).
Edit :
I'd like to load only the video (not all the youtube page).
Use this link format :
url = "https://www.youtube.com/v/hFupHx5G44s";
for example, choose a format like below :
https://www.youtube.com/v/hFupHx5G44s - use the /v/ to get a SWF version
https://www.youtube.com/embed/hFupHx5G44s - use the /embed/ to get a HTML5 version
It's for an ANDROID AIR app.
You should have mentioned that detail in the question. You could try adding
Security.allowDomain("www.youtube.com");
Security.loadPolicyFile("https://www.youtube.com/crossdomain.xml");
//////////# End of Edit
Why not use stageWebView to load the embed (HTML5) player as web page?
The YouTube AS3 API being deprecated likely means you cannot create your own user interface or use features like search. You can re-create the search function easily though (load "search results" source into String and extract links by parsing the source code).
Anyways, I just tried this code below and it worked for an AIR Desktop App.Maybe you can use it as a starting point...
package
{
import flash.display.MovieClip;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.events.*;
import flash.system.*;
public class Youtube_AIR_code extends MovieClip
{
public var YT_Loader : Loader;
public var my_VIDEO_ID : String = "hFupHx5G44s"; //# the YouTube ID
public function Youtube_AIR_code ()
{
Security.loadPolicyFile("http://www.youtube.com/crossdomain.xml");
YT_Loader = new Loader();
YT_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, YT_Ready );
YT_Loader.load( new URLRequest("http://www.youtube.com/v/" + my_VIDEO_ID) );
}
function YT_Ready (e:Event) : void
{
trace("YouTube SWF :: [status] :: Loading Completed");
//addChild(YT_Loader);
//# Wait for Youtube Player to initialise
YT_Loader.content.addEventListener("onReady", on_YT_PlayerLoaded );
}
private function on_YT_PlayerLoaded (e:Event) : void
{
//# Check Original Width/Height - Scale it proportionally
trace( "YT_Loader content Width : " + YT_Loader.content.width );
trace( "YT_Loader content Height : " + YT_Loader.content.height );
//# Testing changed size and position
YT_Loader.width = 320; YT_Loader.height = 240;
YT_Loader.x = 30; YT_Loader.y = 100;
addChild(YT_Loader); //# can add to stage only
}
} //# End Public Class
} //# End Package
Have you seen the Video Player ANE from My Flash Labs?
http://www.myflashlabs.com/product/video-player-ane-adobe-air-native-extension/
I have been trying to load an OWL file that I made in Protégé. I import OWL API 3.4.3 to my project and also passed the sample.owl file to raw folder, but when I try to load the OWL file, it doesn't work. There was no error but I am just getting this message
unfortunately, sampleproject has stopped
Here is the section of code am using. When I try the code in a standard Java environment it works without a problem.
OWLOntology localOntology = null;
int rID = resources.getIdentifier("com.example.cammclient1:raw/"+"sample", null, null);
InputStream input = resources.openRawResource(rID);
OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
ontology = manager.loadOntologyFromOntologyDocument(input);
try {
for (OWLClass cls : localOntology.getClassesInSignature()) {
Log.d("class in the ontology", ((CharSequence) cls).toString());
}
TV1.setText("reading classes...............");
}
catch (Exception e) {
TV1.setText("Not successfull");
}
You are casting OWLClass instances to CharSequence and then calling toString() on it.
This will cause ClassCastException to be thrown - an OWLClass is not a string.
Just use cls.toString() instead, you will have the same result.
You are also swallowing the exception in the catch block. That's not helpful in diagnosing the issue, as it hides information by just saying "Not successful" without providing more information.
I have a Android Actionscript project that I am trying to incorporate sound in, but having a problem that I cannot figure out. I have the sound file in the same directory as my class file and my .xfl file. I am trying to start the music file as soon as the application starts via class reference, but am getting the same error everytime:
Error #2044: Unhandled IOErrorEvent:. text=Error #2032: Stream Error.
at playAudio()[C:\pathtoerror\playAudio.as:9]
at networkScores/frame1()[networkScores::frame1:7]
Here is my class file:
package
{
import flash.media.Sound;
import flash.net.URLRequest;
import flash.media.SoundChannel;
public class playAudio extends Sound
{
public var music:Sound = new Sound(new URLRequest("mathHomeSong.mp3"));
public var sc:SoundChannel;
public var num:int;
public function playAudio(controlNum:int)
{
num = controlNum;
if(num == 1)
{
sc = music.play();
}
else if(num == 2)
{
}
}
}
}
Here is my call from the timeline:
import playAudio;
var playMusic:playAudio = new playAudio(1);
Just figured this out, I had to put a link to the mp3 file in my server, instead of putting it in my directory.
Sorry about the confusion
I posted too soon. I just figured out my question, figures. Ok, so what I did not do is post this file on my server and with the URLRequest(""); I had to put a link to my file on my server, not linki
The answer to this question is that I forgot to add this music file to a server and then link it via hyper link. For example http://mysite.com/audio.file
Sorry about this
thanks
I don't know if you already tested the Google IO application, but there is a cool feature displaying all the tweets including Google IO hashtags.
I really would like to offer the same feature to my users.
I can do something similar using the API, but I would have to create a custom listview, parsing XML/JSON feeds and that's quite overcomplicated! and of course this list will not update automatically and be a livefeed.
In the application, I have just seen that when I turn off wifi, This is indeed a webview with this url:
http://www.google.com/search?%20tbs=mbl%3A1&hl=en&source=hp&biw=1170&bih=668&q=%23io2011&btnG=Search
Here is a screenshot of the app and the same url in the browser
High resolution picture: http://cl.ly/3q1r0c2J3H163E3G2p2X
But using this url in a webview display only a google search, and does not offer same feature.
I know this app will certainly be opensources, but i am so negative about "within next days" that google promise.
We are still waiting for the Twitter app source code!
If you wait 'til after the conference is over, you'll find the source code for the app here. You'll also find last year's application's source code there.
Update:
Just viewed the source code, and you're almost right. It's a webview with this URL: http://www.google.com/search?tbs=mbl%3A1&hl=en&source=hp&biw=1170&bih=668&q=%23io2011&btnG=Search so it just seems you put %20 in there by accident maybe.
Code:
public static final String EXTRA_QUERY = "com.google.android.iosched.extra.QUERY";
public static final String CONFERENCE_HASHTAG = "#io2011";
private String mSearchString;
//onCreate()
final Intent intent = BaseActivity.fragmentArgumentsToIntent(getArguments());
mSearchString = intent.getStringExtra(EXTRA_QUERY);
if (TextUtils.isEmpty(mSearchString)) {
mSearchString = CONFERENCE_HASHTAG;
}
if (!mSearchString.startsWith("#")) {
mSearchString = "#" + mSearchString;
}
//onCreateView
mWebView = (WebView) root.findViewById(R.id.webview);
mWebView.post(new Runnable() {
public void run() {
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
try {
mWebView.loadUrl(
"http://www.google.com/search?tbs="
+ "mbl%3A1&hl=en&source=hp&biw=1170&bih=668&q="
+ URLEncoder.encode(mSearchString, "UTF-8")
+ "&btnG=Search");
} catch (UnsupportedEncodingException e) {
Log.e(TAG, "Could not construct the realtime search URL", e);
}
}
});
Probably implemented with Loaders API with throttling.
Impatiently waiting for source code as well.