Android: Video cannot be played from non hard-coded URL - android

I'm trying to stream a video and play it using VideoView. I supply the view with the source URL of the video with setVideoURI() as shown below. With a hardcoded value like urlString = "www.myvideoserver.com/videos/bigbuckbunny.mp4", the video plays fine. However, when urlString is given the value from the intent (coming from the previous activity where the user chose the video), I get the message: "Sorry, Video cannot be played". I've read that one of the common causes is video format, like it's described here and here. I'm almost certain that it is not a format issue because I can play the video when the URL is fixed (and I know this because I can see from Log.d("PVurl", urlString); that the value of urlString is exactly the same as the one I fixed it to. That is, in LogCat, I copy paste the value into the line urlString = getIntent()... // "www.myvideoserver.com/videos/bigbuckbunny.mp4", and it works but not when urlString is set to the intent return value . LogCat Errror panel gives the following:
04-13 17:35:32.786: ERROR/MediaPlayer(2620): error (1, -1007)
04-13 17:35:32.786: ERROR/MediaPlayer(2620): Error (1,-1007)
I've searched around the internet but it doesn't seem that anyone has encountered such an error code before.
I'd very much appreciate if anyone has any idea what could be the problem. Thanks!
public void playvideo () { // obtain URL of the requested video from the intent in previous activity
try
{
urlString = getIntent().getStringExtra("mypackage.fulladdr");
if (urlStr != null)
{
Log.d("PVurl", urlString);
VideoView v = (VideoView) findViewById(R.id.videoView1);
// play video
v.setVideoURI(Uri.parse(urlString));
v.setMediaController(new MediaController(this));
v.start();
v.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
showRatingDialog();
}
});
}
}
catch (Exception e)
{
Log.d("PV_TAG", +e.getMessage());
e.printStackTrace();
}
}

You haven't added the scheme which the Uri needs to find out what it is referring to (http, in your case) .
urlString = "www.myvideoserver.com/videos/bigbuckbunny.mp4" change it to urlString = "http://www.myvideoserver.com/videos/bigbuckbunny.mp4"

(1, -1007) error means:
MEDIA_ERROR_UNKNOWN - "File or network related operation errors."
this may come from a corrupt file
see also javadoc of android.media.MediaPlayer.OnErrorListener#onError
http://developer.android.com/reference/android/media/MediaPlayer.OnErrorListener.html#onError
#param what the type of error that has occurred:
MEDIA_ERROR_UNKNOWN
MEDIA_ERROR_SERVER_DIED
#param extra an extra code, specific to the error. Typically implementation dependent.
MEDIA_ERROR_IO
MEDIA_ERROR_MALFORMED
MEDIA_ERROR_UNSUPPORTED
MEDIA_ERROR_TIMED_OUT

I tried the same thing and was able to get it to work. Perhaps something else is going on in your code. Here is what I have:
// Intent to start the activity that launches the video player
Intent i = new Intent(this, ExampleActivity.class);
i.putExtra("url", "http://www.yourvideo.com/yourvid.m4v");
startActivity(i);
And here is the code that deals with the player:
private MediaController mController;
private Uri videoUri;
private int percentBuffered= 0;
private VideoView videoView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
mController = new MediaController(this);
videoView = (VideoView) findViewById(R.id.videoView1);
// access String from the intent that launched this Activity
videoUri = Uri.parse(getIntent().getStringExtra("url"));
}
#Override
public void onResume() {
super.onResume();
videoView.setOnCompletionListener(this);
try {
videoView.setVideoURI(videoUri);
mController.setMediaPlayer(videoView);
videoView.setMediaController(mController);
videoView.requestFocus();
videoView.start();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
protected void onPause() {
super.onPause();
finish();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
mController.show();
return super.onTouchEvent(event);
}
#Override
public int getBufferPercentage() {
return percentBuffered;
}
#Override
public int getCurrentPosition() {
return videoView.getCurrentPosition();
}
#Override
public int getDuration() {
return videoView.getDuration();
}
#Override
public boolean isPlaying() {
return videoView.isPlaying();
}
#Override
public void pause() {
videoView.pause();
}
#Override
public void seekTo(int pos) {
videoView.seekTo(pos);
}
#Override
public void start() {
videoView.start();
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
percentBuffered = percent;
}
#Override
public void onCompletion(MediaPlayer mp) {
}
public boolean canPause() {
return true;
}
public boolean canSeekBackward() {
return true;
}
public boolean canSeekForward() {
return true;
}

Alright, I found the problem after many futile hours of digging through to find what MediaPlayer error(1, -1007) means.
In reality, it has to do with the rather unfortunate fact that the line
getIntent().getStringExtra("mypackage.fulladdr");
returns a string with an extra white space at the end. I think it's an implementation issue with getIntent().
Anyone knows where to report these kind of bugs, that'd be great.
Thanks to #Akhil and #javaMe for their attempts!

Related

How to play public web city camera video in android (using android studio)?

I want to create app to take video from public ip camera from city user choose. For example, you write Rome and than you can watch video from one of public cameras from there.
Here is my code for MediaPlayer from Android Studio and it is working only for address that has .mp4 in the end(addres from example below is not working because of the end of it), I do not understand why. I need to play video from real time public city web cams.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String urlStr = "http://www.hf.uio.no/iakh/english/studies/blog/video/mov_0381.mp4?vrtx=view-as-webpage";
Uri uri = Uri.parse(urlStr);
VideoView vv = (VideoView) findViewById(R.id.videoView2);
vv.setOnErrorListener(new MediaPlayer.OnErrorListener() {
#Override
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {return false;}
});
try {vv.setVideoURI(uri);} catch (Exception e) {}
try {vv.start();} catch (Exception e) {}
vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
mp.setOnCompletionListener(null);
}
});
}
}

How to make the VideoView volume as mute?

I am working with the videoView, to play the .m3u8 videos steaming by using the custom controllers.
How can I make the VideoView audio as mute,Now I am going in one way, you can see my java code below. I tried that way but the Log files con't going inside on setOnInfoListener method. Is there any error what i am following.
Code I am using for getting Audio tracks:
Java
videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
MediaPlayer.TrackInfo[] trackInfoArray = mp.getTrackInfo();
Log.e("track Length : ",String.valueOf(trackInfoArray.length));
for (int i = 0; i < trackInfoArray.length; i++) {
Log.e("track "+i+" : ",trackInfoArray[i].getLanguage().toString());
if (trackInfoArray[i].getTrackType() == MediaPlayer.TrackInfo.MEDIA_TRACK_TYPE_AUDIO
&& trackInfoArray[i].getLanguage().equals(Locale.getDefault().getISO3Language())) {
mp.selectTrack(i);
break;
}
}
return true;
}
});
Code I am using for mute the VideoView:
Java
videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
#Override
public boolean onInfo(MediaPlayer m, int i, int i1) {
try {
Log.e("track : ","inside media player");
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
m.setVolume(0f, 0f);
m.setLooping(false);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
});
And remain everything video is running fine.Please give any other ways to make the videoView audio muting.
1.Is there any way to play the video with different audio tracks , I have list of .mp3 links.
Ref: Mute a playing video by VideoView in Android Application
Ans: https://stackoverflow.com/a/12549943/9909365
I have done this using MediaPlayer class. I have used setVolume function of MediaPlayer class to set the volume to 0. also I have realised that dont use AudioManager class, because using AudioManager if a set volume to 0, then it set volume to 0 for all the instance of MediaPlayer and VideoView. But if you will use setVolume() method of MediaPlayer then it will just Mute the volume of that instance only.
Also set volume to 0 is bot easy using VideoView because VideoView is a wrapper over MediaPlayer class and just allow to access few function of MediaPlayer. Also I have read on some blog that though you can reference MediaPlayer instance using VideoView instances, but its very complex and its not recommended to do so. Hope this would be helpful to other new readers how try to do similar things.
Try this:
WebView wView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wView=(WebView)findViewById(R.id.webView1);
wView.setWebViewClient(new WebViewClient(){
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
wView.getSettings().setJavaScriptEnabled(true);
wView.getSettings().setBuiltInZoomControls(true);
wView.addJavascriptInterface(this, "myinterface");
}
public void displayMsg(String name,String pass){
Toast.makeText(getApplicationContext(), name+"\n"+pass, 2000).show();
}
public void test(View v){
switch (v.getId()) {
case R.id.imageView1:
wView.loadUrl("http://www.facebook.com");
break;
case R.id.imageView2:
wView.loadUrl("http://www.linkedin.com");
break;
case R.id.imageView3:
wView.loadUrl("http://www.twitter.com");
break;
case R.id.imageView4:
wView.loadUrl("http://www.youtube.com");
break;
case R.id.imageView5:
wView.loadUrl("file:///android_asset/login.html");
break;
}
}
}

