2

我开始知道我们可以在纹理视图中播放视频。但我只看到了如何播放相机捕捉的视频,但我想在其中播放可用的视频来执行任何动画。

我已经尝试过这段代码,但我不知道在哪里放置 video.mp4

public class MainActivity extends Activity implements
        TextureView.SurfaceTextureListener {

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_main);

        TextureView textureView = (TextureView) findViewById(R.id.textureView1);
        textureView.setSurfaceTextureListener(this);
    }

    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
            int height) {
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setSurface(new Surface(surface));

        try {
            mediaPlayer.setDataSource("video.mp4");
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture arg0) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture arg0, int arg1,
            int arg2) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture arg0) {
        // TODO Auto-generated method stub

    }
}

请通过提供任何工作代码来帮助我。或者,如果我的正确,那么在文件夹中放置 video.mp4 的位置。

提前致谢...

4

1 回答 1

1

将它放在原始文件夹中,然后尝试下面的一个,看看它是否有帮助。

public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setSurface(new Surface(surface));
    try {
        Uri video = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.your_raw_file);
        mediaPlayer.setDataSource(this, video);
        mediaPlayer.prepare();
        mediaPlayer.start();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
于 2015-05-22T07:08:23.110 回答