繁体   English   中英

怎么玩mp4视频文件?

[英]How to play mp4 video file?

我正在练习一个项目,我想播放多种格式的视频。 我在google上搜索了这么多,并且还有很多代码的链接。 我尝试了很多代码,但没有一个能为我工作

我从这个链接尝试了这个代码,但它对我没有用所以任何人都可以建议我必须在哪里研究这个,以便我能够播放多种格式的视频

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".VideoPlayerActivity" >

    <FrameLayout
        android:id="@+id/video_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <VideoView
            android:id="@+id/video_player_view"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </FrameLayout>

</RelativeLayout>

public class VideoPlayerActivity extends Activity {
    VideoView video_player_view;
    DisplayMetrics dm;
    SurfaceView sur_View;
    MediaController media_Controller;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void getInit() {
        video_player_view = (VideoView) findViewById(R.id.video_player_view);
        media_Controller = new MediaController(this);
        dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int height = dm.heightPixels;
        int width = dm.widthPixels;
        video_player_view.setMinimumWidth(width);
        video_player_view.setMinimumHeight(height);
        video_player_view.setMediaController(media_Controller);
        video_player_view.setVideoPath("/sdcard/Bottle.mp4");
        video_player_view.start();
    }
}

首先定义ProgressDialog mProgressDialog;

然后尝试这段代码,它对我有用:

video = (VideoView) findViewById(R.id.VideoView1);
        final MediaController mc = new MediaController(this);
        mc.setAnchorView(video);
        mc.setMediaPlayer(video);
        Uri uri = Uri.parse(path1);
        video.setMediaController(mc);
        System.out.println("!-- uri: "+uri.toString());
        video.setVideoURI(uri);

        video.setOnPreparedListener(new OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {



                   mProgressDialog.dismiss();






            }
        });

        video.start();

        video.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {

            }

        });


    }


    public void showLoaderProgress(){
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setMessage("Loading Video\nPlease wait...");
        mProgressDialog.setIndeterminate(true);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

其中path1是您的视频路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM