Currently I'm trying to simply stream a Video from some link this way:
try {
videoView = (VideoView) findViewById(R.id.videoView);
MediaController vidControl = new MediaController(this);
vidControl.setAnchorView(videoView);
videoView.setMediaController(vidControl);
videoView.setVideoPath("http://some.link/some_video.mp4");
loadingProgress.show();
videoView.requestFocus();
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
loadingProgress.dismiss();
videoView.start();
}
});
} catch (Exception e) {
e.printStackTrace();
}
Everything is going OK, but I want to make that URL (i.e. http://some.link/some_video.mp4) secure. I mean user won't be able to download this video.
The problem is: before loading this video, the logcat is
displaying log like this:
my.app.package.name W/MediaPlayer: Couldn't open http://some.link/some_video.mp4: java.io.FileNotFoundException: No content provider: http://some.link/some_video.mp4
I have noticed:
Even the code is not going into CATCH block.
Made SIGNED App, but that LOG is still appearing.
Those logs are built into the framework MediaPlayer class. The only way I know of to remove it is to take the MediaPlayer code from AOSP, copy it to a class in your app, remove all the log statements, and use that instead of the Android MediaPlayer class.
Related
So I'm trying to do my homework, but the teacher's given me 0 information about this. There is an example of how to play a video from a file in the res/raw folder, but there's nothing about online URLs. Please help me, I just want a simple player. I'll attach a picture detailing exactly what is up. I'll also add the code, since it's not that much and I really have no clue what could be wrong. Error says this:
W/MediaPlayer: Couldn't open http://techslides.com/...
java.io.FileNotFoundException: No content provider: http://techslides.com/demos/sample-videos/small.mp4
And this is the code:
VideoView video;
String url = "http://techslides.com/demos/sample-videos/small.mp4";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video = (VideoView) findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
mc.setAnchorView(mc);
video.setVideoPath(url);
video.setMediaController(mc);
video.start();
}
I will finally add to this that I've tried several different URLs, including some https ones and some http.
EDIT:
So, I tried fixing it and it ended up looking like this:
video = (VideoView) findViewById(R.id.videoView);
final MediaController mc = new MediaController(this);
mc.setAnchorView(mc);
video.setVideoPath(url);
video.setMediaController(mc);
video.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer mp){
video.start();
}
});
But it still gives me the same error when the emu is open. "Can't play this video". On the other hand, I got a bunch of new errors:
E/MediaPlayerNative: error (1, -2147483648)
E/MediaPlayer: Error (1,-2147483648)
D/VideoView: Error: 1,-2147483648
I'm not really familiarized with this technology, and the teacher hasn't given us any notions whatsoever as to what should or shouldn't be in the code for it to work. Just an example of a locally stored video playing in Android Studio with VideoView... that doesn't work when applied to online URLs.
So I've ended up fixing it myself. The problem wasn't in the code, for anyone wondering I've ended up using this simple format:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video = (VideoView) findViewById(R.id.video);
Uri uri = Uri.parse("http://techslides.com/demos/sample-videos/small.mp4");
video.setMediaController(new MediaController(this));
video.setVideoURI(uri);
video.requestFocus();
video.start();
}
The problem was the AVD itself. I had a Pixel 1 running Android 9, and that for some reason didn't work. I've installed a Nexus 5 with Oreo and it works flawlessly.
This may help you
VideoView simpleVideoView = (VideoView) findViewById(R.id.simpleVideoView); // initiate a video view
// perform set on prepared listener event on video view
simpleVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// do something when video is ready to play, you want to start playing video here
}
});
Try to start video only when its fully ready to play. Since mp4 will take some time to download.. so it might be in inconsistent state when you started video.
Hope this will help.
I am wondering how to successfully add subtitles to VideoView. I do not know how to do it. I do not know how to use addSubtitleSource, addTimedTextSource methods properly and if they are correct.
Android documentation says that addTimedTextSource is used only with MediaPlayer (which VideoView isn't) but there are some information about track i.e. getTrackInfo and others. I do not know where to start. There are some solutions in the internet but I do not understand those code snippets and I think they do not give an easy solutions.
Does anybody can help me?
Let's have a simple VideoView with video attached. What's next?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
videoView = findViewById(R.id.video_view1);
MediaController mMedia = new MediaController(this);
mMedia.setMediaPlayer(videoView);
mMedia.setAnchorView(videoView);
videoView.setMediaController(mMedia);
String path1 = VIDEO_URL;
Uri uri = Uri.parse(path1);
videoView.setVideoURI(uri);
videoView.start();
}
EDIT:
I've added the listener to VideoView and then OnTimedTextListener to MediaPlayer mp. Is this MediaPlayer mp attached to my VideoView? Is this the correct way to create MediaPlayer which will correspond with the VideoView?
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
try {
mp.addTimedTextSource("android.resource://"+getPackageName()+"/"+ R.raw.sub,MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() {
#Override
public void onTimedText(MediaPlayer mp, TimedText text) {
text.getBounds();
text.getText();
}
});
I do not know how to use getBounds() and getText() methods.
EDIT 2:
Now I have made something like this...
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
try {
mp.addTimedTextSource("android.resource://" + getPackageName() + "/"+R.raw.sub,
MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
} catch (IOException e) {
e.printStackTrace();
}
mp.selectTrack(2);
mp.setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() {
#Override
public void onTimedText(MediaPlayer mp, TimedText text) {
txtDisplay.setText(text.getText());
}
});
mp.start();
}
});
I put the "android.resource://" + getPackageName() + "/"+R.raw.sub path to the file but it gives me an error E/TaskPersister: File error accessing recents directory (directory doesn't exist?). Later I moved it to the assets folder but the result was the same. What's wrong?
I've added the mp.selectTrack(2); with index = 2 because I read somewhere that this is obligatory and for the videos getTrackInfo returns 2 in most cases.
In the end I thought that txtDisplay.setText() method will be OK. Is it?
Please tell me, is only 1 the issue here or more?
You could use ExoPlayer library for this purpose. Here is a medium post that explains how you could add an SRT subtitle to ExoPlayer tracks.
Hi we have an app in the playstore that has a module that play a stream from an url, the app works fine and we make a yearly release, but now that we are making changes, the part of the radio does not work, in any device.
Basically my code is
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource("http://radio.promosat.com:8104/");
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mediaPlayer.prepareAsync();
}catch (Exception ex){
ex.printStackTrace();
}
I keep getting this error
E/MediaPlayer: Error (1,-2147483648)
This worked perfectly, the url that we use it's in the example code, it uses a pls file, that's where we obtained the stream url.
The error was the encoding of the stream...aacp is not supported in Android.
I have used MediaController in my activity its working fine but when I play video for first time then there should b pause button visible but instead there is play and when I press that button then the video is paused correctly and state remains the same and after that its working properly. And same thing happens when Video Completed.
Is this a bug or I am doing any thing wrong?
videoView.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaController = new MediaController(VideoPlayerActivity.this){
public void hide(){
}
public void show(){
if(isPlayingAd){
super.hide();
}else{
super.show();
}
}
};
videoView.setMediaController(mediaController);
mediaController.setMediaPlayer(videoView);
mediaController.show();
}
});
I've been having the same issue. I was not calling MediaController.setVideoView as you were, as I thought VideoView.setMediaController was sufficient for wiring things up. I tried adding that, then moving the call to show within onPrepared, and now it is working.
I wish I had a better understanding; my best guess is that perhaps everything needs to be wired up properly before the media is prepared, and before calling show. In any case, here is what I have:
mMediaController = new MediaController(VideoPlayerActivity.this, false);
mVideoView.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer pMp) {
mMediaController.show();
}
});
mVideoView.setMediaController(mMediaController);
mMediaController.setMediaPlayer(mVideoView);
mVideoView.setVideoPath(uri); // may not be applicable in your case
mVideoView.requestFocus();
mVideoView.start();
As Oneworld mentioned on the other answer, I had same issue with old Samsung devices. Even though MediaController wired with VideoView properly, play button loses its sync until pause and play again with MediaController.
This thing seems only happens on old Samsung devices(KitKat and below i guess).
The only solution I found was play video programmically by videoview.start() before showing controller by mc.show().
I have mp4 and wmv movie files and need to play only sound not video screen. How to do it?? Could I get sound track from movie file??
I know if I hide the SurfaceView from screen, I can only hear sound.. but I have tried a lot but.. it is impossible.
If you have any solution to extract sound track from movie file, please let me know..
Thanks in advance.
If you are using the mediaplayer API in your app then don't call the API public void setDisplay (SurfaceHolder sh). This will ensure that only the audio is played out even if the video content is present..
More info in android doc here
Use following code, Optimize following code as per your requirement. It play video from youtube.
try {
final MediaPlayer m = new MediaPlayer();
Thread t = new Thread(new Runnable() {
public void run() {
try {
m.setDataSource("rtsp://v8.cache3.c.youtube.com/CjgLENy73wIaLwlQP1m32SiSYxMYJCAkFEIJbXYtZ29vZ2xlSARSB3JlbGF0ZWRggqG7w9aS2-1MDA==/0/0/0/video.3gp");
m.prepare();
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
} catch (Exception e) {
e.printStackTrace();
}