视频的播放
1.
<LinearLayout 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" android:orientation="vertical" tools:context="wyf.ytl.MainActivity" > <SurfaceView android:id="@+id/surfaceView" android:layout_width="212dp" android:layout_height="180dp" /> <!-- add a surface view to play video --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <Button android:id="@+id/play2_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="PLAY" /> <Button android:id="@+id/pause2_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="PAUSE" /> </LinearLayout> </LinearLayout>
2.
package wyf.ytl; import android.graphics.PixelFormat; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; 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.Button; import android.widget.Toast; import com.example.sample_2_11.R; public class MainActivity extends ActionBarActivity implements OnClickListener, SurfaceHolder.Callback { String path = "/sdcard/VideoCache/anim.mp4"; Button mPlayButton; Button mPauseButton; boolean isPause = false; SurfaceHolder mSurfaceHolder; MediaPlayer mediaPlay; SurfaceView mSurfaceView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPlayButton = (Button) findViewById(R.id.play2_button); mPlayButton.setOnClickListener(this); mPauseButton = (Button) findViewById(R.id.pause2_button); mPauseButton.setOnClickListener(this); getWindow().setFormat(PixelFormat.UNKNOWN); mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView); mSurfaceHolder = mSurfaceView.getHolder(); // add callback mSurfaceHolder.addCallback(this); mSurfaceHolder.setFixedSize(176, 144); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mediaPlay = new MediaPlayer(); } public void playVideo(String path) { Log.d("playVideo() ", "playVideo start..."); if (mediaPlay.isPlaying() == true) { mediaPlay.reset(); } mediaPlay.setAudioStreamType(AudioManager.STREAM_MUSIC); // 设置Videoy影片以SurfaceHolder播放 mediaPlay.setDisplay(mSurfaceHolder); try { // 设置路径 mediaPlay.setDataSource(path); mediaPlay.prepare(); } catch (Exception e) { Log.d("playVideo()", "play Video error..." + e.getMessage()); } mediaPlay.start(); } @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.play2_button: Log.d("on click", "click play button..."); isPause = false; playVideo(path); break; case R.id.pause2_button: Log.d("on click", "click pause button..."); Toast.makeText(this, "click pause", Toast.LENGTH_LONG).show(); if (isPause == false) { mediaPlay.pause(); isPause = true; }else{ mediaPlay.start(); isPause = false; } break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void surfaceCreated(SurfaceHolder holder) { } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { } }
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28