Unable to play html5 streaming video in android web view - android

I have bought Foscam Security Camera and I am able to see the JPEG streaming on my MacBook.
But when I open the same link in my phone browser using chrome then it starts downloading something not sure what and in notification menu shows unsuccessful download.
Plus if I open the same link on my Android Firefox browser then I am able to see the video.
I have to create an android application to show the streaming of the video just as it is viewable on laptop browser.
The following is the code I am using:
package org.securitycamera;
import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.VideoView;
public class SecuritycameraActivity extends Activity {
WebView webView;
FrameLayout frameLayout;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflator = getLayoutInflater();
View inflatedView = inflator.inflate(R.layout.main, null);
if (!(inflatedView instanceof FrameLayout))
{
throw new RuntimeException("inflated view not FrameLayout");
}
else
{
frameLayout = (FrameLayout)inflatedView;
}
setContentView(frameLayout);
webView = (WebView) findViewById(R.id.wv);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(android.webkit.WebSettings.PluginState.ON);
webView.setWebChromeClient(new MyWebChromeClient());
try
{
// webView.loadUrl("http://192.168.1.6/videostream.cgi?user=admin&pwd=");
webView.loadUrl("http://broken-links.com/tests/video/");
}
catch(Exception e)
{
throw new RuntimeException();
}
}
private class MyWebChromeClient extends WebChromeClient implements MediaPlayer.OnCompletionListener, MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener {
VideoView videoView;
WebChromeClient.CustomViewCallback customViewCallback;
public void onProgressChanged(WebView view, int newProgress)
{
if (newProgress == 100)
{
view.loadUrl("javascript:playVideo()");
}
}
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)
{
if (view instanceof FrameLayout){
FrameLayout frame = (FrameLayout) view;
if (frame.getFocusedChild() instanceof VideoView){
VideoView video = (VideoView) frame.getFocusedChild();
frame.removeView(video);
setContentView(video);
video.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//Log.i(DVNGActivity.TAG, "LoadData_QRURL --> onCompletion...");
mp.stop();
setContentView(R.layout.main);
}
});
video.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// Log.i(DVNGActivity.TAG, "LoadData_QRURL --> onError");
return false;
}
});
video.start();
}
}
}
public void onPrepared(MediaPlayer mp)
{
}
public void onCompletion(MediaPlayer mp)
{
// this is needed to release the MediaPlayer and its resources so it can
// be used again later
videoView.stopPlayback();
// now remove the video and tell the callback to hide the custom view
frameLayout.removeView(videoView);
customViewCallback.onCustomViewHidden();
finish();
}
public boolean onError(MediaPlayer mp, int what, int extra)
{
return false; // we did not handle the error - onCompletion will be called
}
}
}
I followed this How to Play HTML5 video and YouTube Video within Android WebView?, and if instead of playing the video in example I play the video of my security camera i.e.
webView.loadUrl("http://192.168.1.6/videostream.cgi?user=admin&pwd=");
I get a white screen.

this sample https://gist.github.com/3718414 has an Android webview wrapper and HTML5 video - it's not as simple as just referencing the URL (if you want the video in the webView) but it's not difficult on ICS and above (ensuring you have hardware acceleration enabled seems to be pretty key)

Related

VideoView black background in Fragment