Pixelation in VideoView in Android ONLY with Wifi connection (3G/4G runs perfectly)

I have a problem that, after googling, I don't know why it is happenning.
I 've made and streaming video app with protocol RTSP. All is working good with a 3G/4G connection, but if i change to Wifi, much times video appears with a pixelation like a grey screen.
Testing in Android 5.1 (LG G3).
minSDKVersion = 15.
compiledSDKVersion = 22.
If i change to 3g, all goes well. It seems a problem of the initial buffer but its weird, because the problem is with Wifi connection (tested with two different connections, one xVDSL and one FTTH).
I'm not using any library, doing all the work directly with the android sdk and VideoView and mediaPlayer.
Here the code that init the configuration and how it handle the listeners:
videoView= (VideoView)this.findViewById(R.id.videoView);
videoView.setMediaController(null);
videoView.setOnErrorListener(this);
videoView.setOnCompletionListener(this);
videoView.setOnPreparedListener(this);
videoView.setVideoURI(Uri.parse(URL));
start/stop methods (the button above) and override listener methods:
public void startVideo(){
if(videoView !=null) {
initProgressDialog(getResources().getString(R.string.load_streaming));
startProgressDialog();
videoView.setVisibility(View.VISIBLE);
videoView.requestFocus();
//btnPlay.setVisibility(View.INVISIBLE);
}
}
public void stopVideo(){
if(videoView !=null) {
btnPlay.setText(getResources().getString(R.string.video_button_play));
stateVideo=2;
videoView.suspend();
}
}
public void resumeVideo(){
if(videoView !=null) {
initProgressDialog(getResources().getString(R.string.load_streaming));
startProgressDialog();
btnPlay.setText(getResources().getString(R.string.video_button_stop));
stateVideo=1;
videoView.setVisibility(View.VISIBLE);
videoView.resume();
}
}
private boolean manageErrorVideo(MediaPlayer mediaPlayer, int i, int i1){
return false;
}
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
videoView.setVisibility(View.VISIBLE);
stopProgressDialog();
}
#Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
//A lot of control error code. If need, ask but never go this way when the problem appear.
return true;
}
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
btnPlay.setText(getResources().getString(R.string.video_button_stop));
videoView.start();
stopProgressDialog();
stateVideo = 1;
findViewById(R.id.mainView).setBackgroundColor(Color.BLACK);
Log.d("test", mediaPlayer.getVideoHeight() + "");
Log.d("test", mediaPlayer.getVideoWidth()+"");
}
#Override
public boolean onInfo(MediaPlayer mediaPlayer, int i, int i1) {
return false;
}
#Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
}
Any information or help will be wellcome!
Thank you!!
EDIT: RTSP server is an IP camera.

How to free memory used by VideoView

