android mediaplayer SurfaceView

 

 



 

 

 

package com.curiousby.baoyou.cn.DemoMediaPlayer.activity;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.SeekBar;

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import com.curiousby.baoyou.cn.R;
    /*
    *
    *1. VideoView itself is a package for SurfaceView and MediaPlayer
    *2. Realize video list playback
    *
    * If you read local files and network, you need to add permissions
    *
    * */
public class MainActivity extends  Activity {


    private MediaPlayer mediaPlayer;
    private SurfaceView surfaceView;
    private SeekBar seekbar;
    
        // read local file
    private File file=new File("/storage/emulated/legacy/Pictures/Screenshots", "1.mp4");
        // access network video
    private String uri="http://7xqwgk.com1.z0.glb.clouddn.com/mysqlconsole.mp4";
	protected TimerTask timeTask;
	private Timer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.e("baoyou", "file.exists()"+file.exists());
        seekbar = (SeekBar) findViewById(R.id.sb_video_seekbar);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        mediaPlayer = new MediaPlayer();
        //Get SurfaceHolder You can use this interface to operate the Surface in SurfaceView
        SurfaceHolder surfaceHolder = surfaceView.getHolder();
        //Set up the preparation monitor of Meiaplayer
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                //Play when ready
                mediaPlayer.start();
            }
        });
        
        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
           
			//Return when the Surface in SurfaceView is created
            //This method indicates that the Surface has been created, and drawing operations can be performed in this method
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mediaPlayer.reset();
                try {
                //Set the display parameters of the video file image	
                mediaPlayer.setDisplay(holder);
                // file.getAbsolutePath(); //local video
                seekbar.setEnabled(true);
                //Log.e("baoyou", file.length()+""+"=========mediaPlayer.getDuration()=========");
               // seekbar.setMax(Integer.parseInt(file.length()+""));
                mediaPlayer.setDataSource(file.getAbsolutePath());
                //uri web video
                //mediaPlayer.setDataSource(MainActivity.this, Uri.parse (uri));
                //prepare(); indicates that the preparation work is carried out synchronously, (the preparation work is carried out in the UI thread)
                //When playing a network video, if the network does not want to report ARN, this method is not used
                mediaPlayer.prepare();
                //Asynchronous preparation is done in sub-threads. This method is generally used when playing network videos.
                //mediaPlayer.prepareAsync();
                
                timer = new Timer ();
                timeTask=new TimerTask() {
                     
                    @Override
                    public void run() {
                    	Log.e("baoyou", mediaPlayer.getCurrentPosition()+"=========mediaPlayer.getCurrentPosition()=========");
                    	 
                    	 int position = mediaPlayer.getCurrentPosition();  
                         int duration = mediaPlayer.getDuration();  
                           
                         if (duration > 0) {  
                             long pos = seekbar.getMax() * position / duration;  
                             seekbar.setProgress((int) pos);  
                         }  
                         
                    }
                };     
                timer.schedule(timeTask, 0, 1000);
                
                
                } catch (Exception e) {
                    e.printStackTrace ();
                }
            }

            //Trigger this method when the size of the SurfaceView changes
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }
            //Return when Surface is destroyed
            //When the Surface is destroyed, the MediaPlayer is also destroyed
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                if (mediaPlayer!=null) {
                    mediaPlayer.stop();
                    // release resources
                    mediaPlayer.release();
                }
            }
        });
        //Set surfaceView click listener
        surfaceView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        if (mediaPlayer.isPlaying()) {
                            mediaPlayer.pause();
                        } else {
                            mediaPlayer.start();
                        }
                        break;
                }
                //Return True to indicate that the event has been processed
                return true;
            }
        });
        
        
    }

    
    
    
}

 

 

 

 

<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:gravity="center_vertical">

    <RelativeLayout
        android:id="@+id/rl_video_content"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"  >
    
        <SurfaceView
            android:id="@+id/surfaceView"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
        
        <SeekBar
            android:id="@+id/sb_video_seekbar"
            android:layout_above="@+id/ll_video_operation"
             android:layout_width="match_parent"
       		 android:layout_height="wrap_content"
       		 android:background="@color/white"
            >
        </SeekBar>
        <LinearLayout
             android:id="@+id/ll_video_operation"
             android:layout_width="match_parent"
        	 android:layout_height="30dip"
        	 android:gravity="center_horizontal"
        	 android:background="@color/white"
        	  android:layout_alignParentBottom="true"
            >
            <Button
                 android:id="@+id/ll_video_operation_left"
            	 android:layout_width="wrap_content"
        	 	 android:layout_height="wrap_content"
        	 	 android:text="start"
        	 	 android:textColor="@color/black"
        	 	 android:background="@android:color/transparent"
                />
        </LinearLayout>
    </RelativeLayout>
    
    
</RelativeLayout>

 

 

 

 

 

 

 

 

 

 

 

Donate to developers

Driven by interest, I write 免费something with joy and sweat. I hope you like my work and can support it at the same time. Of course, if you have money to support a money field (the love sign in the upper right corner, support Alipay and PayPal donations), if you have no money to support a personal field, thank you.



 
 
 Thank you for your sponsorship, I will do better!

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326570160&siteId=291194637