Android学习笔记——Day7
多媒体制作:
1、音频制作:
MediaPlayer curPlayer = MediaPlayer.create(context,id);
相关方法:
start():开始播放音乐
pause():暂停音乐
stop():停止音乐
release():释放音乐
seekTo():定位音乐的位置
相关接口:
OnCompletionListener:音乐播放完之后可以做一些操作
OnSeekCompleteListener:音乐在随机定位时可以做一些操作
public class MusicView extends Activity implements OnClickListener,
OnCompletionListener {
private MediaPlayer mediaPlayer;
private Button btnPause;
public void onCompletion(MediaPlayer mp) {
mp.release();
setTitle("资源已经释放");
}
public void onClick(View view) {
try {
switch (view.getId()) {
case R.id.btnStart1:
mediaPlayer = MediaPlayer.create(this, R.raw.music);
mediaPlayer.setOnCompletionListener(this);
if (mediaPlayer != null)
mediaPlayer.stop();
mediaPlayer.prepare();
mediaPlayer.start();
break;
case R.id.btnStop:
if (mediaPlayer != null) {
mediaPlayer.stop();
}
break;
case R.id.btnPause:
if (mediaPlayer != null) {
if ("播放".equals(btnPause.getText().toString().trim())) {
mediaPlayer.start();
btnPause.setText("暂停");
} else if ("暂停".equals(btnPause.getText().toString().trim())) {
mediaPlayer.pause();
btnPause.setText("播放");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mp3view);
Button btnStart1 = (Button) findViewById(R.id.btnStart1);
Button btnStop = (Button) findViewById(R.id.btnStop);
btnPause = (Button) findViewById(R.id.btnPause);
btnStart1.setOnClickListener(this);
btnStop.setOnClickListener(this);
btnPause.setOnClickListener(this);
}
}
mp3view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btnStart1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text=" 播放MP3资源文件 "/>
<Button android:id="@+id/btnStop" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text=" 停止 "/>
<Button android:id="@+id/btnPause" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text=" 暂停 "/>
</LinearLayout>
支持的音乐格式:
WAV (PCM uncompressed)
AAC (Apple iPod format, unprotected)
MP3 (MPEG-3)
WMA (Windows media audio)
AMR (Speech codec)
OGG (Ogg Vorbis)
MIDI (Instruments)
视频播放:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<VideoView android:id="@+id/video" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_centerInParent="true" />
</LinearLayout>
package org.example.video;
import android.app.Activity;
import android.os.Bundle;
import android.widget.VideoView;
public class Video extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Fill view from resource
setContentView(R.layout.main);
VideoView video = (VideoView) findViewById(R.id.video);
// Load and start the movie
video.setVideoPath("/data/samplevideo.3gp" );
video.start();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.video" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Video" android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
常用方法:
//停止视频
video.stopPlayback();
//暂停视频
video.pause();
//获得视频的长度
int length = video.getDuration();
//获得视频的当前进度
int position = video.getCurrentPosition();
//拖动视频到某个位置
video.seekTo(0);
将视频传到手机设置的命令:adb push d:/music.3gp /data/music.3gp