音 + 视频播放

PART_A 音频

一、简述

  • Android提供了MediaPlayer类来实现音频的播放.

  • MediaPlayer类中提供了如下方法,能够很好的控制播放.

    方法作用
    setDataSource()设置音频文件的位置.
    prepare()准备.
    start()开始或继续.
    pause()暂停.
    reset()重置MediaPlayer状态至开始创建时刻.
    seekTo()从指定位置开始播放.
    stop()停止,MediaPlayer对象无法继续播放.
    release()释放MediaPlayer对象的相关资源.
    isPlaying()判断是否正在播放.
    getDuration()获取音频时长.

二、代码

  • xml布局中设置三个Button(分别控制播放、暂停、停止).
<Button
	android:id="@+id/bt_play"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:text="play" />

<Button
	android:id="@+id/bt_pause"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:text="pause" />

<Button
	android:id="@+id/bt_stop"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:text="stop" />
  • 代码中.
private Button[] bts = new Button[3];
private MediaPlayer player = new MediaPlayer(); // 初始化播放器

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_music);
	bts[0] = (Button) findViewById(R.id.bt_play);
	bts[1] = (Button) findViewById(R.id.bt_pause);
	bts[2] = (Button) findViewById(R.id.bt_stop);
	for (int i = 0; i < bts.length; i++) {
		bts[i].setOnClickListener(this);
	}

	initPlayer(); // 初始化音乐路径
}

private void initPlayer() {
	try {
		File file = new File(Environment.getExternalStorageDirectory(), "test.mp3");
		player.setDataSource(file.getPath()); // 指定音频文件的路径
		player.prepare(); // 让MediaPlayer进入到准备状态
	} catch (Exception e) {
		e.printStackTrace();
	}
}

@Override
public void onClick(View v) {
	switch (v.getId()) {
	case R.id.bt_play:
		if (!player.isPlaying()) {
			player.start(); // 开始
		}
		break;
	case R.id.bt_pause:
		if (player.isPlaying()) {
			player.pause(); // 暂停
		}
		break;
	case R.id.bt_stop:
		if (player.isPlaying()) {
			player.reset(); // 停止
			initPlayer();
		}
	}
}

@Override
protected void onDestroy() {
	super.onDestroy();
	if (player != null) {
		player.stop();
		player.release(); // Activity销毁时释放播放器资源
	}
}
  • 当然,别忘了添加权限.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

> PART_B 视频

一、简述

  • Android提供了VideoView类来实现视频的播放.

  • VideoView类中提供了如下方法,能够很好的控制播放.

    方法作用
    setVideoPath()设置视频文件的位置.
    start()开始或继续.
    pause()暂停.
    resume()从头播放.
    seekTo()从指定位置开始播放.
    isPlaying()判断是否正在播放.
    getDuration()获取视频时长.

代码

  • xml布局中设置一个VideoView控件和三个Button(分别控制播放、暂停、停止).
<Button
	android:id="@+id/bt_play"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:text="play" />

<Button
	android:id="@+id/bt_pause"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:text="pause" />

<Button
	android:id="@+id/bt_resume"
	android:layout_width="match_parent"
	android:layout_height="wrap_content"
	android:text="replay" />

<VideoView
	android:id="@+id/vv_video"
	android:layout_width="match_parent"
	android:layout_height="wrap_content" />
  • 代码中
private Button[] bts = new Button[3];
private VideoView vv_video;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_video);
	vv_video = (VideoView) findViewById(R.id.vv_video);
	bts[0] = (Button) findViewById(R.id.bt_play);
	bts[1] = (Button) findViewById(R.id.bt_pause);
	bts[2] = (Button) findViewById(R.id.bt_resume);
	for (int i = 0; i < bts.length; i++) {
		bts[i].setOnClickListener(this);
	}

	initPlayer();
}

private void initPlayer() {
	File file = new File(Environment.getExternalStorageDirectory(), "test.mp4");
	vv_video.setVideoPath(file.getPath()); // 指定视频文件的路径
}

@Override
public void onClick(View v) {
	switch (v.getId()) {
	case R.id.bt_play:
		if (!vv_video.isPlaying()) {
			vv_video.start(); // 开始
		}
		break;
	case R.id.bt_pause:
		if (vv_video.isPlaying()) {
			vv_video.pause(); // 暂停
		}
		break;
	case R.id.bt_resume:
		if (vv_video.isPlaying()) {
			vv_video.resume(); // 重播
		}
		break;
	}
}

@Override
protected void onDestroy() {
	super.onDestroy();
	if (vv_video != null) {
		vv_video.suspend(); // Activity销毁时释放播放器资源
	}
}
  • 当然,与上面播放音频的一样,需要读取SD卡权限.

PART_C 补充

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值