I adding VideoView in fragment but only I get black background nothing more I tried with two codes but both doesn't work can you help me?
PS I don't need play, stop button and anything other just to show the video Here is the code that I add will be good if I can mute the audio of the video and replay
Code 1
package com.Hristijan.Aleksandar.GymAssistant.Exercises;
import android.media.session.MediaController;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.VideoView;
import java.net.URL;
/**
* A simple {#link Fragment} subclass.
*/
public class BenchFragment extends Fragment {
private VideoView MyVideoView;
public BenchFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bench, container, false);
MyVideoView = (VideoView)rootView.findViewById(R.id.video_view);
Uri uri= Uri.parse("android.resource://"+getActivity().getPackageName()+"/"+R.raw.bench);
MyVideoView.setVideoURI(uri);
MyVideoView.start();
return inflater.inflate(R.layout.fragment_bench, container, false);
}
}
Code2
package com.hristijan.aleksandar.gymworkout.myapplication;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.VideoView;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private VideoView MyVideoView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyVideoView = findViewById(R.id.videoViewId);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.bench);
MyVideoView.setVideoURI(uri);
MyVideoView.start();
}
}
Layout.xml
<LinearLayout 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:orientation="vertical"
android:background="#000000"
tools:context="com.Hristijan.Aleksandar.GymAssistant.Exercises.BenchFragment">
<VideoView
android:id="#+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Try this to play,replay and identify the error
private void startVideo() {
String path = "android.resource://" + getPackageName() + "/" + R.raw.crop;
// Log.e(TAG,Uri.parse(path) + " ");
video_view.setVideoURI(Uri.parse(path));
video_view.start();
video_view.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int i, int i1) {
Log.e(TAG,String.format("Error: What: %d, Extra: %d",i,i1));
return false;
}
});
video_view.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.start();
}
});
}
If the video doesn't play it should log error in LogCat. You can identify what kind of error using this doc
About Muting the Audio
You can refer to this Question basically you just need to get the media player object when the content is ready then you can mute the audio by setting m.setVolume(0f, 0f);
I am sure that Code 1 and Code 2 can work
First, make sure your video format is Supported Media Formats
Then, make sure your video file bench stored in ...app\src\main\res\raw (And your file name should be bench not bench.xxx)
On the other hand, in Code 2 something is wrong:
MyVideoView = findViewById(R.id.videoViewId);
should be
MyVideoView = findViewById(R.id.video_view);
in order to find a VideoView by correct Id
Try putting videoView.start() inside videoView.setOnPreparedListener() as:
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mediaPlayer){
videoView.start();
}
});

Why my music won't play on Android Studio?

I'm using Android Studio.I tried all the codes that I've seen on the internet but my app doesn't work. I have a raw folder which contains the music that I'll be use but still I don't know what is the error in my code.
Here's my code:
package com.example.aloja.babysteps;
import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
/**
* Created by Aloja on 3/27/2017.
*/
public class A extends Activity {
Button btnBack3,btnPlay;
ImageView ivApple;
MediaPlayer apple;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
btnBack3 = (Button) findViewById(R.id.btnBack3);
btnPlay =(Button) findViewById(R.id.btnPlay);
ivApple = (ImageView) findViewById(R.id.ivApple);
ivApple.setImageResource(R.drawable.apple);
apple= MediaPlayer.create(this, R.raw.apple);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
apple.start();
}
});
}
protected void onPause(){
super.onPause();
apple.stop();
apple.release();
}
}
P.S Sorry for my english. Hope you understand what I'm trying to ask
Try this code hope it will help you.
int resID=getResources().getIdentifier("filename", "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);
mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
Log.e("MediaPlayer", "what=="+what);
Log.e("MediaPlayer", "extra=="+extra);
return false;
}
});
mediaPlayer.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
Log.e("MediaPlayer", "what=="+what);
Log.e("MediaPlayer", "extra=="+extra);
return false;
}
});
mediaPlayer.start();
I test your code on MX5 and it works well.
Did you enable your phone's music volume ?
(In MX5, you can see tone, media, notification and system volume.)

Android VideoView Activity crashing

I used the tutorial here (https://examples.javacodegeeks.com/android/android-videoview-example/) to build a videoview activity in my app though it keeps crashing when it's opened. I can't figure it out to save my life. I've checked the other posts on here and none of the suggestions have
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
public class DemoActivity extends AppCompatActivity {
private VideoView myVideoView;
private int position = 0;
private ProgressDialog progressDialog;
private MediaController mediaControls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quote);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// set the main layout of the activity
setContentView(R.layout.activity_main);
//set the media controller buttons
if (mediaControls == null) {
mediaControls = new MediaController(DemoActivity.this);
}
//initialize the VideoView
myVideoView = (VideoView) findViewById(R.id.video_view);
// create a progress bar while the video file is loading
progressDialog = new ProgressDialog(DemoActivity.this);
// set a title for the progress bar
progressDialog.setTitle("JavaCodeGeeks Android Video View Example");
// set a message for the progress bar
progressDialog.setMessage("Loading...");
//set the progress bar not cancelable on users' touch
progressDialog.setCancelable(false);
// show the progress bar
progressDialog.show();
try {
//set the media controller in the VideoView
myVideoView.setMediaController(mediaControls);
//set the uri of the video to be played
myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.demo));
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
myVideoView.requestFocus();
//we also set an setOnPreparedListener in order to know when the video file is ready for playback
myVideoView.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mediaPlayer) {
// close the progress bar and play the video
progressDialog.dismiss();
//if we have a position on savedInstanceState, the video playback should start from here
myVideoView.seekTo(position);
if (position == 0) {
myVideoView.start();
} else {
//if we come from a resumed activity, video playback will be paused
myVideoView.pause();
}
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
//we use onSaveInstanceState in order to store the video playback position for orientation change
savedInstanceState.putInt("Position", myVideoView.getCurrentPosition());
myVideoView.pause();
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
//we use onRestoreInstanceState in order to play the video playback from the stored position
position = savedInstanceState.getInt("Position");
myVideoView.seekTo(position);
}
}
In your code have two setContentView() for two layout. That is reason of your error. You need merge two layout and sure that VideoView available on that view.

