Video Streaming in android using webview - android

In my application I want to stream video from url. Video is playing perfectly.When user click on that webview its redirect to video's url and showing an alertdialog "Complete action Using" where user can download that video by clicking the option Internet.I want to restrict that means user only watch that video but can not download that. I want user to watch that video using android default player. Thanks in advance.
public class PlayVideo extends Activity {
private String vUrl, vName;
private WebView webView;
Button back, home;
TextView videoName;
Vibrator vibrator;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// For full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.videoplay);
webView = (WebView) findViewById(R.id.webView01);
back = (Button) findViewById(R.id.bBack);
home = (Button) findViewById(R.id.bHome);
videoName = (TextView) findViewById(R.id.tvVidName);
// for vibration when a button clicked
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// getWindow().setFormat(PixelFormat.TRANSLUCENT);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
vibrator.vibrate(40);
finish();
}
});
vName = getIntent().getStringExtra("videoname");
videoName.setText(vName);
vUrl = getIntent().getStringExtra("vid_url");
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.getSettings().setJavaScriptEnabled(true);
Log.i("fired url", vUrl);
webView.loadUrl(vUrl);
}
}

You can do that by launching a specific application for streaming the video.
BUT NOTE:
users with only 1 suitable video app, will not see the dialog.
Also,
users that have set a default video app, will not see the dialog
either.
Hence, I would not limit myself, by launching a specific video application, and instead let the android users have a nice android experience.
Should you have very good reasons to launch a specific video player, you can use:
try {
Intent intent = new Intent("com.mxtech.videoplayer.ad"); // Will launch MX player
intent.setDataAndType(Uri.parse("your_path"), "video/*");
startActivity(intent);
} catch(ActivityNotFoundException e){
// the app mxplayer was not found...
Toast.makeText(this, "mx player is not installed", Toast.LENGTH_SHORT).show();
}

Related

How to use Android Buttons for controlling YouTube video playback in WebView?

I have two buttons and a webview in a relative layout. The WebView will always load youtube.com .I want to control the youtube's video playback i.e play or pause using my own buttons instead of the inbuilt youtube play/pause buttons. Is it even possible ? do we need to use Javascript / HTML5 or it can be Done using Youtube API ? Suggest me some solution or any other alternative.
This is how the actual app looks
Thanks a lot .
public class WebViewTube extends AppCompatActivity {
WebView webView;
Button play, stop ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view_tube);
play = (Button)findViewById(R.id.playTube);
stop = (Button)findViewById(R.id.stopTube);
webView = (WebView)findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("http://www.youtube.com/");
play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// No idea What to write here
}
});
stop.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
// No idea What to write here
}
});
}
}
The first thing you have to do is to load an embedded youtube player in your WebView as described here. Dont forget to include enablejsapi=1.
Then make your android buttons invoke some javascript to pause, play, whatever, using
webView.loadUrl("javascript:playVideo()");

Multiple pages in Eclipse android application

i am new in this space i am working with mini app.
i have Animal images in Main template and want when click for example Dog image it must switch another page and there will be dog description and image gallery.
so i need to know how to create multiple pages and redirect it.
this is my app.
Thanks ...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//////////////////////// Dog
Button button3 = (Button)this.findViewById(R.id.button3);
final MediaPlayer dog = MediaPlayer.create(this, R.raw.dog);
button3.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// If the music is playing
if(dog.isPlaying() == true)
// Pause the music player
dog.pause();
// If it's not playing
else
// Resume the music player
dog.start();
}
});
You must create differents activity for different pages.
And use Intent to change page.
Example :
Intent intent = new Intent(MainActivy.this, OtherActivity.class);
startActivity(intent);

how to make a videoview NOT started automatically after prepared

