当前位置:首页 > 开发 > 移动开发 > 正文

多媒体的浅尝辄止

发表于: 2014-08-12   作者:不懂事的小屁孩   来源:转载   浏览:
摘要: 下面简单讲几种Android的多媒体技术,音频,视频,摄像头,录音。介绍的内容很简单,因为暂时没有用到,这是简单的学习一下。 首先讲下多媒体资源的位置: 1:源文件(即res/raw下或assets下) 2:文件系统(即sdcard) 3:流媒体(即网络) 声音 :声音又游戏声音和歌曲等,游戏声音一般比较小,适合用SoundPool,歌曲比较大,适合用MediaPlayer。
下面简单讲几种Android的多媒体技术,音频,视频,摄像头,录音。介绍的内容很简单,因为暂时没有用到,这是简单的学习一下。

首先讲下多媒体资源的位置:
1:源文件(即res/raw下或assets下)
2:文件系统(即sdcard)
3:流媒体(即网络)


声音 :声音又游戏声音和歌曲等,游戏声音一般比较小,适合用SoundPool,歌曲比较大,适合用MediaPlayer。
SoundPool:

private SoundPool sp;
private int soundId;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
		
	sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
	soundId = sp.load(this, R.raw.note1, 1);
		
	findViewById(R.id.btnPlaySound).setOnClickListener(new View.OnClickListener() {
			
	@Override
	public void onClick(View v) {				
//		声音ID,左声道(0-1.0),右声道,优先级,循环(0不循环,-1一直循环,rate声道频率0.5最慢,1正常,2最快)
//		sp.play(soundID, leftVolume, rightVolume, priority, loop, rate)
		sp.play(soundId, 1, 1, 0, 0, 2.0f);
	}
});
		
		
}

MediaPlayer:
import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {
	
	private MediaPlayer mp=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		findViewById(R.id.btnPlaySong).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mp!=null) {
					mp.start();
				}
			}
		});
	}
	
	@Override
	protected void onResume() {
		
		mp= MediaPlayer.create(this, R.raw.song);
		try {
			mp.prepare();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		if (mp!=null) {
			mp.release();
		}
		super.onPause();
	}

}

视频 :视频文件如果在raw下面的话,只能用MediaPlayer,如果是SD卡或者网络资源的话,可以用VideoView
MediaPlayer:

import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public class SurfaceViewPlayerActivity extends Activity {
	
	private SurfaceView sv;
	private SurfaceHolder holder;
	
	private Callback surfaceHolderCallback=new Callback() {
		
		private MediaPlayer mp;
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			mp.release();
		}
		
		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			mp = MediaPlayer.create(SurfaceViewPlayerActivity.this, R.raw.movie);
			try {
				mp.prepare();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			mp.setDisplay(holder);
			mp.start();
		}
		
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		sv = new SurfaceView(this);
		holder = sv.getHolder();
		holder.addCallback(surfaceHolderCallback);
		
		setContentView(sv);
	}

}

VideoView:
import android.app.Activity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoViewPlayerAvtivity extends Activity {

	
	private VideoView vv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		vv =new VideoView(this);
		setContentView(vv);
		vv.setVideoPath("/mnt/sdcard/movie.mp4");
		vv.setMediaController(new MediaController(this));
	}
}


