用MediaPlayer播放音频
1.创建并装载音频
MediaPlayer player = MediaPlayer.create(this,R.raw.<id>);
MediaPlayer player = MediaPlayer.create(this,Uri.parse(“http://”));
如果要访问网络中的资源,要有网络访问权限
<uses-permission android:name="android.permission.INTERNET"/>
或者用无参构造函数
MediaPlayer player = new MediaPlayer();
player.setDataSource(“”); //磁盘路径
player.prepare();
2.MediaPlayer操作
player.start();
player.stop();
player.pause();
用SoundPool播放音频
1.创建对象
SoundPool(int maxStream, int streamType,int srcQuality)
maxStream指可容纳多少个音频,
streamType指声音类型,可取为STREAM_VOICE_CALL(通话)、STREAM_SYSTEM(系统声音)、STREAM_RING(铃声)、STREAM_MUSIC(音乐)、STREAM_ALARM(闹铃声);
srcQuality指音质,通常为1
SoundPool soundpool = new SoundPool(10,AudioManager.STREAM_SYSTEM, 0);
2.加载音频
public int load(Context context, int resid,int priority);
public int load(String path, int priority);
int soundId = soundpool.load(this, R.raw.ding, 1);
3.播放音频
play(int soundID, float leftVolume, floatrightVolume, int priority, int loop, float rate);
leftVolume,rightVolume通常取1;priority指优先级,通常为1;loop指定循环次数,0为不循环,-1循环;rate指播放速率,正常为1
soundpool.play(soundID, 1, 1, 0, 0, 1);
通常为了管理,使用HashMap
HashMap<Integer, Integer> soundmap =new HashMap<Integer, Integer>();
SoundPool soundpool = new SoundPool(5,AudioManager.STREAM_SYSTEM, 0);
soundmap.put(1, soundpool.load(this,R.raw.login1, 1));
soundmap.put(2, soundpool.load(this,R.raw.login2, 1));
soundmap.put(3, soundpool.load(this,R.raw.login3, 1));
soundmap.put(4, soundpool.load(this,R.raw.login4, 1));
soundmap.put(5, soundpool.load(this,R.raw.login5, 1));
使用VideoView播放音频
现在layout文件中创建该组件,然后
VideoView video = (VideoView)findViewById(R.id.videoView1);
video.setVideoPath(String path);
video.setVideoURI(Uri uri);
video.start();
video.pause();
video.stopPlayback();
使用MediaPlayer和SurfaceView播放视频
1定义SurfaceView
<!--keepScreenOn是否打开屏幕-->
<SurfaceView
android:id="@+id/surfaceView1"
android:keepScreenOn="true"
android:layout_gravity="center"
android:layout_width="300sp"
android:layout_height="400sp" />
2创建MediaPlayer
3将视频输出到SurfaceView
mediaplayer.setDisplay(surfaceview.getHolder());
4利用MediaPlayer控制视频的播放
控制相机拍照
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置全屏显示,放在setContentView之前
final PictureCallback jpeg = new PictureCallback(){
@Override
publicvoid onPictureTaken(byte[] arg0, Camera arg1) {
//TODO Auto-generated method stub
//用来接收数据并且写入文件
}
};
//拍摄的图片一般放在SD卡内
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(this,"请安装SD卡!", Toast.LENGTH_SHORT).show();
}
Camera camera = Camera.open();
Camera.Parameters params = camera.getParameters();
params.setPictureSize(100, 100);
camera.setParameters(params);
camera.startPreview();//开始预览
camera.takePicture(null, null, jpeg);
camera.stopPreview();
camera.release();
Camera.takePicture(ShutterCallback shutter,PictureCallback raw, PictureCallback jpeg)
该函数的详细解释在这里 http://www.cnblogs.com/mengdd/archive/2013/04/06/3002975.html