Android 视频播放


视频播放对应的Java代码:

mport android.app.Activity;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.MediaController;

import android.widget.VideoView;

 

public class androidmanifest4 extends Activity {

    private VideoView mVideoView;

    private Button playBtn, stopBtn;

    MediaController mMediaController;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.androidmanifest);

        mVideoView = new VideoView(this);

        mVideoView = (VideoView) findViewById(R.id.video);

        mMediaController = new MediaController(this);

        playBtn = (Button) findViewById(R.id.playbutton);

        stopBtn = (Button) findViewById(R.id.stopbutton);

        playBtn.setOnClickListener(new mClick());

        stopBtn.setOnClickListener(new mClick());

    }

 

    class mClick implements View.OnClickListener {

        @Override

        public void onClick(View v) {

 

            String uri = “android.resource://” + getPackageName() + “/” + R.raw.b_6;

 

            mVideoView.setVideoURI(Uri.parse(uri));

            mMediaController.setMediaPlayer(mVideoView);

            mVideoView.setMediaController(mMediaController);

            if (v == playBtn) {

                mVideoView.start();

            } else if (v == stopBtn) {

                mVideoView.stopPlayback();

            }

        }

    }

}

  

视频播放对应的xml文件:

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

    android:layout_width=”fill_parent”

    android:layout_height=”fill_parent”

    android:orientation=”vertical” >

    <TextView

        android:id=”@+id/TextView01″

        android:layout_width=”wrap_content”

        android:layout_height=”wrap_content”

        android:layout_gravity=”center_horizontal”

        android:text=”媒体播放器”

        android:textSize=”30sp” />

    <VideoView

        android:id=”@+id/video”

        android:layout_width=”250dp”

        android:layout_height=”200dp”

        android:layout_gravity=”center”

        android:layout_marginTop=”30sp”/>

    <LinearLayout

        android:layout_width=”wrap_content”

        android:layout_height=”wrap_content”

        android:layout_gravity=”center”

        android:layout_marginTop=”30sp”

        android:orientation=”horizontal”>

        <Button

            android:id=”@+id/playbutton”

            android:layout_width=”wrap_content”

            android:layout_height=”wrap_content”

            android:text=”开始播放”

            android:layout_gravity=”center”

            android:textSize=”25sp”   />

        <Button

            android:id=”@+id/stopbutton”

            android:layout_width=”wrap_content”

            android:layout_height=”wrap_content”

            android:text=”停止播放”

            android:layout_marginLeft=”30sp”

            android:layout_gravity=”center”

            android:textSize=”25sp”   />

    </LinearLayout>

</LinearLayout>

                                             5.14

原创文章,作者:dweifng,如若转载,请注明出处:https://blog.ytso.com/273149.html

(0)
上一篇 2022年7月10日
下一篇 2022年7月10日

相关推荐

发表回复

登录后才能评论