安卓播放电影文件在动态壁纸

这有可能吗?我尝试过使用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类型。

任何人都知道为什么会发生这种情况,或者对另一种播放视频的方法有建议?


答案 1

您可以使用以下代码

VideoView videoView;
    VideoView = (VideoView) findViewById (R.id.txt1);
    videoView.setVideoPath(path);
    videoView.setVisibility(VideoView.VISIBLE);
videoView.start();

我试图在我的模拟器上播放mp4,但它没有显示视频,但当我在设备上尝试时,它工作正常。


答案 2

以前没有尝试过,但我认为您可以使用完全免费的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


推荐