这甚至可能吗?我尝试过使用MediaPlayer,但它会在MediaPlayer对象上抛出NullPointerException。我可以让音频工作,但视频不会。
mp=MediaPlayer.create(getApplicationContext(), R.raw.sample);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener()
{
public void onCompletion(MediaPlayer mp) {
mp.release();
playing = false;
}
});
样本为.mp4类型。
任何人都知道为什么会发生这种情况,或者对另一种播放视频的方法有所建议?
答案 0 :(得分:3)
您可以使用以下代码
VideoView videoView;
VideoView = (VideoView) findViewById (R.id.txt1);
videoView.setVideoPath(path);
videoView.setVisibility(VideoView.VISIBLE);
videoView.start();
我曾尝试在我的模拟器上播放mp4,但它没有显示视频但是当我在设备上尝试时它工作正常。
答案 1 :(得分:1)
之前没有尝试过,但我认为你可以使用完全免费的vlcj框架,可以有效地播放几乎任何类型的视频(当然还有播放.mp4视频文件)。我不能在android中给你任何代码因为从来没有使用过android但是我知道java并且它只是起作用。所以我在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());
您必须包含在类路径中的jar文件是jna-3.4.0.jar,platform-3.4.0.jar,vlcj-2.1.0.jar