I have created a videoview with a standard media controller on it. I was able to play the video by calling start() method in setOnPreparedListener, so the video will play automatically when it finished preparing it self.
However, what i want to do is to make the video stand-by (NOT playing automatically), so the user need to tap/click/touch the videoview to start the video.
I've done some googling, and i also tried to setOnTouchListener on my videoview and calling the start() method there. But the result is unexpected (and confusing as well), a pop up dialog appears and said "The video cant be played".
This is the complete code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
txtTitle = (TextView)findViewById(R.id.txtTitle);
player = (VideoView)findViewById(R.id.player);
Bundle video = getIntent().getExtras();
if(video != null)
{
id = video.getString("id");
title = video.getString("title");
rtsp = video.getString("rtsp");
}
txtTitle.setText(title);
pDialog = new ProgressDialog(this);
pDialog.setTitle("Please Wait...");
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(this);
mediacontroller.setAnchorView(player);
// Get the URL from String VideoURL
Uri uri = Uri.parse(rtsp);
player.setMediaController(mediacontroller);
player.setVideoURI(uri);
player.setBackgroundColor(Color.WHITE);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
player.requestFocus();
player.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer arg0) {
// TODO Auto-generated method stub
pDialog.dismiss();
player.setBackgroundColor(Color.TRANSPARENT);
//the video will be played if i call the start() method here
}
});
player.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
//"The video cant be played" pop up dialog appeared, video wont start
player.start();
return false;
}
});
}
I got confuse because it can be played when i put the start() method in setOnPreparedListener but it couldn't when i put it in setOnTouchListener.
I don't know if this is related to the problem or not, but im buffering a youtube video (RTSP link) on my videoview.
Any help is appreciated, Thanks.
This should be a comment but my reputation is not high enough. I do not see you calling prepare() method so probably you are trying to invoke start() when player is in initialized state. You might try to set the onTouchListener of player object in the onPrepared() callback.

How to play mp3 continuosly when application starts and stop when user close app in background

I am writing an app in which i am allowing user to view images and select one of them to set an WALLPAPER, and in this i also want to play an mp3 when user starts an App and stop that mp3 when user close application
I have stored an MP3 Music file in res/raw folder namely : mymusic.mp3
I know how to play and stop MP3 music by using click on button, but don't know how to play mp3 in background continuosly, play when user start an app and stop when user close an app.
Please someone help me, its much needed any suggestion, sample code would be helpful...
MediaPlayer mPlayer;
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.music_file);
Button buttonPlay;
Button buttonStop;
buttonPlay = (Button) findViewById(R.id.play);
buttonPlay.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.mymusic.mp3);
mPlayer.start();//Start playing the music
}
});
buttonStop = (Button) findViewById(R.id.stop);
buttonStop.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer!=null && mPlayer.isPlaying()){//If music is playing already
mPlayer.stop();//Stop playing the music
}
}
});
This part has to be in EVERY activity's onPause:
Stop music automatically when user exit from app
public void onPause(){
super.onPause();
Context context = getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
if (!taskInfo.isEmpty()) {
ComponentName topActivity = taskInfo.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
StopPlayer();
Toast.makeText(xYourClassNamex.this, "YOU LEFT YOUR APP. MUSIC STOP", Toast.LENGTH_SHORT).show();
}
}
}
This part has to be in EVERY activity's onResume:
Play music automatically when user resume the app
Public void onResume()
{
super.onResume();
StartPlayer();
}
You can put your Player functionalty in global class. where every class can call it's player. so your plyer will be remain same in whole application. & you can start or stop it. on Pause method it will detect wether user left this App or not. if user left from the app so u can stop it.
GlobalPlayer.class
public MediaPlayer mPlayer;
public void StartPlayer(){
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.music_file);
// TODO Auto-generated method stub
mPlayer = MediaPlayer.create(getApplicationContext(),R.raw.mymusic.mp3);
mPlayer.start();//Start playing the music
}
public void StopPlayer(){
if(mPlayer!=null && mPlayer.isPlaying()){//If music is playing already
mPlayer.stop();//Stop playing the music
}
}
simply put your player.start() method in onResume() method and call player.stop ()
in onPause() method.Take a look at this
Difference between onStart() and onResume()
create service and put Mp3 start code in OnstartCommand(its a service class method you can override it) and whenever your you want to start mp3 just create Intent object and call StartService method and call StopSerice where you want to stop mp3 in application.

Android: Video Record using default camera

Below is my code and I'm gonna integrate with the application. My problem is when the button clicked video recorder invoked and video recoded smoothly but I want to get the response from the camera when the video recording is done every time.
public class AndroidVideoActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnVideoRecorder = (Button)findViewById(R.id.buttonClick);
btnVideoRecorder.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.VIDEO_CAMERA");
startActivity(intent);
}
});
}
}
Can someone please guide me.
Thanks for your time in advance.
Try changing
startActivity(intent);
to
startActivityForResult(intent);
Then when the video is done recordng, it will return to your app and you will be able to do something with the video

Categories

Resources