currently, I'm working on a video-based app project. Now I'm struggling with some problems.
As you can see, I try to put a MediaController to control the video playback. But, there is a problem. The MediaController won't align to the center of the VideoView.
I tried to following some suggestion from:
MediaController positioning over VideoView
Positioning the MediaController over the VideoView
MediaController Positioning - bind to VideoView
Android MediaController position
But, none of them are work.
Here is my code:
video1 = findViewById(R.id.video_1);
video1.setVideoURI(videoPath);
MediaController mc = new MediaController(this);
mc.setMediaPlayer(video1);
video1.setMediaController(mc);
mc.setAnchorView(video1);
video1.start();
Any suggestions or solutions to my problem? Thank You.
Related
So I have a activity page with a button on it. If you click the button it takes you to a new activity page where it will play an audio file that inside /res/raw. This plays perfectly fine no problem.
My problem is I am unable to attach a mediacontroller object to the mediaplayer and therefore I can't play/pause/stop/rewind the audio file. So my code for the activity page that plays the media file looks something like this:
public class MediaActivity extends Activity {
private MediaController a;
private MediaPlayer b;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
a = (MediaController) findViewById(R.id.mediaController1);
b = MediaPlayer.create(MediaActivity.this, R.raw.sound);
b.start();
}
I'm missing a few extra things like the onCreateOptionsMenu function, but that's not relevant here. The problem is when I transition to this activity, the sound plays but I don't see the mediacontroller. I've set the controller to visible, I've tried several things like calling the .show() function but still nothing. I ran across a couple of code snippets but the code is too long for my purposes, I just need to attach the controller to the player.
Any ideas on how I can do this?
I'm new to android development, self taught so can expect a few errors here and there but none so irritating as this. I've looked over my code a thousand times, searched high and low across multiple websites, books and forums for an answer but I still get the same error so this is a last resort.
I just want to play a hardcoded path to a video in an activity, which is part of my video portfolio app. (The hardcoded path is just for testing, later I will call each video from the related button press, but only after I sort the player out!).
Here is my code:
String path = "android.resource://mysite/res/raw/video1";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videoplayer);
VideoView view = (VideoView) this.findViewById(R.id.vv);
MediaController controller = (MediaController) new MediaController(this);
controller.setMediaPlayer(view);
view.setVideoPath(path);
view.requestFocus();
view.start();
}
This snippet is inside my VideoPlayer class, where vv is the VideoView in the XML and video1 is the video to be played. The video is h.264 mp4, 1 minute long and 3mb in size and can be played normally through the default player.
XML:
<VideoView
android:id="#+id/vv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
The activity loads but returns the error "Can't play this video".
Any help I greatly appreciate.
VideoView view = (VideoView) this.findViewById(R.id.vv);
view.setVideoPath(path);
view.setMediaController(new MediaController(this));
view.requestFocus();
view.start();
Use like this. hope this will give you some solution.
According Android's official documentation Video decoding support for MP4(h.264) added from android 3.0+, so I think you are playing this video on version below 3.0. Try it on the device which has android os version 3.0+.
Try this..
video0=(VideoView)findViewById(R.id.vv);
video0.setMediaController(new MediaController(this));
video0.setVideoURI(Uri.parse("android.resource://" +getPackageName()+ "/" +R.raw.video1));
video0.requestFocus();
video0.start();
Did you try
MediaController mc = new MediaController(this);
mc.setAnchorView(vv);
I want to animate the displaying and hiding of a VideoView.
My View is wrapped in a MediaController.
My code:
VideoView videoView = (VideoView) findViewById(R.id.myvideoview);
mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setOnCompletionListener(myVideoViewCompletionListener);
I tried to animate with this:
Animation anim = AnimationUtils.loadAnimation(context, R.anim.my_anim);
videoview.startAnimation(anim);
I also tried to animate the MediaController but no luck so far.
I found just un- or not quite answered posts around the net.
Does anybody has an idea what I might be doing wrong?
Thanks.
Ron
Is this even possible? I have tried using the MediaPlayer but it throws a NullPointerException on the MediaPlayer object. I can get audio to work but video wont.
mp=MediaPlayer.create(getApplicationContext(), R.raw.sample);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer mp) {
mp.release();
playing = false;
}
});
the sample is of .mp4 type.
Anyone have an idea of why this is happening or have a suggestion for another method of getting videos to be played?
You can use the following code
VideoView videoView;
VideoView = (VideoView) findViewById (R.id.txt1);
videoView.setVideoPath(path);
videoView.setVisibility(VideoView.VISIBLE);
videoView.start();
i have tried to play mp4 on my emulator but it was not showing video but when i tried on device it work fine.
Haven't tried that before but I think you can use vlcj framework that's totally free and can play effectively almost any type of video (and of course plays .mp4 video files) .I can't give you any code in android because have never worked with android but I know java and and it just works.So here what i use in Java:
NativeLibrary.addSearchPath("libvlc",path); //To set path of libvlc
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);//To import libvlc
//The path can be a folder in your android project.All the files needed are in vlc player installation folder.so yes you have to install vlc in your computer to get those files but just once.
canvas = new WindowsCanvas();
panel.add(canvas);//panel is like your VideoView
canvas.setVisible(true);
canvas.setBackground(Color.black);
mediaPlayerFactory = new MediaPlayerFactory();
player12 = mediaPlayerFactory.newEmbeddedMediaPlayer();
CanvasVideoSurface videoSurface = mediaPlayerFactory.newVideoSurface(canvas);
player12.setVideoSurface(videoSurface);
player12.setPlaySubItems(true);
player12.startMedia(yourVideoPath);
player12.setAspectRatio(""+panel.getWidth()+":"+panel.getHeight()); //Those two lines are for your video to be adusted in your panel or better to your VideoView
player12.setCropGeometry(""+panel.getWidth()+":"+panel.getHeight());
The jar files you have to include in your classpath are jna-3.4.0.jar,platform-3.4.0.jar,vlcj-2.1.0.jar
I want to create an Android application that is a client for an Internet radio station. And I want it look native to Android? But im confused with Android API logic and documentation. What i've got is that I need MediaPlayer and MediaController classes. Am I right, and is there any good example of AUDIO player for Android?
Especially, I'm very interested how to use MediaPlayer and MediaController classes together.
UPD:
Finally I've got the code, that does exactly what I want:
Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = Uri.parse(%file_uri%));
i.setData(u);
startActivity(i);
you can look at those links :
http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-i
Hope it will help.
[EDIT]
You have also some example on the official android developer website :
http://developer.android.com/guide/topics/media/index.html