android youtube api not playing videos as expected - android

i am trying to embed a youtube api for android, everything for good except the fact, that video play for a one second only and then stop automaticly. but when i am going to the fullScreenMode, the video playing as expected.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_show);
ivThumbnail = (ImageView) findViewById(R.id.ivThumbnail);
tv = (ImageView) findViewById(R.id.tv);
Intent i = getIntent();
screenCase = i.getIntExtra("screenCase", -1);
selectedVideoId = Splash.playlists[screenCase].getItems().get(0).getVideo().getId();
setProperTvImage();
createButtons();
ivThumbnail.setImageBitmap(Splash.playlists[screenCase]
.thumbnails[0]);
youtubeLV = (ListView)findViewById(R.id.lvPlaylist);
adapter = new MyYoutubeListAdapter(this, Splash.playlists[screenCase].getItems(), screenCase);
youtubeLV.setAdapter(adapter);
youtubeLV.setOnItemClickListener(this);
youtubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youtubePlayerView.initialize(DeveloperKey.DEVELOPER_KEY, this); //init Player
}
onInitializationSuccess
public void onInitializationSuccess(Provider arg0, YouTubePlayer _player,
boolean wasRestored) {
this.player = _player;
//player.setPlayerStyle(PlayerStyle.CHROMELESS);
if(!wasRestored){
playVideoAtSelection();
}
}
and finally playVideAtSelection methode
private void playVideoAtSelection() {
if(player != null){
Log.e("player ", "not null");
player.cueVideo(selectedVideoId);
}else{
Log.e("player ", "null");
}
}
Any suggestions?

Well, its a bit late for answer, but someone might stumble on this post and might get an insight.
I was facing the same issue and 1st option below solved my problem.
After a lot of search regarding the similar issue, I came across two things.
1) Check if your YoutubePlayerView (in activity XML) has padding option. Remove the padding if its there.
2) As per youtube Api documentation, the minimum size specified is 200 x 110 dp for YoutubePlayerView. Make suitable changes in size if that is the case.
Hope it helps somebody.

Related

How to Play Youtube Playlist Intent Videos in Full Screen

I am beginner in android and developing an app in which i am using YouTubePlayer API in order to show Youtube Videos of specific Playlist. I'm succeed in doing so. But what I want is that whenever the user selects any video from that playlist; the video should be played in Full Screen. Here is my code:
public class Main4Activity extends AppCompatActivity
{
Toolbar main_toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
main_toolbar=(Toolbar) findViewById(R.id.my_thirdtoolbar);
setSupportActionBar(main_toolbar);
getSupportActionBar().setTitle(R.string.my_tb_title);
getSupportActionBar().setIcon(R.drawable.tblogo);
OnClickButtonListener();
}
public void OnClickButtonListener()
{
Button youtubebtn = (Button) findViewById(R.id.button8);
youtubebtn.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String PLAYLIST_ID = "PLXRActLQ03oY_6AQb-5EMuKFYQA_fDE40";
Intent intent=YouTubeIntents.createOpenPlaylistIntent(Main4Activity.this,PLAYLIST_ID);
startActivity(intent);
}
}
);
}
}
You can use methods from YouTubeIntents. Here are some methods that you can use:
canResolvePlayVideoIntentWithOptions (Context context)
Checks if the YouTube application installed on the user's device supports the fullscreen and finishOnEnd optional parameters when resolving a play video intent.
If this method returns true, then your application can call the createPlayVideoIntentWithOptions(Context context, String videoId, boolean fullscreen, boolean finishOnEnd) method which creates an Intent that, when resolved, will start playing the video specified by videoId, within the YouTube application.
If this method returns false, then your application should call the createPlayVideoIntent(Context context, String videoId) method instead.
Additionally, please also check other implementations and try the suggested solutions in this SO post. Hope it helps!

Take a screenshot of a video in a webview android

I am working on a project where I save a screenshot in a WebView. API Level is 21. On a normal homepage it works absolutely fine, but when I visit youtube and watch a video, the video player returns as a black rectangle.
What I want to do is take a screenshot whenever the user touches the screen.
When I take a screenshot with Power & Volume Button it works perfectly. Is there an Intent which takes this kind of a screenshot? Ideally the screenshot would only contain the webview, but if it's the entire screen I can work with that too.
Any help is greatly appreciated.
Cheers,
Toby
Edit: some of my code
View rootView;
ValueCallback<String> callback = new ValueCallback<String>() {
#Override
public void onReceiveValue(String value) {
category1.setText(value);
}
};
String root = android.os.Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DCIM).toString(), filename, time, puretime, movementType = ""; //movementType is set according to MotionEvent.getAction()
File myDir = new File(root + "/saved_files/")
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
}
public class MyView extends View {
public MyView(Context context) {
super(context);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
calendar = Calendar.getInstance();
time = String.format("%02d:%02d:%02d.%03d", calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND),
calendar.get(Calendar.MILLISECOND));
puretime = String.format("%02d%02d%02d%03d", calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND),
calendar.get(Calendar.MILLISECOND));
if (movementType.equals("down")) {
try {
filename = puretime + ".xml";
webView.saveWebArchive(myDir+filename, false, callback);
} catch (Exception e) {
e.printStackTrace();
}
}
invalidate();
return true;
}
edit2:
apparently the problem for the black rectangles is that elements as the video are processed by the gpu and for a "normal" snapshot to work the program would need to calculate every frame which is too much work.
I think this will help
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
Bitmap screenshot = rootView.getDrawingCache();
Let me know once you try.
UPDATE:
If I have understood your comment correctly, you were not able to change path with saveWebArchive(filename).
Then you might want to use saveWebArchive(String,boolean,ValueCallback<String> callBack).
This will save file with the given name and you can use that file name in the callback function to do whatever you want to (like saving to external storage or anything).
Check here - saveWebArchive

