Video Player not Working - android

I have create a method for playing a video in a video player and called that method on a button click, but whenever I was click on the button then the activity page blink for a half second but the player wasn't appear on the screen. Plz help me Is there neccessary to create view or a layout for a video player for playing a video in xml file. I havn't create this.
import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
public class OptionsmenuAct extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button play =(Button)findViewById(R.id.video);
Equity.setOnClickListener(new OnClickListener(){
public void onClick(View V){
videoPlayer("/Optionsmenu/assets/lic.3gp","lic",true);
}
});}
public void videoPlayer(String path, String fileName,boolean autoplay){
getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = new VideoView(this);
videoHolder.setMediaController(new MediaController(this));
videoHolder.setVideoURI(Uri.parse(path+"/"+fileName));
videoHolder.requestFocus();
videoHolder.start();
if(autoplay){
videoHolder.start();
}
}
}
Plz tell me how's it will worked.
I have store my video file into assets folder.

You can use VideoView. See example of usage here.
Adding VideoView to layout XML file is as any other component, for example (taken from android site):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<VideoView
android:id="#+id/surface_view"
android:layout_width="320px"
android:layout_height="240px"
/>
</LinearLayout>

Related

My activity is unable to resolve a symbol to the layout file

I have the following activity
package com.example.responsivelayout;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class DisplayText extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displaytext);
Intent intent = getIntent();
String message = intent.getStringExtra("com.example.responsivelayout.MESSAGE");
TextView tv = (TextView)findViewById(R.id.user_text);
tv.setText(message);
}
}
But the line setContentView(R.layout.activity_displaytext); gives error as "cannot resolve symbol". I have the following layout file as activity_displaytext.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.constraint.ConstraintLayout>
which is present in the layout/ folder. After trying all the efforts, i am clueless why i am getting this error. I tried project clean, make, eveything. Please help.
Make sure below import is not there
import android.R; //Resource class
It may be issue with import
Import should be
import com.example.responsivelayout.R;

Android application that downloads images from bing image search not having the correct image

I have an android application that has a button and an image view
when clicking the button the application goes to bing image search and search for images then get the url of the first image and make it the source of the image view so that it has the first image of the search
When I do the image search using the explorer (I use chrome) I find that the first image displayed in my browser is not the same displayed in the image view
when I search for (facebook.com logo) or (google.com logo) the image view is the same of the first image shown in the search result but if I change the search to (yahoo.com logo) the image view is the second
If I change the search to (outlook.com logo) it takes the 10th image of the search and if I search for (hotmail.com logo) it does not show an image related to hotmail at all
I use JSOUP library to parse the HTML of the web page of the search results
here is the xml of the project:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="arb.myapplication.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:layout_alignParentBottom="true"
android:layout_marginBottom="131dp"
android:id="#+id/imageView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="39dp"
android:layout_marginEnd="39dp"
android:layout_marginTop="11dp"
android:id="#+id/button3"
android:elevation="0dp" />
</RelativeLayout>
and this is the Java file of the project:
package arb.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
class DownloadIcon extends AsyncTask<Object, Object, Integer>
{
#Override
protected Integer doInBackground(Object... objects) {
Connection connecion= Jsoup.connect("http://www.bing.com/images/search?q=outlook.com+logo");
try {
Document document=connecion.get();
Elements element=document.select("img.mimg");
String url=element.get(0).absUrl("src");
InputStream inputStream= new URL(url).openStream();
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
publishProgress(bitmap);
} catch (IOException e1) {
publishProgress(-1);
}
return 0;
}
#Override
protected void onProgressUpdate(Object... values) {
ImageView imageView=(ImageView) findViewById(R.id.imageView);
Bitmap bitmap=(Bitmap) values[0];
imageView.setImageBitmap(bitmap);
}
#Override
protected void onPostExecute(Integer integer) {
Toast.makeText(getBaseContext(),"done",Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button3=(Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DownloadIcon().execute();
}
});
}
}
Help me please

How to get video stream from H.264 IP Encoder from a Camera

I am trying to get a video stream of a normal camera connected to a H.264 IP encoder on my Android device. I have tried using webView which get me the login screen but I don't get the stream after logging in.
The encoder has a Log in screen and later opens a html page which has a bunch of options and the video in the middle like youtube.
I want to receive just the video and not the entire HTML page on the android device.
I have even tried using SurfaceView which shows a blank screen.
I tried implementing this http://ijoshsmith.com/2014/01/25/video-streaming-from-an-ip-camera-to-an-android-phone/ but didn't work either.
activtiy_main.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#000000"
tools:context=".MainActivity" >
<VideoView
android:id="#+id/myVideo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
MainAcitvity.java
package video_stream.telesystem.magnum.videostream;
import android.app.Fragment;
import android.content.Context;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.app.Activity;
import android.util.Base64;
import android.webkit.WebChromeClient;
import android.webkit.WebHistoryItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends Activity{
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
setContentView(R.layout.activity_main);
VideoView vidView = (VideoView)findViewById(R.id.myVideo);
MediaController vidControl = new MediaController(this);
String vidAddress = "http://192.168.0.10:554/Login.htm";
Uri vidUri = Uri.parse(vidAddress);
vidView.setVideoURI(vidUri);
vidView.start();
vidControl.setAnchorView(vidView);
vidView.setMediaController(vidControl);
}
}

Android-background change

So basically what I want to do is to change the background of the application when integer A value is greater than 50. I've tried using:
setBackgroundResource(R.drawable.imagename);
but it doesn't work.
I would really appreciate your help.
UPDATE:
My code:
package com.example.highhope;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
runOnUiThread(new Runnable() {
#Override
public void run() {
view.setBackgroundResource(R.drawable.i0);
}
});
}
});
}
}
My layout:
<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: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="com.example.highhope.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="90dp"
android:text="Button" />
So for starters I'm just trying to change application background on button click and I've tried everything and nothing seems to work.
You can make that with a Selector, Check this tutorial.
it's pretty easy, you just have to:
Add the images to your app resources
Create your background_button.xml (selector in the tutorial) in the drawable carpet
Set the background_button as your button background in your layout (android:background="#drawable/background_button")
Set an onClickListener to your button
Test it!.
When you do it this way, the background of your button will change with no need of handle it programmatically.
Are you making the background change in the UI thread? Try setBackgroundResource in the UI thread like below.
runOnUiThread(new Runnable() {
#Override
public void run() {
view.setBackgroundResource(R.drawable.bg);
}
});
Also, if you are using a Fragment. you would have to put getActivity(). in front of runOnUiThread.

how to write a simple video player on android?

I have code for simple audio player on android. But it is not working for videos. Please guide me to write a simple video player.
The code of audio player is
package com.example.helloplayer;
public class HelloPlayer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MediaPlayer mp = MediaPlayer.create(this, R.raw.file);
mp.start();
}
}
For making a video player you will have to make use of video view. a sample is shown below
Layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<VideoView android:id="#+id/videoView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
Here i am playing a video stored in "resource/raw" folder , "one" is the name of video file,you can replace it with name of your video file .also make sure that you are going to play an android supported video format
VideoplayerActivitvity
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
public class VideoplayerActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VideoView videoView =(VideoView)findViewById(R.id.videoView);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.one);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
}
}

Categories

Resources