zoukankan      html  css  js  c++  java
  • 播放视频



    使用MediaPlayer可以播放音频,也可以播放视频,

    播放视频时考虑到图片刷新频率高,可以使用SerfaceView组件完成。

    本程序只能播放.3gp格式的视频,要想播放其他格式的视频需要使用其他算法。

    在sdcard里面存放一个名为Vieo.3gp的视频文件。

    在main.xml中:

    <LinearLayout

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:background="#000000"

        android:gravity="center_horizontal"

        android:orientation="vertical">

        <LinearLayout

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginTop="20dp"

            android:gravity="center_horizontal">

            <ImageButton

                android:id="@+id/play"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:src="@drawable/play"/>

            <ImageButton

                android:id="@+id/stop"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:src="@drawable/stop"/>

        </LinearLayout>

        <SurfaceView

            android:id="@+id/surfaceView"

            android:layout_margin="50dp"

            android:layout_width="fill_parent"

            android:layout_height="180dp"/>

    </LinearLayout>

    在MyVideoDemo.java中:

    package com.li.video;

    import java.io.IOException;

    import android.media.AudioManager;

    import android.media.MediaPlayer;

    import android.os.Bundle;

    import android.app.Activity;

    import android.view.Menu;

    import android.view.MenuItem;

    import android.view.SurfaceHolder;

    import android.view.SurfaceView;

    import android.view.View;

    import android.view.View.OnClickListener;

    import android.widget.ImageButton;

    import android.support.v4.app.NavUtils;

    public class MyVideoDemo extends Activity {

      private ImageButton play = null;

      private ImageButton stop = null;

      private MediaPlayer media = null;

      private SurfaceView surfaceView = null;

      private SurfaceHolder surfaceHolder = null;

        @Override

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            super.setContentView(R.layout.main);

            this.play = (ImageButton)super.findViewById(R.id.play);

            this.stop = (ImageButton)super.findViewById(R.id.stop);

            this.surfaceView = (SurfaceView)super.findViewById(R.id.surfaceView);

            this.surfaceHolder = this.surfaceView.getHolder();

            this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            this.media = new MediaPlayer();

            try {

           this.media.setDataSource("/sdcard/wmv.3gp");

         } catch (IllegalArgumentException e) {

           e.printStackTrace();

         } catch (IllegalStateException e) {

           e.printStackTrace();

         } catch (IOException e) {

           e.printStackTrace();

         }

            this.play.setOnClickListener(new PlayOnClickListener());

            this.stop.setOnClickListener(new StopOnClickListener());

           

        }

        private class PlayOnClickListener implements OnClickListener{

         public void onClick(View v) {

           MyVideoDemo.this.media.setAudioStreamType(AudioManager.STREAM_MUSIC);

           MyVideoDemo.this.media.setDisplay(MyVideoDemo.this.surfaceHolder);

           try {

             MyVideoDemo.this.media.prepare();

           } catch (IllegalStateException e) {

             e.printStackTrace();

           } catch (IOException e) {

             e.printStackTrace();

           }

           MyVideoDemo.this.media.start();

         }

        }

        private class StopOnClickListener implements OnClickListener{

        public void onClick(View v) {

            MyVideoDemo.this.media.stop();

        }

        }

    }

  • 相关阅读:
    电脑进入bios和u盘启动快捷键
    Securecrt 在win7下 字体太少问题
    windows无法安装到这个磁盘 gpt分区形式
    优酷上传高清视频
    将文件服务器及域控制器从2003迁移至Windows Server 2008 R2
    L SERVER 数据库被标记为“可疑”的解决办法
    Outlook关闭时最小化
    windows 7系统封装总结
    查询某软件所连接的外网IP地址
    windows桌面图标及任务管理栏丢失
  • 原文地址:https://www.cnblogs.com/riskyer/p/3317950.html
Copyright © 2011-2022 走看看