Android Audio not playing from server

I am playing an audio (.mp3) from the server url using the following Android code. But, it is not playing the audio, simply does nothing, no errors. I have set Android min 11 to 22. I am testing on 5.0.2 Android phone. Could someone help what could be the reason not playing, please?
UPDATED:
I have solved it now. I converted google drive url to google drive download url using the help this link, and able to play the audio well.
http://www.labnol.org/internet/direct-links-for-google-drive/28356/
Code
package com.bclradio.manket.bclfm;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.media.MediaPlayer;
import android.media.AudioManager;
import java.io.IOException;
import android.util.Log;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Context;
public class BCLFMActivity extends Activity {
final Context context = this;
static final String AUDIO_PATH =
"https://drive.google.com/open?id=0BzcFuGIeWflwMmlITExmY3BCck0&authuser=0";
private MediaPlayer mediaPlayer;
private int playbackPosition=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bcl_fm);
}
public void doClick(View view) {
switch (view.getId()) {
case R.id.PlayStopButton:
Log.e("FMApp", "Play button clicked");
//AlertDialog.Builder ab = new AlertDialog.Builder(context);
//ab.setMessage("Test").show();
try {
playAudio(AUDIO_PATH);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
private void playAudio(String url) throws Exception
{
killMediaPlayer();
Log.e("FMApp", "Play Audio function triggered");
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
killMediaPlayer();
}
private void killMediaPlayer() {
if(mediaPlayer!=null) {
try {
mediaPlayer.release();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_bcl_fm, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Look at these two tutorials, In these the .mp3 files are playing through web url,
Example of streaming mp3 mediafile from URL with Android MediaPlayer class
Play Mp3 file from a Url
Also if you want to play .mp3 file in background I think you have to use Service and AIDL for it,
Look at basic Android-Music Player demo MusicDroid - Audio Player Part II it describe how to use Service and AIDl for your Audio Player.
Thanks..
Check out this answer.

RTSP Android Media player

I am trying to create a RTSP player for Android, but I am getting the error Video Can't be played. I don't know whats the mistake I am making, this is simply not working, I tried all methods, I am giving the code below
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity {
VideoView myVideoView;
ProgressDialog progDailog;
AudioManager audio;
MediaController mediaController;
String unStringUrl="rtsp://his.dvrdns.org:8554/channel/2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myVideoView = (VideoView)findViewById(R.id.videoplayer);
progDailog = ProgressDialog.show(MainActivity.this, null, "Video loading...", true);
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mediaController = new MediaController(this);
myVideoView.setMediaController(mediaController);
myVideoView.setVideoURI(Uri.parse(unStringUrl));
myVideoView.requestFocus();
myVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer arg0) {
// called too soon with rtsp in 4.1
if(progDailog != null) {
progDailog.dismiss();
}
myVideoView.start();
}
});
myVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
/*Intent intent = new Intent(MyVideoView.this, lastActivity);
intent.putExtra("cleTitre", activityTitle);
intent.putExtra("cleSegment", activityCat);
startActivity(intent);*/
}
});
myVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
if(progDailog != null) {
progDailog.dismiss();
}
return false;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You can try including internet -Permisison line in (following line) in your app's Manifest File.In my case it worked.
uses-permission android:name="android.permission.INTERNET"
Hope this helps.

Categories

Resources