下面简单讲几种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);
}
}