Effect display:
- First register in the manifest file:
<uses-permission android:name< /span>="android.permission.READ_EXTERNAL_STORAGE" />
- java code:
ol>
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback { private SurfaceView surfaceView; p rivate SurfaceHolder holder; private TextView tvSound, tvCurrentT, tvDuration; private ProgressBar progressBar; private MediaPlayer mediaPlayer; private Uri uri; private Handler handler; private float downX, downY; private int screenWidth; private int FACTOR = 100; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate( savedInstanceState); sup portRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //TODO Set the screen to landscape() setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //TODO sets the screen to portrait() //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); screenWidth = getResources( ).getDisplayMetrics().widthPixels; surfaceView = (SurfaceView) findViewById(R.id.surface_view); surfaceView.setOnTouchListener(new View.OnTouchListener() {@Override public boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN: downX = event.getX(); downY = event.getY(); break; < span class="hljs-keyword">case MotionEvent.ACTION_MOVE: // TODO volume float distanceX = event.getX()-downX; float distanceY = event.getY()-downY; if (downX> screenWidth-200 && Math.abs(distanceX) <50 && distanceY> FACTOR) { // TODO decrease the volume setVolume(false); } else if (downX> screenWidth-200 && Math.abs(distanceX) <50 && distanceY <-FACTOR) {// TODO increase the volume setVolume(true);} // TODO playback progress adjustment if (Math.abs(distanceY) <50 && distanceX> FACTOR) {// TODO fast forward< /span> int currentT = mediaPlayer.getCurrentPosition();//playing position mediaPlayer.seekTo(currentT + 15000); downX = event.getX(); downY = event.getY(); Log.i("info", "distanceX fast forward=" + distanceX);} else if (Math.abs(distanceY) <50 && distanceX <-FACTOR) {// TODO rewind int currentT = mediaPlayer.getCurrentPosition(); mediaPlayer.seekTo(currentT-15000); downX = event.getX(); downY = event.getY(); Log.i ("info", "distanceX=" + distanceX);} break;} return true;} }); holder = surfaceView.getHolder( ); holder.addCallback(this); tvSound = (TextView) findViewById(R.id.tv_sound); tvCurrentT = (TextView) findViewById(R.id.tv_current) ; tvDuration = (TextView) findViewById(R.id.tv_duration); progressBar = (ProgressBar) findViewById(R.id.progress); mediaPlayer = new MediaPlayer(); // TODO set the playback source for videoview (set by local storage card) // uri = Uri.fromFile(new File ("/sdcard/download/video1.mp4")); //TODO set the playback source for viedeoview (through resource text When calling the resource file, PS adds "://" uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + ": //" + getPackageName() + "/" + R.raw.video1);//TODO Add video1 video under raw() handler = new Handler(); mediaPlayer.setOnPreparedListener(new span> MediaPlayer.OnPreparedListener() {@Override public void onPrepared(MediaPlayer mp) {// start playing the video mediaPlayer.start(); < span class="hljs-comment">// set total duration tvDuration.setText(mp.getDuration() / 1000 + ""); tvCurr entT.setText(mp.getCurrentPosition() / 1000 + ""); progressBar.setMax(mp. getDuration()); updateView();} }); mediaPlayer .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {@Override public void onCompletion( MediaPlayer mp) {mediaPlayer.start();} });} private void setVolume(boolean flag) {// Get volume manager AudioManager manager = (AudioManager) getSystemService (AUDIO_SERVICE); // Get current volume int curretnV = manager.getStreamVolume(AudioManager.STREAM_MUSIC); < span class="hljs-keyword">if (flag) {curretnV++;} else {curretnV--;} manager.setStreamVolume(AudioManager.STREAM_MUSIC, curretnV, AudioManager.FLAG_SHOW_UI); tvSound.setVisibility(View.VISIBLE); tvSound.setText("Volume:" + curretnV); handler.postDelayed(new Runnable() {@Override public void run() {tvSound.setVisibility(View.GONE);} }, 1000); /** * 1. AudioManager.STREAM_MUSIC multimedia 2. AudioManager.STREAM_ALARM alarm clock* 3. AudioManager.STREAM_NOTIFICATION notification 4. AudioManager.STREAM_RING ring tone* 5. AudioManager.STREAM_SYSTEM system tone 6. AudioManager. STREAM_VOICE_CALL * Phone* * AudioManager.FLAG_SHOW_UI: Display volume control*/} @Override public void surfaceCreated(SurfaceHolder holder) {// When the surfaceView is The canvas can be drawn before the creation is complete, so it can only be played after this callback method try {// 1 .Specify the playback source mediaPlayer.setDataSource(this, uri); // 2. When mediaplayer and surfaceView Line binding mediaPlayer.setDisplay(holder); // 3. Prepare for asynchronous playback (when prepareAsync is called, the onPrepared callback method of mediaPlayer will be executed)mediaPlayer.prepareAsync();} catch (IOException e) {e.printStackTrace();}} @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height ) {} @Override public void < span class="hljs-title">surfaceDestroyed(SurfaceHolder holder) {try {mediaPlayer.stop(); mediaPlayer.release();} catch (Exception e) {e.printStackTrace();}} < span class="hljs-javadoc">/** * Recursion to update playback progress*/ private void updateView() {handler.postDelayed(new Runnable() {@Override public void run() {// TODO settings progress control if (mediaPlayer != null && mediaPlayer.isPlaying()) {tvCurrentT.setText("Progress:" + mediaPlayer.getCurrentPosition() / 1000); progressBar.setProgress(mediaPlayer.getCurrentPosition());} updateView(); } }, 1000); }}
3. Layout file code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"< /span> android:layout_width="match_parent" android: layout_height="match_parent" > <SurfaceView android:id="@+id/surface_view" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id=" @+id/tv_sound" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#33ffffff" android:padding="8dp" android:text< /span>="Volume" android:visibility=" gone" /> <RelativeLayout android: layout_width="m atch_parent" android:layout_height="48dp" android:layout_alignParentBottom="true" android:layout_margin="4dp" android:background="#33ffffff">> span> <TextView android:id="@+id/tv_current" android:layout_width="wrap_content" span> android:layout_height="24dp" android:layout_marginLeft ="4dp" android:textColor= "#fff" /> <TextView android:id="@+id/tv_duration" android:layout_width="wrap_content" android:layout_height="24dp" android:layout_alignParentRight< /span>="true" android:layout_marginRight=" 4dp" android:textColor="#fff" /> <ProgressBar android:id="@+id/progress" style="?android:progressBarStyleHorizontal" < span class="hljs-attribute">and roid:layout_width="match_parent" android:layout_height="24dp" android:layout_alignParentBottom="true" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" /> RelativeLayout>< /span>RelativeLayout>
Effect display:
- First register in the manifest file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- java code:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback { private SurfaceView surfaceView; private SurfaceHolder holder; private TextView tvSound, tvCurrentT, tvDuration; < span class="hljs-keyword">private ProgressBar progressBar; private MediaPlayer mediaPlayer; private Uri uri; private Handler ha ndler; private float downX, downY; private int screenWidth; private int FACTOR = 100; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate( savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); //TODO Set the screen to landscape() setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) ; //TODO sets the screen to portrait() //setRequestedOrientation(ActivityInfo.SCREEN_ ORIENTATION_PORTRAIT); screenWidth = getResources().getDisplayMetrics().widthPixels; surfaceView = (SurfaceView) findViewById(R.id.surface_view); surfaceView.setOnTouchListener(new span> View.OnTouchListener() {@Override public boolean onTouch(View v, MotionEvent event) {switch (event.getAction()) { case MotionEvent.ACTION_DOWN: downX = event.getX(); downY = event.getY(); break ; case MotionEvent.ACTION_MOVE: // TODO volume float distanceX = event.getX()-downX; float distanceY = event.getY()-downY; if (downX> screenWidth-200 && Math.abs(distanceX) <50 && distanceY> FACTOR) {// TODO decrease volume setVolume(false);} else if (downX> screenWidth-200 && Math.abs(distanceX) <50 && distanceY <-FACTOR) { // TODO increase the volume setVolume(true);} / / TODO playback progress adjustment if (Math.abs(distanceY) <50 && distanceX> FACTOR) {// TODO fast forward int currentT = mediaPlayer.getCurrentPosition();//Playing position mediaPlayer.seekTo(currentT + 15000); downX = event.getX(); downY = event.getY (); Log.i("info", "distanceX fast forward=" + distanceX);}else if (Math.abs(distanceY) <50< /span> && distanceX <-FACTOR) {// TODO rewind int currentT = mediaPlayer.getCurrentPosition( ); mediaPlayer.seekTo(currentT-15000); downX = event.getX(); downY = event.getY(); Log.i("info", "distanceX=" + distanceX);} break;} return true;} }); holder = surfaceView.getHolder(); holder.addCallback(this); tvSound = (TextView) findViewById(R.id.tv_sound); tvCurrentT = (TextView) findViewById(R.id.tv_current); tvDuration = (TextView) findViewById(R.id.tv_duration); progressBar = (ProgressBar) findViewById(R.id.progress); mediaPlayer = new MediaPlayer(); // TODO set the playback source for videoview (set by local storage card) // uri = Uri.fromFile(new File(" /sdcard/download/video1.mp4")); //TODO set the playback source for viedeoview (set by the resource file), when the PS calls the resource file, After the protocol header, add "://" uri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + < span class="hljs-string">"/" + R.raw.video1);//TODO add video1 video under raw () handler = new Handler(); mediaPlayer. setOnPreparedListener(new MediaPlayer.OnPreparedListener() {@Override public void onPrepared(MediaPlayer mp) {/ / Start playing the video mediaPlayer.start(); // Set the total duration tvDuration.setText(mp.getDuration() / 1000 + ""); tvCurrentT.setText(mp.getCurrentPosition() / 1000 span> + ""); progressBar.setMax(mp.getDuration()); updateView();} }); mediaPlayer .setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) {mediaPlayer.start();} });} private void setVolume(boolean flag) {// Get the volume manager AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE); // Get the current volume int curretnV = manager.getStreamVolume(AudioManager.STREAM_MUSIC); if (flag) {curretnV++;} else {curretnV--;} manager.se tStreamVolume(AudioManager.STREAM_MUSIC, curretnV, AudioManager.FLAG_SHOW_UI); tvSound.setVisibility(View.VISIBLE); tvSound.setText("Volume:" + curretnV); handler. postDelayed(new Runnable() {@Override public span> void run() {tvSound.setVisibility(View.GONE);} }, 1000); /** * 1. AudioManager.STREAM_MUSIC 2. AudioManager.STREAM_ALARM alarm clock* 3. AudioManager.STREAM_NOTIFICATION notification 4. AudioManager .STREAM_RING ring tone* 5.AudioManager.STREAM_SYSTEM system prompt tone 6.AudioManager.STREAM_VOICE_CALL * Phone* * AudioManager.FLAG_SHOW_UI: display volume control*/} @Override span> public void surfaceCreated(SurfaceHolder holder) {// when The canvas can be drawn before the surfaceView is created, so it can only start playing after this callback method try {/ / 1. Specify the playback source mediaPlayer.setDataSource(this, uri); // 2. Set the mediaplayer and SurfaceView time line binding mediaPlayer.setDisplay(holder); // 3. Prepare for asynchronous playback (when prepareAsync is called, the onPrepared callback method of mediaPlayer will be executed) span> mediaPlayer.prepareAsync();} catch (IOException e) {e.printStackTrace();}} @Override< /span> public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) {try {mediaPlayer.stop(); mediaPlayer.release();} catch (Exception e) {e.printStackTrace();}} /** * Recursion to update playback progress*/ < span class="hljs-keyword">private void updateView() {handler.postDelayed (new Runnable() { @Override publi c void run() { // TODO 设置进度控件 if (mediaPlayer != null && mediaPlayer.isPlaying()) { tvCurrentT.setText("进度:" + mediaPlayer.getCurrentPosition() / 1000); progressBar.setProgress(mediaPlayer.getCurrentPosition()); } updateView(); } }, 1000); }}
3.布局文件代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <SurfaceView android:id="@+id/surface_view" android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@+id/tv_sound" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#33ffffff" android:padding="8dp" android:text="音量" android:visibility="gone" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="48dp" android:layout_alignParentBottom="true" android:layout_margin="4dp" android:background="#33ffffff"> <TextView android:id="@+id/tv_current" android:layout_width="wrap_content" android:layout_height="24dp" android:layout_marginLeft="4dp" android:textColor="#fff" /> <TextView android:id="@+id/tv_duration" android:layout_width="wrap_content" android:layout_height="24dp" android:layout_alignParentRight="true" android:layout_marginRight="4dp" android:textColor="#fff" /> <ProgressBar android:id="@+id/progress" style="?android:progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="24dp" android:layout_alignParentBottom="true" android:layout_marginLeft="4dp" android:layout_marginRight="4dp" /> RelativeLayout>RelativeLayout>