Android WebView resize default audio player - android

I am using webview in my application where i am giving it a audio url.
Its working absolutely fine in some devices. see below screenshot:
But in some device its not looking user friendly.
Is there any way to set the size of webview audio player?
Update: Code snippet:
// open DOI activity
Intent podcastIntent = new Intent(mContext, DoiWebActivity.class);
podcastIntent.setAction(Constants.INTENT_VIEW_LINK);
podcastIntent.putExtra(Constants.EXTRA_DOI_LINK_URL, podcasts.get(0).getPodcastLink());
podcastIntent.putExtra(Constants.CALLING_FRAGMENT,Constants.CALLING_FRAGMENT_PODCAST_LISTING);
podcastIntent.putExtra(Constants.EXTRA_PODCAST_NAME,mContext.getString(R.string.podcast));
mContext.startActivity(podcastIntent);
//Called from podcastListingFragment : podcast name
if (bundle != null && bundle.containsKey(Constants.CALLING_FRAGMENT) &&
bundle.containsKey(Constants.EXTRA_PODCAST_NAME)) {
String title = bundle.getString(SilverChairConstants.EXTRA_PODCAST_NAME);
}
if (Utility.isNetworkAvailable(mActivity)) {
/**
* Showing external link.
*/
mWebView.loadUrl(mIntentText);
} else {
networkNotAvailable();
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
tools:context=".fragments.WebFragment">
<WebView
android:layout_below="#+id/toolbar"
android:id="#+id/wv_webcontainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--progress bar-->
<include layout="#layout/silverchair_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#id/wv_webcontainer"
android:layout_alignBottom="#id/wv_webcontainer"/>
<include layout="#layout/empty_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerInParent="true"
/>
</RelativeLayout>

add this line in your code where you are initializing your webview. mWebView.getSettings().setPluginState(WebSettings.PluginState.ON);
If you have already done this I would recommend you to use MediaPlayer for straming.
MediaPlayer mediaPlayer = MediaPlayer.create(this, Uri.parse("http://vprbbc.streamguys.net:80/vprbbc24.mp3"));
mediaPlayer.start();
See this answer for more details about MediaPlayer streaming.

Related

How to enable video playing in full screen in my Android Webview

I tried to enable graphics acceleration but didn't worked for me
Here is my code
Click here to see
I am getting this error too click here too see
Please help me I am stuck
Can someone help me with the code
Probably you should use VideoView for this purpose. It allows to view videos from some url in fullscreen.
First, create a player activity xml file with VideoView:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<VideoView android:id="#+id/video_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
In your AndroidManifest.xml for your PlayerActivity set screenOrientation attribute to landscape:
<activity android:name=".PlayerActivity"
android:screenOrientation="landscape" />
Finally, in your PlayerActivity.kt set fullscreen flags and load your video url (or local file - check documentation):
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Fullscreen mode
requestWindowFeature(Window.FEATURE_NO_TITLE)
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
setContentView(R.layout.activity_player)
// Load your video file
video_view.setVideoURI(Uri.parse(intent.getStringExtra(URL_TO_VIEW)))
video_view.setMediaController(MediaController(this))
video_view.requestFocus(0)
video_view.start()
}
Or if you write in Java, in your PlayerActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Fullscreen mode
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_player);
// Load your video file
VideoView videoView = findViewById(R.id.video_view);
videoView.setVideoURI(Uri.parse(getIntent().getStringExtra(URL_TO_VIEW)));
videoView.setMediaController(new MediaController(this));
videoView.requestFocus(0);
videoView.start();
}

play Youtube with webview carshes

I'm trying to play YouTube video in my application using WebView.
But when I click "Play" the application crashes.
This is my code:
public class Youtube extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_video_item);
WebView videoView = (WebView)findViewById(R.id.videoView);
TextView tvTitle = (TextView) findViewById(R.id.VideoTitle);
WebSettings videoViewSettings = videoView.getSettings();
videoViewSettings.setJavaScriptEnabled(true);
videoViewSettings.setPluginState(WebSettings.PluginState.ON);
videoView.setWebChromeClient(new WebChromeClient());
String youtubeID ="XSzE2LLFluE";
tvTitle.setText(youtubeID);
videoView.loadUrl("http://www.youtube.com/embed/" + youtubeID +"?autoplay=1&vq=small");
}
}
And this is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/videoView"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_below="#+id/VideoTitle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextView
android:id="#+id/VideoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/imgdesc"
android:layout_marginRight="10dp"/>
</RelativeLayout>
I'm testing the project with Nexus 5 on Android Studio emulator.
You have to add the two lines of code:
webView.getSettings().setPluginsEnabled(true); webView.getSettings().setJavaScriptEnabled(true);
this will solve the problem. Try it and tell.
I found the issuse.
Beacuse I did the tests on Emulator without Youtube app its crashes.
On a device with Youtube app its working fine.
Now its crasheswhen i click on 'Full screen' button

How to display two videos simultaneously with surface view

