Using:
private void play() {
VideoView v = (VideoView) findViewById(R.id.videoView);
MediaPlayer mp = MediaPlayer.create(this, R.raw.video);
mp.setDisplay(v.getHolder());
mp.start();
}
My video plays just about 5 first seconds, and stay like paused.. why does it happen? Is it something related to buffer? (I'm playing a local resource)
--
I've tried another 3gp video, and the same problem happens.
Combining a VideoView and a MediaPlayer may not be the right approach. VideoView uses its own MediaPlayer. Either use VideoView by itself, or use a combination of MediaPlayer and SurfaceView.
Working code (maybe was some problem with resource overuse):
private void play() throws Exception {
v = (VideoView) findViewById(R.id.videoView);
if (!firstPlay) {
mp.release();
}
mp = MediaPlayer.create(this, R.raw.video);
mp.setDisplay(v.getHolder());
mp.start();
firstPlay = false;
}
Related
I am trying to play an audio file in my actvity.
The file should play in loop but the first time it plays it gets cut in the beginning for almost a second. The other times it plays correctly. I simply do the following. I also tried with prepare and onpreparedlistener but the result is the same. Any Help?
MediaPlayer mp = new MediaPlayer();
mp = MediaPlayer.create(getApplicationContext(),R.raw.audio_test);
mp.setLooping(true);
mp.start();
add the loop function to your code:
mp.setLooping(true);
for the lag are you tried it in an actual devise?
Try this:
MediaPlayer mp;
mp = MediaPlayer.create(getApplicationContext(), R.raw.audio_test);
mp.setLooping(true);
mp.prepare();
mp.start();
I am new to Android Dev but I am trying to get a video to run when the app first starts. I have the .mp4 file in a 'raw' folder under 'res' directory. I have this so far...
public class MainActivity extends Activity {
MediaPlayer videotime;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videotime = MediaPlayer.create(this, R.raw.lessonslearned);
videotime.start();
}
protected void onResume() {
Log.e("Pickle", "onResume");
videotime = MediaPlayer.create(this, R.raw.lessonslearned);
videotime.start();
super.onResume();
}
Any help would be appreciated. The video should start when the app starts, play all the way through, then stop.
Thanks!
EDIT: I get MEDIAPLAYER error (1, -2147483648)
basically the video does not play and the audio is not in the background.
You shouldn't be calling videotime.start() in onCreate. That will start the MediaPlayer before the activity is in the forefront. You're calling videotime.start() twice. Only call it in the onResume() method.
You also need to call prepare on the MediaPlayer before starting it.
See here for an example of how to properly start a MediaPlayer:
Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();
This should work the same for video, but with different options.
Definitely a better approach would be to use a VideoView. I´m not completely sure but if you want to play a video with a MediaPlayer you need a SurfaceView to see the video, I have used Phoenixblade9 solution but only with .mp3 files.
You can use a VideoView
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView v = (VideoView) findViewById(R.id.myVideoView);
v.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video));
v.start();
}
just add the VideoView into your layout.
<VideoView
android:id="#+id/myVideoView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
Update: to play a Video in a MediaPlayer a SurfaceView is required.
I want to mute a playing Video by VideoView in my Android Application.
I couldn't find any method to do so in VideoView Class.
Any idea how to do this?
I have found a method "setVolume" in MediaPlayer Class, But I am unable to find any working code to play video by MediaPlayer class.
So I believe I can set volume 0 by this method.
Therefore I am looking for any working code to play video using MediaPlayer Class or how to control volume using VideoView Class.
Below is the code for playing video using VideoView , which I am using.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
String _path = "/mnt/sdcard/Movies/video5.mp4";
videoView.setVideoPath(_path);
videoView.requestFocus();
videoView.start();
}
If you want to get access to the MediaPlayer of a VideoView you have to call MediaPlayer.OnPreparedListener and MediaPlayer.OnCompletionListener, then you can call MediaPlayer.setVolume(0f, 0f); function to set the volume to 0.
Do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
VideoView videoView = (VideoView)this.findViewById(R.id.VVSimpleVideo);
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
String _path = "/mnt/sdcard/Movies/video5.mp4";
videoView.setVideoPath(_path);
videoView.setOnPreparedListener(PreparedListener);
videoView.requestFocus();
//Dont start your video here
//videoView.start();
}
MediaPlayer.OnPreparedListener PreparedListener = new MediaPlayer.OnPreparedListener(){
#Override
public void onPrepared(MediaPlayer m) {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
m.setVolume(0f, 0f);
m.setLooping(false);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
};
videoview.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setVolume(0, 0);
}
});
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.
I need to play two video one after another(as a pair), the first video is as intro video and the second video is as main video, So what I actually need is that after finishing intro video the main video will start...say intro-1 & main-1, intro-2&main-2, intro-3& main3...so on.
the problem that I am getting is that i cnt move to intro video again after completing the main video.Only the main video is played again and again
Here is that code:
videoView.setVideoPath(introPath);
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(final MediaPlayer mp) {
videoView.setVideoPath(mainPath);
MediaController mc = new MediaController(DisplayVideo.this);
videoView.requestFocus();
videoView.start();
}
}
any help will really be appreciated Thanks
Create a list of the video paths, something like:
List<String> videoPathes = new ArrayList<String>();
videoPathes.add(path1);
videoPathes.add(path2);
// etc..
and some index, for example:
int i = 0;
In the onCompletionListener, set the next path this way:
public void onCompletion(final MediaPlayer mp) {
i = (i + 1) % videoPathes.size();
videoView.setVideoPath(videoPathes.get(i));
// the rest ...
}
I am trying to play a video that is passed from a WebView in a VideoView. It works, except VideoView does not want to read it. I keep getting the error:
"Sorry, this video cannot be played."
Here is the code for the VideoView:
public class VideoHandler extends Activity {
WebView myWebView;
VideoView myVideoView;
WebChromeClient chromeClient;
WebViewClient wvClient;
Intent in;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_player);
myVideoView = (VideoView) findViewById(R.id.videoview);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(myVideoView);
String video = (MNWVMainPage.myWebView.getUrl());
myVideoView.setMediaController(mediaController);
myVideoView.setVideoPath(video);
myVideoView.start();
myVideoView.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
setContentView(R.layout.mnwv_main);
}
});
}
}
Why wont this load the video?
From just reading from your code, I can see no error.
Can you check a few things:
Does the video align to the format list here?
Have you open the camera/ another VideoView? Even if you release them, the buffer seems to need some time to be actually released.