13

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?

Matthew
  • 131
  • 4
  • I suggest reading the documentation: http://developer.android.com/reference/android/media/MediaPlayer.html. For starters you need to call prepare() or prepareAsync() before calling start(); – William Seemann Jun 07 '12 at 02:23
  • 2
    I can feel my battery draining just thinking about it... – KidTempo Jun 12 '12 at 18:19
  • Are you able to get your code to play video in a regular app? That's one way to rule out whether or not your code is the problem. – Yusuf X Jun 19 '12 at 23:53

2 Answers2

3

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.

Salman
  • 149
  • 7
1

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

PeGiannOS
  • 227
  • 4
  • 19