标签:and layout print 代码 apk extern nts instance dimen
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>
从零開始学android<mediaplayer自带播放器(视频播放).四十九.>
标签:and layout print 代码 apk extern nts instance dimen
原文地址:http://www.cnblogs.com/yangykaifa/p/6713367.html