【原文:http://blog.csdn.net/corder_raine/article/details/8317454】
目录
第十三天.Android多媒体开发
13.1 播放音乐
13.1.1 多媒体架构
Ø
基于第三方Packet Video公司的Open Core platform实现
Ø
支持几乎所有通用的音频、视频、静态图像格式,包括:MPEG4、H.264、MP3、AAC、AMR、JPG、PNG、GIF等。
Ø
通过Open Core程序员可以方便快速的开发出想要的多媒体应用程序,例如:音视频的采集,回放,视频会议,实时的流媒体播放等应用。
Ø
Android提供了MediaPlayer和MediaRecorder等上层的Media API给开发人员使用。
Ø
Open Core的代码在Android代码的External/Opencore目录中。
13.1.2 Open Core框架
![图片1.jpg 图片1.jpg](http://www.apkbus.com/data/attachment/forum/201212/05/194649kqicrcpv0pc9qrp3.jpg)
![图片2.jpg 图片2.jpg](http://www.apkbus.com/data/attachment/forum/201212/05/194654ewzqwbb84qbyjpyj.jpg)
13.1.4 音乐播放
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
|
MediaPlayer mediaPlayer = newMediaPlayer();
if
(mediaPlayer.isPlaying()) {
mediaPlayer.reset();
//重置为初始状态
}
mediaPlayer.setDataSource(
"/sdcard/good.mp3"
);
mediaPlayer.prepare();
//缓冲
mediaPlayer.start();
//开始或恢复播放
mediaPlayer.pause();
//暂停播放
mediaPlayer.start();
//恢复播放
mediaPlayer.stop();
//停止播放
mediaPlayer.release();
//释放资源
mediaPlayer.setOnCompletionListener(newMediaPlayer.OnCompletionListener() {
public
void
onCompletion(MediaPlayer arg0) {
//播放完毕调用
mediaPlayer.release();
} });
mediaPlayer.setOnErrorListener(newMediaPlayer.OnErrorListener() {
// 错误处理事件
public
boolean
onError(MediaPlayer player,
int
arg1,
int
arg2) {
mediaPlayer.release();
returnfalse;
} });
|
13.2 播放视频
13.2.1 播放视频
01
02
03
04
05
06
07
08
09
10
11
|
/* 创建VideoView对象 */
final VideoView videoView = (VideoView)findViewById(R.id.VideoView01);
/*设置视频路径*/
videoView.setVideoPath("/sdcard/test.mp4");
/* 设置模式-播放进度条 */
videoView.setMediaController(new MediaController(Activity01.this));
videoView.requestFocus();
/* 开始播放 */
videoView.start();
/* 暂停 */
videoView.pause();
|
13.2.2 音乐/视频播放案例
Ø
音乐播放案例
u
MediaPlayerDemo
u
视频播放案例
u
VideoViewDemo
13.3 录制音频
13.3.1 实现录音功能
第一步:在功能清单文件AndroidManifest.xml中添加录音与写sdcard权限:
1
2
|
<
uses-permission
android:name
=
"android.permission.RECORD_AUDIO"
/>
<
uses-permission
android:name
=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
|
第二步:编写音频刻录代码:
01
02
03
04
05
06
07
08
09
10
11
|
MediaRecorder recorder = newMediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//从麦克风采集声音
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//内容输出格式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//音频编码方式
recorder.setOutputFile(
"/sdcard/lxt008.amr"
);
recorder.prepare();
//预期准备
recorder.start();
//开始刻录
...
recorder.stop();
//停止刻录
recorder.reset();
//重设
recorder.release();
//刻录完成一定要释放资源
|
13.3.2 音视频采集
第一步:在功能清单文件AndroidManifest.xml中添加照相机权限:
1
2
|
<
uses-permission
android:name
=
"android.permission.RECORD_AUDIO"
/>
<
uses-permission
android:name
=
"android.permission.CAMERA"
/>
|
第二步:编写拍照代码:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
//获取窗口服务
Display display =wm.getDefaultDisplay();
//获取屏幕信息
recorder =
new
MediaRecorder();
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//从照相机采集视频
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setVideoSize(display.getWidth(),display.getHeight());
//大小为屏幕的宽和高
recorder.setVideoFrameRate(
3
);
//每秒3帧
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
//设置视频编码方式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(
"/sdcard/lxt008.3gp"
);
recorder.prepare();
//预期准备
recorder.start();
//开始刻录
...
recorder.stop();
//停止刻录
recorder.reset();
//重设
recorder.release();
//刻录完成一定要释放资源
|
13.4 拍摄照片
13.4.1 录音/拍照案例
Ø 录音案例
u RecordAudioDemo
u
Ø 拍照案例
u CameraDemo
13.5 铃声设置
13.5.1 铃声设置:设置各种铃声
01
02
03
04
05
06
07
08
09
10
11
|
//打开系统铃声设置
Intent intent = newIntent(RingtoneManager.ACTION_RINGTONE_PICKER);
//设置铃声类型和title
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_RINGTONE);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,“设置来电铃声");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_ALARM);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
"设置闹铃铃声"
);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE,
"设置通知铃声"
);
//当设置完成之后返回到当前的Activity
startActivityForResult(intent,ButtonAlarm);
|
13.5.2 铃声设置:回调函数
1
2
3
4
5
6
7
8
9
|
/* 当设置铃声之后的回调函数 */
protected
void
onActivityResult(intrequestCode,
int
resultCode, Intent data){
//得到我们选择的铃声
Uri pickedUri =data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
//将我们选择的铃声设置成为默认
RingtoneManager.setActualDefaultRingtoneUri(Activity01.
this
,RingtoneManager.TYPE_RINGTONE, pickedUri);
Uri pickedUri =RingtoneManager.setActualDefaultRingtoneUri(Activity01.
this
,RingtoneManager.TYPE_ALARM, pickedUri);
RingtoneManager.setActualDefaultRingtoneUri(Activity01.
this
,RingtoneManager.TYPE_NOTIFICATION, pickedUri);
}
|
13.5.3 铃声设置:main.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
|
<
LinearLayout
android:orientation
=
"vertical"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
TextView
android:id
=
"@+id/TextView01"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"选择下面按钮来设置铃声"
/>
<
Button
android:id
=
"@+id/ButtonRingtone"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"设置来电铃声"
/>
<
Button
android:id
=
"@+id/ButtonAlarm"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"设置闹钟铃声"
/>
<
Button
android:id
=
"@+id/ButtonNotification"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:text
=
"设置通知铃声"
/>
</
LinearLayout
>
|
13.5.4 铃声设置:AndroidManifest.xml
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
|
<
manifest
xmlns:android
=
"http://schemas.android.com/apk/res/android"
[/align]
package
=
"com.yarin.android.Examples_07_08"
android:versionCode
=
"1"
android:versionName
=
"1.0"
>
<
application
android:icon
=
"@drawable/icon"
android:label
=
"@string/app_name"
>
<
activity
android:name
=
".Activity01"
android:label
=
"@string/app_name"
>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.MAIN"
/>
<
category
android:name
=
"android.intent.category.LAUNCHER"
/>
</
intent-filter
>
</
activity
>
</
application
>
<
uses-permission
android:name
=
"android.permission.INTERNET"
/>
<
uses-permission
android:name
=
"android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
/>
<
uses-sdk
android:minSdkVersion
=
"7"
/>
</
manifest
>
|