how to view youtube video - java

I am finding great difficulties to view YouTube videos in my app.
here is my code:
package com.example.webvideo;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class WebVideo extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView vv = (VideoView) findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
mc.setAnchorView(vv);
vv.setMediaController(mc);
vv.setVideoURI(Uri.parse("http://youtu.be/2OIOOb-0t44"));
vv.start();
}
}
The emulator is showing an error that the video cannot be played.
What am I doing wrong? Am I giving the URL in a wrong format?

I would expect that the URI you need to give is to the actual media file to be played. I wouldn't count on redirects working either... and anyway that redirect you give seems to point to a YouTube web page, which I sure wouldn't expect the video player to be able to render.

http://youtu.be/2OIOOb-0t44 is certainly an invalid URL. Maybe you meant http://youtube.com/watch?v=2OIOOb-0t44?

The URL you are using is for the webpage where you can view it, not for the video itself. The embedding URL appears to be http://www.youtube.com/embed/2OIOOb-0t44, but I think it's HTML5, not flash... you may have to go old school and track down an AVI or MPEG file.

Related

How to embed WebView in Java in Android Auto

I'm new to Java and am currently developing an Android Auto app.
It's using WebView to display local HTML files.
I've tried many ways, but everything went wrong.
My App can start in Android Auto, but embedding WebView fails every time.
And the Internet is like empty on this subject.
Here is what I'm trying currently:
package com.example.webviewandroidauto;
import android.webkit.WebView;
import androidx.annotation.NonNull;
import androidx.car.app.CarContext;
import androidx.car.app.Screen;
import androidx.car.app.model.Template;
public class StartScreen extends Screen {
public StartScreen(CarContext carContext) {
super(carContext);
}
#NonNull
#Override
public Template onGetTemplate() {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView mywebview = (WebView) findViewById(R.id.webView);
mywebview.loadUrl("file:///android_asset/myresource.html");
}
}
Can someone please help me out?
Thank you!
Proof that my app starts in Android Auto: https://i.stack.imgur.com/OXC80.png

i'm trying to add music to my game but it stops after

so i am very new to scripting and this is my first application i build followed a tutorial but now i want to add music to it found a small code online but the music stops after 1 min while music should last 7 mins?
anyone here who can help me with this?
and also how could i make a song loop?
i did get this " MediaPlayer.setLooping(true); " but i don't know if it's gonna work so wanted to ask with this question too :p
Thanks already for you'r time.
i've looked for similar questions and used those answers but did not work, are i did implement it wrong since i am very new to this..
My Code.
package com.example.myapplication;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
Constants.SCREEN_WIDTH = dm.widthPixels;
Constants.SREEN_HEIGHT = dm.heightPixels;
setContentView(new GamePanel(this));
MediaPlayer Music = MediaPlayer.create(MainActivity.this,R.raw.music);
Music.start();
MediaPlayer.setLooping(true);
}
}
I would like music to play and continuesly looping it does not need to stop.
You need WakeLock or background service for playing music in sleep mode. I guess you can try keep your device alive (tap screen) and music would play.
1) Wakelock. Add permission to manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />
And call on Create method:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakelock= pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getCanonicalName());
wakelock.acquire();
docs
2) Background service
docs
This is much more complicated, because you need to create notification etc. But you can try to follow android docs.
3) Read this doc. Here is guide with examples with wakelock and service.

WebView won't execute site commands

I have a WebView that loads this WebSite
If you try to put an identification and click out of the box, it shows a little loading and tells if the identification is correct it shows a virtual keyboard within the site, not a device keyboard, if its incorrect it shows a error message, like this
Site showing error
However, if i put the same website on my WebView, it loads the site perfectly, but it won't execute these commands, if i put any identification and click out it doesn't shows the loading, it won't do anything.
Funny part is, if i do the same WebView in Xcode for iPhone, it does everything normally, even the commands, it checks for the identification, etc...
What i've tried
Turning on/off:
Javascript
Plugins(even deprecated)
My code:
package com.example.viskee.webview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wbAba = (WebView) findViewById(R.id.wbAba);
wbAba.setWebViewClient(new WebViewClient());
wbAba.getSettings().setJavaScriptEnabled(true);
wbAba.loadUrl("https://ib.rendimento.com.br");
}
}
EDIT:
If i use
wbAba.setChromeClient(new ChromeClient);
it works fine, however i don't want to use ChromeClient because it adds an address bar, etc... I want my WebView to be FullScreen, show only the website
Have you try this:
WebView wbAba = (WebView) findViewById(R.id.wbAba);
wbAba.setWebViewClient(new WebViewClient());
wbAba.getSettings().setUseWideViewPort(true);
wbAba.getSettings().setLoadWithOverviewMode(true);
wbAba.getSettings().setJavaScriptEnabled(true);
wbAba.loadUrl("https://ib.rendimento.com.br");
and one final thing, don't forget to set internet permission on manifest.xml file:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Let me know, if this doesn't work.
I found a solution, all i had to do was:
wbAba.getSettings().setDomStorageEnable(true);

How to use a button to within a app to display the phone's contact list

Hello everyone I am completely new to programing no experience at all so please treat me as a 2 year old when you respond. I have alot of time on my hands as I sit in an office with nothing to do so I figure I should do something productive, and since I have a android tablet I figure I'll try to learn how to make apps for it. This is my first attempt and I'm getting an error in the .java portion of the code, I've been reading and reading on various sites and can't seem to get a clear understanding of what I need to do to correct it. PLEASE HELP
Now down to business, in the app I am trying to learn how to display an image its that simple, I'm taking baby steps. So I started a project, read a few tutorials on how to code ImageView and then I got stuck. I have the error that says "The public type TestImages must be defined in its own file" so I ask how to define/create this file? where to put it?
Here is the .java portion of the code where the error lies.
package com.example.myproject;
import com.example.myproject.util.SystemUiHider;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class TestImages extends Activity {
/**Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
ImageView image = (ImageView) findViewById(R.id.test_image);
}
}
Please help, and if you know of any other resource out there that can help a newbie/dummy like myself who don't know anything about coding and need every detail explained please point me in that direction. I've tried the Android Developers site but the information presented on there are not explained in detail it looks as if they expects everyone who tries to do this have a background in coding and I don't. Oh and please don't discourge encourage, because I'm eager to learn.
use on click listener on the button and then check this
https://github.com/android/platform_packages_apps_contacts

Creating Background Image for Android App - Java

I'm just getting started with android development and I need help with background images. I want to be able to have a background image and then overlay other items (buttons, text, etc.) on top of that background with a layout. I used a LinearLayout just for the sake of being simple and because I don't know what's best for me at the moment.
Anyways, I can't get an image to display using the following code:
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.*;
public class NewGameActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundDrawable(Drawable.createFromPath("/assets/images/androidBackground.png"));
this.setContentView(ll);
}
}
ll.setBackgroundResource(R.drawable.image_name);
http://developer.android.com/reference/android/view/View.html#setBackgroundResource%28int%29
This is the preferred way of accessing drawable resources. The image_name is a png image in your drawable or drawable-mdpi folder.
yes if you are using XML then find the id of that linearlayout like
ll=(LinearLayout)findViewById(R.id.linear1)
Then set its background as
ll.setBackgroundResource(R.drawable.image_name);
else here as your code given here you can directly go for
ll.setBackgroundResource(R.drawable.image_name);

Categories

Resources