1、调用系统音乐播放器
[java] view plaincopy
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);
}
或者
[java] view plaincopy
Intent it = new Intent(Intent.ACTION_VIEW);
it.setDataAndType(Uri.parse("/sdcard/111.mp3"), "audio/mp3");
startActivity(it);
2、调用系统视频播放器
[java] view plaincopy
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);
}
或者
[java] view plaincopy
Intent it = new Intent(Intent.ACTION_VIEW);
it.setDataAndType(Uri.parse("/sdcard/1122.mp4"), "video/mp4");
startActivity(it);
播放来自网络多媒体文件
[java] view plaincopy
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
[java] view plaincopy
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;
}
}
}
}