AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.pengdl.video_mediaplayer" > <uses-sdk android:minSdkVersion="21"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".Video_MediaPlayer" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
activity_video__media_player.xml
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Video_MediaPlayer"> <SurfaceView android:id="@+id/surface1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true"> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/start"/> <Button android:id="@+id/pause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/pause"/> <Button android:id="@+id/stop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/stop"/> </LinearLayout> </RelativeLayout>
strings.xml
<resources> <string name="app_name">Video_MediaPlayer</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="start">Start</string> <string name="pause">Pause</string> <string name="stop">Stop</string> </resources>
Video_MediaPlayer.java
package com.example.pengdl.video_mediaplayer; import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import java.io.IOException; public class Video_MediaPlayer extends Activity implements View.OnClickListener{ private Button start; private Button pause; private Button stop; private MediaPlayer mediaPlayer; private SurfaceView surfaceView; private int postion = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video__media_player); start = (Button)findViewById(R.id.start); pause = (Button)findViewById(R.id.pause); stop = (Button)findViewById(R.id.stop); surfaceView = (SurfaceView)findViewById(R.id.surface1); mediaPlayer = new MediaPlayer(); surfaceView.getHolder().setKeepScreenOn(true); surfaceView.getHolder().addCallback(new SurfaceViewLis()); start.setOnClickListener(this); pause.setOnClickListener(this); stop.setOnClickListener(this); } private class SurfaceViewLis implements SurfaceHolder.Callback { @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { if (postion == 0) { try { play(); mediaPlayer.seekTo(postion); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } @Override public void surfaceDestroyed(SurfaceHolder holder) { } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_video__media_player, 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(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start: try { play(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; case R.id.pause: if (mediaPlayer.isPlaying()) { mediaPlayer.pause(); } else { mediaPlayer.start(); } break; case R.id.stop: if (mediaPlayer.isPlaying()) mediaPlayer.stop(); break; default: break; } } public void play() throws IllegalArgumentException, SecurityException, IllegalStateException, IOException { mediaPlayer.reset(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource("/sdcard/Movies/u-boot.mp4"); // 把视频输出到SurfaceView上 mediaPlayer.setDisplay(surfaceView.getHolder()); mediaPlayer.prepare(); mediaPlayer.start(); } @Override protected void onPause() { if (mediaPlayer.isPlaying()) { // 保存当前播放的位置 postion = mediaPlayer.getCurrentPosition(); mediaPlayer.stop(); } super.onPause(); } @Override protected void onDestroy() { if (mediaPlayer.isPlaying()) mediaPlayer.stop(); mediaPlayer.release(); super.onDestroy(); } }
完
时间: 2024-10-09 21:01:30