摄像头 :摄像头的使用分为两种,一种是使用摄像头,另外一种是直接调用系统的摄像头,
使用摄像头:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	private SurfaceView cameraPreview;
	private Camera camera=null;
	private Callback cameraPreviewHolderCallback=new Callback() {
		
		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			stopPreview();
		}
		
		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			startPreview();
		}
		
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		cameraPreview = (SurfaceView) findViewById(R.id.cameraPreview);
		cameraPreview.getHolder().addCallback(cameraPreviewHolderCallback);
		
		findViewById(R.id.btnTakePic).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				camera.takePicture(null, null, new Camera.PictureCallback() {
					
					@Override
					public void onPictureTaken(byte[] data, Camera camera) {
						
						String path=null;
						if ((path =saveTempFile(data))!=null) {
							
							Intent i = new Intent(MainActivity.this, ImagePreviewAty.class);
							i.putExtra("path", path);
							startActivity(i);
						}else{
							Toast.makeText(MainActivity.this, "保存照片失败", Toast.LENGTH_SHORT).show();
						}
					}
				});
			}
		});
	}
	
	private String saveTempFile(byte[] bytes){
		
		try {
			File f = File.createTempFile("img", "");
			FileOutputStream fos = new FileOutputStream(f);
			fos.write(bytes);
			fos.flush();
			fos.close();
			
			return f.getAbsolutePath();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	private void startPreview(){
		camera = Camera.open();
		try {
			camera.setPreviewDisplay(cameraPreview.getHolder());			
			camera.setDisplayOrientation(90);
			camera.startPreview();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void stopPreview(){
		camera.stopPreview();
		camera.release();
	}

}


import java.io.File;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;

public class ImagePreviewAty extends Activity {

	private ImageView iv;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		iv = new ImageView(this);
		setContentView(iv);
		
		String path = getIntent().getStringExtra("path");
		if (path!=null) {
			iv.setImageURI(Uri.fromFile(new File(path)));
		}
	}
}

直接调用系统的摄像头:
import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {
		
	private static final int REQUEST_CODE_TAKE_PICTURE = 1;
	private ImageView iv;
	
	private File currentImageFile = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		iv = (ImageView) findViewById(R.id.iv);
		
		findViewById(R.id.btnStartSystemCamera).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				Environment.getExternalStorageDirectory()  SD卡的根目录
				File dir = new File(Environment.getExternalStorageDirectory(), "pictures");
				if (!dir.exists()) {
					dir.mkdirs();
				}
				currentImageFile = new File(dir, System.currentTimeMillis()+".jpg");
				if (!currentImageFile.exists()) {
					try {
						currentImageFile.createNewFile();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				
				Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(currentImageFile));
				startActivityForResult(i, REQUEST_CODE_TAKE_PICTURE);
			}
		});
	}	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		switch (requestCode) {
		case REQUEST_CODE_TAKE_PICTURE:
			iv.setImageURI(Uri.fromFile(currentImageFile));
			break;
		default:
			break;
		}
		
		super.onActivityResult(requestCode, resultCode, data);
	}

}

录音 :一个简单的录音例子,基本不怎么使用的
录音:

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

	private OnClickListener btnClickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			switch (v.getId()) {
			case R.id.btnStartRecord:
				startRecord();
				
				break;
			case R.id.btnStopRecord:
				stopRecord();
				break;
			}
			
		}
	};
	
	private void startRecord(){
		if (mp==null) {
			
			File dir = new File(Environment.getExternalStorageDirectory(), "sounds");
			if (!dir.exists()) {
				dir.mkdirs();
			}
			
			File soundFile = new File(dir, System.currentTimeMillis()+".amr");
			if (!soundFile.exists()) {
				try {
					soundFile.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			mp = new MediaRecorder();
			mp.setAudioSource(MediaRecorder.AudioSource.MIC);
			mp.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB);
			mp.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
			mp.setOutputFile(soundFile.getAbsolutePath());
			try {
				mp.prepare();
				mp.start();
			} catch (IllegalStateException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private void stopRecord(){
		if (mp!=null) {
			
			mp.stop();
			mp.release();
			mp = null;
		}
	}
	
	private MediaRecorder mp=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		findViewById(R.id.btnStartRecord).setOnClickListener(btnClickListener);
		findViewById(R.id.btnStopRecord).setOnClickListener(btnClickListener);
	}

}

多媒体的浅尝辄止

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
实习期间使用Spring MVC & Spring & hibernate 做了一个简单的内容管理网站。浅尝辄止,谈不上什么
矩阵与SAS IML浅尝辄止 基本概念: 转置矩阵: 如果把mn阶矩阵的第i行改为第i列,第j列改为第j行,
1. 在触控事件中,让ImageView跟随着触控的坐标变化移动,为什么Imageview跟手指触控屏幕的位坐标总
OpenCore的另外一个常用的称呼是PacketVideo,它是Android的多媒体核心。PacketVideo是一家公司的名
Web 上的多媒体指的是音效、音乐、视频和动画。 现代网络浏览器已支持很多多媒体格式。 什么是多媒
多媒体播放框架 多媒体核心框架涉及到的代码文件有: java层: frameworks/base/media/java/android
OMCS开发手册(00) -- 概述(暨OMCS 1.0正式发布!) 作为.NET平台上的开发人员,要开发出一个像
本文来自:http://yueguc.iteye.com/blog/836964 Android 多媒体扫描过程(Android Media Scanner P
OpenCore是Android多媒体框架的核心,所有Android平台的音频视频采集,播放的操作都是通过它来实现
实时流协议RTSP是由RealNetworks和Netscape共同提出的,该协议定义了一对多应用程序如何有效地通过I
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号