zoukankan      html  css  js  c++  java
  • Android 调用系统播放器,调用系统Camera,自定义视频播放

    1、调用系统音乐播放器

    private void playAudio(String audioPath){	
    		Intent intent = new Intent();  
            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(audioPath), "audio/mp3");
            intent.setComponent(new ComponentName("com.android.music","com.android.music.MediaPlaybackActivity"));
            startActivity(intent);
    		
    	}
    
    //或者
    Intent it = new Intent(Intent.ACTION_VIEW);
    		it.setDataAndType(Uri.parse("/sdcard/111.mp3"), "audio/mp3");
    		startActivity(it);
    

    2、调用系统视频播放器

    private void playVideo(String videoPath){
    		   Intent intent = new Intent(Intent.ACTION_VIEW);
    		   String strend="";
    		   if(videoPath.toLowerCase().endsWith(".mp4")){
    			   strend="mp4";
    		   }
    		   else if(videoPath.toLowerCase().endsWith(".3gp")){
    			   strend="3gp";
    		   }
    		   else if(videoPath.toLowerCase().endsWith(".mov")){
    			   strend="mov";
    		   }
    		   else if(videoPath.toLowerCase().endsWith(".wmv")){
    			   strend="wmv";
    		   }
    		   
    		   intent.setDataAndType(Uri.parse(videoPath), "video/"+strend);
    		   startActivity(intent);
    	   }
    
    //或者
    Intent it = new Intent(Intent.ACTION_VIEW);
    		it.setDataAndType(Uri.parse("/sdcard/1122.mp4"), "video/mp4");
    		startActivity(it);
    

     自定义视频播放控件

        private View vroot;
        private SurfaceHolder sfh;
        private SurfaceView sfv;
        protected MediaPlayer mPlayer;
    
    private void init(){
            if(null==vroot) {
                vroot = LayoutInflater.from(mCtx).inflate(R.layout.video_container, null);
            }
            this.sfv = (SurfaceView)vroot.findViewById(R.id.video);
            sfh = this.sfv.getHolder();
    //        sfh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            sfh.setKeepScreenOn(true);
            MonitorState mo = new MonitorState();
            sfh.addCallback(mo);
    
            mPlayer = new MediaPlayer();
            mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    //        mPlayer.setWakeMode(mCtx.getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
            mPlayer.setOnPreparedListener(mo);
            mPlayer.setOnCompletionListener(mo);
    //        mPlayer.setOnBufferingUpdateListener(mo);
            mPlayer.setOnErrorListener(mo);
        }
    
        class MonitorState implements SurfaceHolder.Callback ,MediaPlayer.OnPreparedListener ,
                MediaPlayer.OnErrorListener,MediaPlayer.OnCompletionListener{
    
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                mPlayer.setScreenOnWhilePlaying(true);
                mPlayer.setDisplay(sfh);
                Logger.d("surfaceCreated");
            }
    
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    
            }
    
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                Logger.d("surfaceDestroyed");
                mPlayer.stop();
    //            mPlayer.release();
                mPlayer.reset();
            }
    
            @Override
            public void onCompletion(MediaPlayer mp) {
                mHandler.sendEmptyMessageDelayed(0,200);
            }
    
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                return false;
            }
    
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        }
    

     布局文件:

    <SurfaceView
            android:id="@+id/video"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    

    视频画面比例缩放处理

      private float mVideoAspectRatio = 16/9;
        private int mSurfaceWidth,mSurfaceHeight;
        private int lastSurfaceWidth,lastSurfaceHeight;
    
        private void changeVideoSize(int layout, float aspectRatio){
            int windowWidth = frameLayout.getWidth(), windowHeight = frameLayout.getHeight();
            if(0==windowWidth || 0==windowHeight)
                return;
            if(0!=lastSurfaceWidth && 0!=lastSurfaceHeight) {
                if (mSurfaceWidth == lastSurfaceWidth && mSurfaceHeight == lastSurfaceHeight)
                    return;
            }
    
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(windowWidth,windowHeight);
            float windowRatio = windowWidth / (float) windowHeight;
            float videoRatio = aspectRatio <= 0.01f ? mVideoAspectRatio : aspectRatio;//1.7
    
            boolean full = layout == VIDEO_LAYOUT_STRETCH;
            lp.width = (full || windowRatio < videoRatio) ? windowWidth : (int) (videoRatio * windowHeight);
            lp.height = (full || windowRatio > videoRatio) ? windowHeight : (int) (windowWidth / videoRatio);
    //        sfv.setLayoutParams(lp);
            sfv.getHolder().setFixedSize(lp.width, lp.height);
    
            lastSurfaceWidth = lp.width;
            lastSurfaceHeight = lp.height;
        }
    public static final int VIDEO_LAYOUT_ORIGIN = 0;
    public static final int VIDEO_LAYOUT_SCALE = 1;
    public static final int VIDEO_LAYOUT_STRETCH = 2;
    public static final int VIDEO_LAYOUT_ZOOM = 3;

     调用画面缩放

     @Override
            public void onPrepared(MediaPlayer mp) {
                mVideoWidth = mp.getVideoWidth();
                mVideoHeight = mp.getVideoHeight();
                mp.start();
                changeVideoSize(VIDEO_LAYOUT_SCALE,(float) mVideoWidth/(float)mVideoHeight);
            }
     @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                Logger.d("surfaceChanged");
                mSurfaceWidth = width;
                mSurfaceHeight = height;
                changeVideoSize(VIDEO_LAYOUT_SCALE,(float) mVideoWidth/(float)mVideoHeight);
            }
    

    播放来自网络多媒体文件

    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    Intent mediaIntent = new Intent(Intent.ACTION_VIEW);
    mediaIntent.setDataAndType(Uri.parse(url), mimeType);
    startActivity(mediaIntent);

    调用系统Camera

    public class SysCamera extends Activity {
    
    	public static final int MEDIA_TYPE_IMAGE = 1;
    	public static final int MEDIA_TYPE_VIDEO = 2;
    
    	private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    	private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;
    
    	private Uri fileUri;
    	Button btvideo,btphoto;
    	
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    	    super.onCreate(savedInstanceState);
            
    	    setContentView(R.layout.syscamera);
    	   
    	    btphoto=(Button)findViewById(R.id.sysPhoto);
    	    btvideo=(Button)findViewById(R.id.sysVideo);
    	    btphoto.setOnClickListener(new Monitor());
    	    btvideo.setOnClickListener(new Monitor());
    	    
    	}
    	
    	private void PhotoIntent(){
    		 // create Intent to take a picture 
    	    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
    	    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    	    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
    	    // start the image capture Intent
    	    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    	}
    	
    	private void VideoIntent(){
    		 //create new Intent
    	    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
    
    	    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);  // create a file to save the video
    	    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name
    	    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high
    	    // start the Video Capture Intent
    	    startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
    	}
    	
    	/** Create a file Uri for saving an image or video */
    	private static Uri getOutputMediaFileUri(int type){
    	      return Uri.fromFile(getOutputMediaFile(type));
    	}
    
    	/** Create a File for saving an image or video */
    	private static File getOutputMediaFile(int type){
    		if (Environment.getExternalStorageState() == null){
    			return null;
    		} 
    	    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
    	              Environment.DIRECTORY_PICTURES), "MyCameraApp");
    	   
    	    // Create the storage directory if it does not exist
    	    if (! mediaStorageDir.exists()){
    	        if (! mediaStorageDir.mkdirs()){
    	            Log.d("MyCameraApp", "failed to create directory");
    	            return null;
    	        }
    	    }
    
    	    // Create a media file name
    	    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    	    File mediaFile;
    	    if (type == MEDIA_TYPE_IMAGE){
    	        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
    	        "IMG_"+ timeStamp + ".jpg");
    	    } else if(type == MEDIA_TYPE_VIDEO) {
    	        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
    	        "VID_"+ timeStamp + ".mp4");
    	    } else {
    	        return null;
    	    }
    
    	    return mediaFile;
    	}
    	
    	 @Override
    	 public void onBackPressed() {
    	    	Intent in=new Intent(this, MyRecorderActivity.class);
    	    	startActivity(in);
    	    	super.onBackPressed();
    	    }
    	 
    	@Override
    	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
    	        if (resultCode == RESULT_OK) {
    	            // Image captured and saved to fileUri specified in the Intent
    	            Toast.makeText(this, "Image saved to:\n" +
    	                     data.getData(), Toast.LENGTH_LONG).show();
    	        } else if (resultCode == RESULT_CANCELED) {
    	            // User cancelled the image capture
    	        } else {
    	            // Image capture failed, advise user
    	        }
    	    }
    
    	    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
    	        if (resultCode == RESULT_OK) {
    	            // Video captured and saved to fileUri specified in the Intent
    	            Toast.makeText(this, "Video saved to:\n" +
    	                     data.getData(), Toast.LENGTH_LONG).show();
    	        } else if (resultCode == RESULT_CANCELED) {
    	            // User cancelled the video capture
    	        } else {
    	            // Video capture failed, advise user
    	        }
    	    }
    	}
    	
    	class Monitor implements OnClickListener{
    
    		@Override
    		public void onClick(View v) {
    			
    				switch(v.getId()){
    				case R.id.sysPhoto:
    					PhotoIntent();
    					break;
    				case R.id.sysVideo:
    					VideoIntent();
    					break;
    				}
    		}
    	}
    }
    

     


  • 相关阅读:
    kafka概述
    Spark网络通信分析
    spark序列化及MapOutputTracker解析
    spark checkpoint详解
    深入理解spark streaming
    spark Listener和metrics实现分析
    Spark SQL catalyst概述和SQL Parser的具体实现
    spark block读写流程分析
    java 分布式实践
    单元测试ppt
  • 原文地址:https://www.cnblogs.com/happyxiaoyu02/p/6818991.html
Copyright © 2011-2022 走看看