I am a beginner in android development.
I was trying to display a video with surface view using media codec for which i am successful.
Now I want to add one more video at run time which has to be displayed or hidden as per the wish of the user or to switch between the two.
Can I have some suggestions for the same...
Thanks......
Try this
public class CustomPictureActivity extends Activity {
/** Called when the activity is first created. */
VideoView vd1,vd2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vd1=(VideoView) findViewById(R.id.v1);
vd2=(VideoView) findViewById(R.id.v2);
vd1.setVideoURI(Uri.parse("/mnt/sdcard/file.mp4"));
vd1.setMediaController(new MediaController(this));
vd1.requestFocus();
vd1.start();
vd2.setVideoURI(Uri.parse("/mnt/sdcard/android.mp4"));
vd2.setMediaController(new MediaController(this));
vd2.requestFocus();
vd2.start();
}
}
Your xml code should be like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<VideoView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:id="#+id/v1"/>
<VideoView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:id="#+id/v2"/>
</LinearLayout>
May this will help you.

How to return data from another activity, back button only way to return from launced activity

I have an lg g-slate that has two rear cameras to take stereoscopic (3D) pictures/video. In my app I am trying to call the 3D video recorder application that came preinstalled on the device. I was able to successfully start the app with the following:
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("com.lge.stereo.camcorder");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivityForResult(i, ACTION_TAKE_VIDEO);
} catch (PackageManager.NameNotFoundException e) {
After recording a video with the app there is no way to return from the app besides using the back button. So, in onActivityResult() the result code is RESULT_CANCELLED and there is no way for me to get any data returned from the intent. I would like to get the Uri of the file where the video was saved but I am not sure how I would be able to do this since using the back button is the only way to return from the app.
I have used the following code before to record 2D video:
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(takeVideoIntent, ACTION_TAKE_VIDEO);
After recording a video a dialog popups where you can select "Ok" to return to your application and get the file URI in onActivityResult.
Is there any way to launch the 3D video recorder so that I can get a result back?
Any help or suggestions would be greatly appreciated? Thanks
Open camera app within your app only on a custom layout. Where you can have two buttons for save and discard video. As user press to save video save it on a predefined location and you will get to know the path of video as you have already defined it yourself and when user press cancel video discard the location and cancel video or just override back button functionality.
for e.g.
Inside your onCreate() method , get instance of camera
private Camera myCamera;
// Get Camera for preview
myCamera = getCameraInstance();
private Camera getCameraInstance() {
// TODO Auto-generated method stub
Camera c = null;
try {
// attempt to get a Camera instance
c = Camera.open();
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
// returns null if camera is unavailable
return c;
}
now create Surface view for camera
private MyCameraSurfaceView myCameraSurfaceView;
myCameraSurfaceView = new MyCameraSurfaceView(this, myCamera);
FrameLayout myCameraPreview = (FrameLayout) findViewById(R.id.videoview);
myCameraPreview.addView(myCameraSurfaceView);
myButton = (Button) findViewById(R.id.mybutton);
myButton.setOnClickListener(myButtonOnClickListener);
flashOff = (RadioButton) findViewById(R.id.flashoff);
flashTorch = (RadioButton) findViewById(R.id.flashtorch);
and create your layout like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/videoview"
android:layout_width="720px"
android:layout_height="480px" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/mybutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="REC"
android:textSize="12dp" />
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RadioButton
android:id="#+id/flashoff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="OFF"
android:textSize="8dp" />
<RadioButton
android:id="#+id/flashtorch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Torch"
android:textSize="8dp" />
</RadioGroup>
<Button
android:layout_marginTop="10dp"
android:id="#+id/btn_next"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="NEXT"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Well I found a suitable solution. I get the current time in milliseconds before calling startActivityForResult, then I get the current time in onActivityResult. I know the directory the 3D recorder saves the videos so I iterate through the files in the directory and check the last modified times of each file. If the last modified time is between the start and end times of using the 3D recorder app I know that this was the video that was recorded.

Android: Button on an WebView-Element (.png-Image)

I need to create a floorplan which can be scrolled and zoomed.Some sections of this plan shall be linked to other activities (ie. kitchen). My idea was to lay a simple button on the kitchen-region. With a click on it you can get to the kitchen activity.
Because of scrolling- and zooming-requirement I created a WebView containing my floorplan:
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".FloorplanActivity"
xmlns:android="http://schemas.android.com/apk/res/android">
<WebView
android:id="#+id/webViewFloorplan"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<Button
android:id="#+id/buttonKuehltheke"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</WebView>
</LinearLayout>
But now I'm not able to lay a button on a specific region on the floorplan. The Button always appears in upper left corner of my floorplan.
MainActivity-Code:
WebView wvFloorplan = (WebView) findViewById(R.id.webViewFloorplan);
wvFloorplan.getSettings().setBuiltInZoomControls(true);
wvFloorplan.loadUrl("file:///android_asset/services_plan.png");
What needs to be done to set the button to every postition on my WebView (Floorplan)?
Thx Arne
If you are going to be scrolling and zooming this WebView, the coordinates for the kitchen section are going to be dynamic, so positioning a button over it will be difficult.
I would suggest overriding WebView.shouldOverrideUrlLoading. This way you can use html to create a link to the kitchen activity.
wvFloorplan.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("act:")) {
Intent intent = new Intent( <kitchen activity> );
startActivity(intent);
return true;
}
return false;
}
});
You will have to write an html wrapper around the image to map parts of your images to different links but now the coordinates will be static. Instead of http urls use Href="act:kitchen"

Categories

Resources