从零开始学android<mediaplayer自带播放器(视频播放).四十九.>
SurfaceHolder显示一个surface的抽象接口,使你可以控制surface的大小和格式, 以及在surface上编辑像素,和监视surace的改变。这个接口通常通过SurfaceView类实现。
<span style="font-size:18px;"><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=".MainActivity" > <ImageButton android:id="@+id/imageButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:src="@drawable/play" /> <SurfaceView android:id="@+id/surfaceview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_below="@+id/imageButton1" /> <ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/surfaceview" android:layout_alignRight="@+id/surfaceview" android:src="@drawable/stop" /> <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/surfaceview" android:layout_centerHorizontal="true" android:layout_marginBottom="162dp" /> </RelativeLayout> </span>
<span style="font-size:18px;">package com.example.mediaplayer2; import java.io.File; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Environment; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnHoverListener; import android.widget.ImageButton; import android.widget.ImageView; public class MainActivity extends Activity { private ImageButton play = null;// 播放 private ImageButton stop = null;// 停止播放 private MediaPlayer media = null; private ImageView image=null ; private SurfaceView surfaceView = null; private SurfaceHolder surfaceHolder = null; private Boolean pauseFlag=false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.activity_main); this.play = (ImageButton) super.findViewById(R.id.imageButton1); this.stop = (ImageButton) super.findViewById(R.id.imageButton2); this.surfaceView = (SurfaceView) super.findViewById(R.id.surfaceview);// 实例化SurfaceView this.surfaceHolder = this.surfaceView.getHolder();// 实例化SurfaceHolder this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 设置格式 this.image=(ImageView)this.findViewById(R.id.imageView1); this.media = new MediaPlayer();// 实例化MeidaPlayer组件 try { // 取得SD卡上的视频文件 String path = Environment.getExternalStorageDirectory() + File.separator + "Movies" + File.separator + "abc.mp4"; // 设置资源 this.media.setDataSource(path); // 少了一些判断 } catch (Exception e) { e.printStackTrace(); } // 分别对开始播放盒停止播放进行监听 this.play.setOnClickListener(new PlayOnClickListenerImpl()); this.stop.setOnClickListener(new StopOnClickListenerImpl()); this.surfaceView.setOnClickListener(new pauseOrstart() ); this.surfaceView.setOnHoverListener(new OnHoverListener() { public boolean onHover(View v, MotionEvent event) { // TODO Auto-generated method stub if (MainActivity.this.media.isPlaying()) { MainActivity.this.image.setImageResource(R.drawable.stop); } else { MainActivity.this.image.setImageResource(R.drawable.play); } return true; } }); } private class PlayOnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { // 设置音频格式啊 MainActivity.this.media .setAudioStreamType(AudioManager.STREAM_MUSIC); // 设置播放的范围 MainActivity.this.media.setDisplay(MainActivity.this.surfaceHolder); try { // 准备播放 MainActivity.this.media.prepare(); } catch (Exception e) { } // 开始播放 MainActivity.this.media.start(); } } private class StopOnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { // 停止播放 if (MainActivity.this.media!=null) { MainActivity.this.media.stop(); } } } private class pauseOrstart implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub if (MainActivity.this.media!=null) { if (MainActivity.this.pauseFlag) { MainActivity.this.media.start(); MainActivity.this.pauseFlag=false; } else { MainActivity.this.media.pause(); MainActivity.this.pauseFlag=true; } } }} } </span>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。