android基础---->音频和视频的使用

  Android 在播放音频和视频方面也是做了相当不错的支持,它提供了一套较为完整的API,使得开发者可以很轻松地编写出一个简易的音频或视频播放器。今天我们开始android中音频和视频使用的学习。

目录导航

  1.    音频的播放
  2.    视频的播放
  3.    友情链接

音频的播放

项目结构如下:一个简单的读取sd卡上的音频或者视频资源的应用

在Android 中播放音频文件一般都是使用MediaPlayer 类来实现的,它对多种格式的音频文件提供了非常全面的控制方法,从而使得播放音乐的工作变得十分简单。

一、 初始化音频的播放,调用MediaPlayersetDataSource方法,可以接收音频文件的绝对路径,也可以是http或者rtsp的url:

private final static String TAG = "MainActivity";
private MediaPlayer mediaPlayer = new MediaPlayer();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initMediaPlayer();
}

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

涉及到读取sd卡文件的权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

二、 对音频的一系列操作:

// 播放音频
public void playMusic(View view) {
    if (!mediaPlayer.isPlaying()) {
        mediaPlayer.start(); // 开始播放
    }
}

// 暂停音频
public void pauseMusic(View view) {
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.pause(); // 暂停播放
    }
}

// 停止音频
public void stopMusic(View view) {
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.reset(); // 停止播放
        initMediaPlayer();
    }

三、 在ondestroy方法中释放资源:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mediaPlayer != null) {
        mediaPlayer.stop();
        mediaPlayer.release();
    }
}

四、 layout文件activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="playVideo"
            android:text="PlayVideo" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="pauseVideo"
            android:text="PauseVideo" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="replyVideo"
            android:text="ReplayVideo" />
    </LinearLayout>
</LinearLayout>
View Code

视频的播放

一、 初始化视频的播放:

private VideoView videoView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play_video);
    videoView = (VideoView) findViewById(R.id.video_view);
    initVideoPath();
}

// 初始化播放文件
private void initVideoPath() {
    File file = new File(Environment.getExternalStorageDirectory(), "test.mp4");
    videoView.setVideoPath(file.getPath()); // 指定视频文件的路径
}

 二、 视频的一系列的操作:

// 播放视频
public void playVideo(View view) {
    if (!videoView.isPlaying()) {
        videoView.start(); // 开始播放
    }
}

//暂停视频
public void pauseVideo(View view) {
    if (videoView.isPlaying()) {
        videoView.pause(); // 暂时播放
    }
}

//重新播放视频
public void replyVideo(View view) {
    if (videoView.isPlaying()) {
        videoView.resume(); // 重新播放
    }
}

三、 在ondestroy方法中释放资源:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (videoView != null) {
        videoView.suspend();
    }
}

四、 视频的layout的文件:activity_play_video.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="playVideo"
            android:text="PlayVideo" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="pauseVideo"
            android:text="PauseVideo" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="replyVideo"
            android:text="ReplayVideo" />
    </LinearLayout>
</LinearLayout>
View Code

友情链接

原文地址:https://www.cnblogs.com/huhx/p/mediaAndvideo.html