案例:
activity_main.xml
MainActivity.java
package com.example.administrator.cameravideo; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import java.io.File; public class MainActivity extends AppCompatActivity { private static final int MEDIA_TYPE_IMAGE = 1; private static final int MEDIA_TYPE_VIDEO = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //拍照 public void imageCapture(View view) { Intent intent = new Intent(); intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri(MEDIA_TYPE_IMAGE)); startActivityForResult(intent, MEDIA_TYPE_IMAGE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (resultCode) { case RESULT_OK: if (MEDIA_TYPE_IMAGE == requestCode) { //图片 // Uri uri= data.getData(); System.out.println(file); } else if (MEDIA_TYPE_VIDEO == requestCode) { System.out.println(file); System.out.println(data.getData()); } break; } } //录制视频 public void videoCapture(View view) { Intent intent=new Intent(); intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT,getOutputMediaFileUri(MEDIA_TYPE_VIDEO)); //第二个参数为视频质量 intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1); startActivityForResult(intent,MEDIA_TYPE_VIDEO); } File file = null; private Uri getOutputMediaFileUri(int type) { file = getOutputMediaFile(type); return Uri.fromFile(file); } private File getOutputMediaFile(int type) { File file = null; String rootPath = null; switch (type) { case MEDIA_TYPE_IMAGE: rootPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath(); file = new File(rootPath + File.separator + System.currentTimeMillis() + ".jpg"); break; case MEDIA_TYPE_VIDEO: rootPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getPath(); file = new File(rootPath + File.separator + System.currentTimeMillis() + ".mp4"); break; } return file; } }
音频录制
activity_main2.xml
MainActivity.java
package com.example.administrator.cameravideo; import android.app.Activity; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.Button; import java.io.File; import java.io.IOException; public class MainActivity2 extends Activity implements MediaRecorder.OnErrorListener { private MediaRecorder mr; private boolean prepared = false; private Button button_record; private Button button_stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); button_record = (Button) findViewById(R.id.button_record); button_stop = (Button) findViewById(R.id.button_stop); mr = new MediaRecorder(); init(); } private void init() { mr.reset(); mr.setAudioSource(MediaRecorder.AudioSource.MIC); mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + File.separator + System.currentTimeMillis() + ".mp3"; mr.setOutputFile(path); try { mr.prepare(); prepared = true; } catch (IOException e) { e.printStackTrace(); } button_stop.setEnabled(false); } public void recordClick(View view) { if (prepared) { mr.start(); prepared = false; } button_stop.setEnabled(true); button_record.setEnabled(false); } public void stopClick(View view) { mr.stop(); init(); button_stop.setEnabled(false); button_record.setEnabled(true); } @Override protected void onDestroy() { super.onDestroy(); if (mr != null) { mr.release(); } } @Override public void onError(MediaRecorder mr, int what, int extra) { mr.reset(); } }
Vitamio框架
准备工作,下载Vitamio。在AS中添加依赖。必须加上以下权限和内容
在界面Avtivity设置布局前加上
Vitamio.isInitialized(getApplicationContext());
案例一:播放音频
activity_main.xml
MainActivity.java
package com.example.administrator.vitamio; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import java.io.IOException; import io.vov.vitamio.MediaPlayer; import io.vov.vitamio.Vitamio; public class MainActivity extends Activity implements MediaPlayer.OnPreparedListener { private MediaPlayer mediaplayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //加载so类库(通常使用C。C++编写类库) /*if (!LibsChecker.checkVitamioLibs(this)) { return ; } */ Vitamio.isInitialized(getApplicationContext()); setContentView(R.layout.activity_main); initMethodPlayer(); } private void initMethodPlayer() { mediaplayer=new MediaPlayer(this); } public void play(View view) { String path= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)+"/a.mp3"; try { mediaplayer.reset(); mediaplayer.setDataSource(path); mediaplayer.setOnPreparedListener(this); mediaplayer.prepareAsync(); } catch (IOException e) { e.printStackTrace(); } } public void pause(View view) { mediaplayer.pause(); } public void stop(View view) { mediaplayer.stop(); mediaplayer.release(); } @Override public void onPrepared(MediaPlayer mp) { mp.start(); } }
案例二:VideoView播放视频
activity_main2.xml
MainActivity2.java
package com.example.administrator.vitamio; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import io.vov.vitamio.Vitamio; import io.vov.vitamio.widget.MediaController; import io.vov.vitamio.widget.VideoView; public class MainActivity2 extends AppCompatActivity { private VideoView vv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Vitamio.isInitialized(getApplicationContext()); setContentView(R.layout.activity_main2); vv= (VideoView) findViewById(R.id.surface_view); MediaController mc=new MediaController(this); vv.setMediaController(mc); String path= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)+"/Nothing.ifox"; vv.setVideoPath(path); vv.requestFocus(); vv.start(); } }