0
点赞
收藏
分享

微信扫一扫

音 + 视频播放

钟罗敏 2023-02-01 阅读 69


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 补充

  • 上述使用的是Android系统提供的API,功能及支持的文件格式相当有限.
  • 具体想实现多媒体文件播放这块请参考​​Vitamio-自行百度.官网打不开,你懂得.​​


举报

相关推荐

0 条评论