I have been having this issue for a while now.
I have a simple application that plays a playlist of videos within a videoview with a pause of a few seconds between them.
private final Runnable playNormalVideo = new Runnable() {
public void run() {
try {
final String filepath = ...;
viewVideo.setOnErrorListener(new OnErrorListener() {
public boolean onError(MediaPlayer mp, int what, int extra) {
onEndVideo(false);
return true;
}
});
viewVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
onEndVideo(false);
}
});
viewVideo.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
background.setVisibility(View.INVISIBLE);
viewVideo.start();
}
});
viewVideo.setVideoPath(filepath);
viewVideo.setVisibility(View.VISIBLE);
viewVideo.requestFocus();
} catch (Exception e) {
String error = Utils.getStackTrace(e);
Utils.log(true, error);
}
}
};
and in my onEndVideo() function I make the background visible and check what video to play next and with a Handler i request to play it after x seconds.
My issue is the following :
Within a long run (approx 1 day) the background becomes invisible within a lot of seconds before the video starts. I don't understand why. If someone could help me get rid of this issue.
I also thought I should free memory between video plays but i don't seem to find how to do that.
Note : All the videos are saved on the device.
Thanks for any help.

Android MediaPlayer Streaming Error: 100: MEDIA_ERROR_SERVER_DIED

I've developed an app which takes an advantage of the native Android's MediaPlayer. The source code of my class making use of Media Player is below.
The problem is that only on some devices after some miliseconds of playback (I hear only voice, the screen remains black) I keep getting error(100,0) which according to the documentation says
public static final int MEDIA_ERROR_SERVER_DIED
Media server died. In this case, the application must release the MediaPlayer object and instantiate a new one.
On forums I've found out that I need to reset the player every time I get it... but I get it after just a short moment and then it dies forever. I cannot reset the player every second since playback is useless. I cannot get why some devices have this problem and others not. The one that I know has Android OS > 4.0.
Of course, first init() and then showVideo() are getting called. The last onError with code 100 is then called. What's a potential solution to make the streams run continuously and not break?
public class NativePlayer extends Player implements OnBufferingUpdateListener,
OnCompletionListener, OnErrorListener, OnInfoListener {
private VideoView videoview;
private PlayerListener listener;
private MainActivity context;
private final Logger logger = LoggerFactory.getLogger(NativePlayer.class);
#Override
public void init(MainActivity activity) {
this.videoview = (VideoView) activity.findViewById(R.id.video);
context = activity;
}
#Override
public void showVideo(final String url, final PlayerListener _listener) {
listener = _listener;
videoview.setVisibility(View.VISIBLE);
try {
Uri video = Uri.parse(url);
videoview.setVideoURI(video);
} catch (Exception e) {
logger.error("Error playing video", e);
listener.onVideoError();
return;
}
videoview.setOnCompletionListener(this);
videoview.setOnErrorListener(this);
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
videoview.start();
if (listener != null) {
listener.onVideoStarted();
}
}
});
}
#Override
public void onStop() {
stop();
}
private void stop() {
if (videoview == null) {
return;
}
if (videoview.isPlaying()) {
videoview.stopPlayback();
}
}
#Override
public void onDestroy() {
}
#Override
public void onCompletion(MediaPlayer mp) {
stop();
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
if (listener != null) {
listener.onVideoError();
}
return false;
}
#Override
public boolean onInfo(MediaPlayer mp, int what, int extra) {
if (listener != null) {
listener.onInfo(what, extra);
}
return false;
}
#Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
if (listener != null) {
listener.onBufferingUpdate(percent);
}
}
}
I had same problem (error 100, mediaplayer died, etc.).
I resolve it by using .stopPlayback(), and starting stream again.
Below is my part of code:
private void startWatchVideo(final string video_link) {
videoViewVA.setMediaController(new MediaController(this));
videoViewVA.setVideoURI(Uri.parse(video_link));
videoViewVA.requestFocus();
videoViewVA.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer media) {
media.start();
}
});
videoViewVA.setOnErrorListener(new OnErrorListener() {
#Override
public boolean onError(MediaPlayer media, int what, int extra) {
if (what == 100)
{
videoViewVA.stopPlayback();
startWatchVideo(video_link);
}
return true;
}
});
}
On practice it looks like video is just slows down

Categories

Resources