How to play youtube video using Youtube Data api v3 Android?

Few days back I asked question Youtube API v2 not supported So as per youtube they have deprecated and all application and websites using this v2 api wont work at all. As they told to migrate to v3 there are any good example or documents for android. I have youtube url with video id. I just want to play videos using this api
You can use Video ID to play video...
Use YouTubePlayerView for that...
Let me show you a example which will makes everything clear....
below is a class which contains a YouTubePlayerView..
public class Play_youtube_video extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener
{
YouTubePlayerView video_player;
public static String VIDEO_ID = "";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// HIDE THE KEYBOARD
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
// TITLE BAR DISABLES AND FULL SCREEN IMPLEMENTATION
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_play_youtube_video);
try
{
System.out.println("youtube----VIDEO_ID---->" + VIDEO_ID);
video_player = (YouTubePlayerView) findViewById(R.id.youtubeplayerview_full_screen);
video_player.initialize(GlobalConstant.YOUTUBE_APIKEY, Play_youtube_video.this);
}
catch(Exception e)
{
e.printStackTrace();
}
catch(OutOfMemoryError e)
{
e.printStackTrace();
}
}
#Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult result)
{
GlobalUtills.showToast("Youtube player not found.", Play_youtube_video.this);
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored)
{
if( !wasRestored )
{
player.loadVideo(Youtube_VIDEO_ID);
// player.setFullscreen(true);
// player.play();
}
}
public void closeYoutube(View v)
{
Play_youtube_video.this.finish();
}
}
I am answering this question because after some frustration it was not on android api related issue but Gdata youtube api i was using was returning wrong results.
Now its fixed.

youtube video 400 network when try to play video

I am try to play video using youtube video API
When I put static ID means declare ID in file at that time code is working.
But when I try to fetch ID from getIntent() I also got ID but video not play.
I got There was problem with network.
Below is my code. Its working in this condition.
public String VIDEO = "QqnBjKnwCwE";
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(DEVELOPER_KEY, Videosshow.this);
#Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult error) {
Toast.makeText(this, "Oh no! " + error.toString(), Toast.LENGTH_LONG)
.show();
}
#Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
player.loadVideo(String.valueOf( VIDEO));
}
Now When I used
Bundle data = getIntent().getExtras();
VIDEO=data.getString("videourl");
youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
youTubeView.initialize(DEVELOPER_KEY, Videosshow.this);
not working I got 400 Network problem error
Check your id .I think there is space in id or youtube url either start or end .I have similar problem and solved by this .You should also try .It may be help for if you have setup all properly .
I figured out that the id I was sending was incorrect, it had an extra = (say =hdeFZ6nt9tc)which was not to be sent. The problem occurred as I was extracting it from the youtube's actual URL(say https://www.youtube.com/watch?v=hdeFZ6nt9tc). I hope even you too might have been sending the wrong id.
Double check it again even if you are pretty sure.
What I think is youtube must send a 404(which generally refers to "Page Not Found" on the web) for such requests.
I had same the problem but retrieving video id from url solved my problem.
String video_id=jsonObject.getString("id");
Thank You ritesh.

My app gets "Force close" on several devices

I have people complaining my application gets FC when they launch it (meanwhile others never had a single problem). Here is my full activity source. Since it happens on devices I don't own I can not fix it. From what they tell me it doesn't work on: Motorola Blackflip, Motorola Dext, Motorola CLIQ XT. Guess Motorola doesn't like my app after all...
Could it be that I allow a minSdkVersion="3"? I tested 1.5 on the emulator and worked fine...
Thank you in advance for your responses.
public class workit extends Activity implements OnClickListener {
Button yay;
Button yay0;
Button yay1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
yay = (Button) findViewById(R.id.gostart);
yay.setOnClickListener(this);
yay0 = (Button) findViewById(R.id.dontstart);
yay0.setOnClickListener(this);
yay1 = (Button) findViewById(R.id.exit);
yay1.setVisibility(ImageView.GONE);
ImageView inizio = (ImageView)findViewById(R.id.start);
inizio.setVisibility(ImageView.VISIBLE);
inizio.setBackgroundResource(R.drawable.start);
}
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == yay0) {
finish();
}
if (v == yay) {
ImageView inizio = (ImageView)findViewById(R.id.start);
inizio.setVisibility(ImageView.GONE);
WebView work = new WebView(this);
setContentView(work);
work.loadUrl("file:///android_asset/index1.html");
work.setWebViewClient( new work());
work.setBackgroundColor(0);
work.getSettings().setBuiltInZoomControls(true);
work.getSettings().setDefaultZoom(ZoomDensity.FAR);
}
if (v == yay1) {
finish();
}
}
private class work extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("exit.html")) {
// TODO: do what you have to do
finish();
}
view.loadUrl(url);
return true;
}
}
}
Your best bet is to ask somebody to send you the LogCollector output (in my experience, users are very happy to provide you information to debug problems. There are some really cool people out there). That should give you a callstack, and information on what kind of exception you triggered (NullPointerException, etc).
Next up - what are you building your app against? There should be an "Android x.x" entry in your project structure somewhere. If you're building something that is supposed to run on Android 1.5, then make sure you actually build against 1.5. You CAN build against 2.0 if you want, but if you need to use 2.0-specific functions, you'll have to encapsulate them properly. (This has been explained in detail on stackoverflow several times.)
On an unrelated note - I recommend more informative variable names. "yay0" doesn't mean anything to anyone who hasn't been working intimately with the code for a while.

Categories

Resources