安卓多媒体(视频录制、播放、音效播放)

41 篇文章 2 订阅
27 篇文章 0 订阅

安卓多媒体(视频录制、播放、音效播放)

一、MediaRecorder录制视频

使用MediaRecorder能够编写从设备麦克风与相机捕获音视频、保存音视频并(使用MediaPlayer)进行播放的应用。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sut5OuR5-1623675956185)(C:\Users\23737\AppData\Roaming\Typora\typora-user-images\image-20210614202933768.png)]
在这里插入图片描述

1.申请权限
 <uses-permission android:name="android.permission.RECORD_AUDIO" />
 <uses-permission android:name="android.permission.CAMERA" />

分别是音频麦克风和视频相机的权限

2.在主活动的布局文件上添加TextureView和一个按钮
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".VideoRecording.MediaRecordActivity">

    <TextureView
        android:id="@+id/textureView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/btn_opt"
        android:onClick="onClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
3.在java文件下编辑代码
package com.example.it_train.VideoRecording;

import androidx.appcompat.app.AppCompatActivity;

import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;

import com.example.it_train.R;

import java.io.File;
import java.io.IOException;

public class MediaRecordActivity extends AppCompatActivity {

    private TextureView mTextureView;
    private Button mBtn_opt;
    private Camera camera;
    private MediaRecorder mediaRecorder;

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

        //找到控件
        mTextureView = findViewById(R.id.textureView);
        mBtn_opt = findViewById(R.id.btn_opt);
    }

    public void onClick(View view){
        CharSequence text = mBtn_opt.getText();
        if (TextUtils.equals(text,"开始")) {
            mBtn_opt.setText("结束");

            //初始化相机
            camera = Camera.open();
            /**
             * camera.setDisplayOrientation(90);使用在录制视频的时候,显示的效果不是颠倒的
             * 录制分为  预览 和  真实拍摄  两个阶段
             * 预览时候是反的,使用 camera.setDisplayOrientation(90);来解决
             * 真实拍摄是反的,使用mediaRecorder.setOrientationHint(90);来解决
             */
            camera.setDisplayOrientation(90);
            camera.unlock();
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setCamera(camera);

            //设置视频相关信息
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //设置音频源 麦克风
            mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//设备视频源 摄像头
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//指定视频文件格式

            //设置音频编码
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            //设置视频编码
            mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            //默认是颠倒的,这里要设置回来
            mediaRecorder.setOrientationHint(90);
            //设置视频输出文件
            mediaRecorder.setOutputFile(new File(getExternalFilesDir(""), "a.mp4").getAbsolutePath());
            mediaRecorder.setVideoSize(640, 480);
            //设置摄像头预览的画布
            mediaRecorder.setPreviewDisplay(new Surface(mTextureView.getSurfaceTexture()));

            try {
                //录制视频准备
                mediaRecorder.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //进入了录制
            mediaRecorder.start();


        }else{
            mBtn_opt.setText("开始");

            //回到初始阶段
            mediaRecorder.stop();
            mediaRecorder.release();

            //这里要记得释放camera
            camera.stopPreview();
            camera.release();
        }
    }
}

代码注释已经写得很详细了,不懂的看注释就行

二、MediaPlayer播放视频

1.简介:

MediaPlayer类是媒体框架最重要的组成部分之一。此类的对象能够获取、解码以及播放音频和视频,而且只需极少量设置。它支持多种不同的媒体源,例如:

  • 本地资源
  • 内部URL,例如可能从内容解析器那里获得url
  • 外部网址

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hSTHcNjQ-1623675956187)(C:\Users\23737\AppData\Roaming\Typora\typora-user-images\image-20210614203410240.png)]
在这里插入图片描述

2.在布局文件下添加TextuerView和按钮
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".VideoRecording.VideoActivity">

    <TextureView
        android:id="@+id/textureView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/btn_opt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
3.在java文件下编辑代码
package com.example.it_train.VideoRecording;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;

import com.example.it_train.R;

import java.io.File;
import java.io.IOException;

public class VideoActivity extends AppCompatActivity implements View.OnClickListener,MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener {

    private TextureView textureView;
    private Button btn_opt;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        textureView = findViewById(R.id.textureView);
        btn_opt = findViewById(R.id.btn_opt);

        btn_opt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        CharSequence text = btn_opt.getText();
        if (TextUtils.equals(text, "开始")) {
            btn_opt.setText("结束");
            mediaPlayer = new MediaPlayer();
            //设置准备监听
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.setOnCompletionListener(this);
            try {
                //指定视频源
                mediaPlayer.setDataSource(new File(getExternalFilesDir(""), "a.mp4").getAbsolutePath());
            } catch (IOException e) {
                e.printStackTrace();
            }
            //设置画布
            mediaPlayer.setSurface(new Surface(textureView.getSurfaceTexture()));
            mediaPlayer.prepareAsync();
        } else {
            btn_opt.setText("开始");
            //释放资源
            mediaPlayer.stop();
            mediaPlayer.release();
        }
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mediaPlayer.start();
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        btn_opt.setText("开始");
        mediaPlayer.release();
    }

}

三、使用VideoView来播放视频

1.添加控件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

</androidx.constraintlayout.widget.ConstraintLayout>
2.java文件下编辑代码
package com.enjoy.mediademo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;

public class VideoViewActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_videoview);
        VideoView videoView = findViewById(R.id.videoView);

        //设置一个控制器(可以进行视频播放的操控)
        MediaController mediaController = new MediaController(this);
        //给控制器设置一个监听,设置上一个下一个功能
        mediaController.setPrevNextListeners(this, this);

        videoView.setMediaController(mediaController);

        //设置视频路径
        videoView.setVideoPath(new File(getExternalFilesDir(""), "a.mp4").getAbsolutePath());
        videoView.start();
    }

    @Override
    public void onClick(View v) {
        Log.i("VideoView","====");
    }
}

四、SoundPool播放音效

MediaPlayer虽然也能播放音频,但是它有资源占用量较高、延迟时间较长、不支持多个音频同时播放等缺点。这些缺点决定了MediaPlayer在某些场合的使用情况不会很理想,例如在对时间精准度要求相对较高的场景。而SoundPool一般用来播放密集、急促而又短暂的音效,比如: “滴滴一下,马上出发”。

1.添加控件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.设置适配器
package com.enjoy.mediademo;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> implements View.OnClickListener {

    private final List<SoundActivity.Sound> data;
    private final Context context;
    private final RecyclerView recyclerView;
    private OnItemClickListener listener;

    public MyAdapter(List<SoundActivity.Sound> data, RecyclerView recyclerView, Context context) {
        this.data = data;
        this.recyclerView = recyclerView;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        TextView textView = new TextView(context);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = 18;
        layoutParams.leftMargin = 18;
        textView.setLayoutParams(layoutParams);
        textView.setOnClickListener(this);
        return new MyViewHolder(textView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        ((TextView) holder.itemView).setText(data.get(position).getName());
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onItemClick(recyclerView.getChildAdapterPosition(v));
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    interface OnItemClickListener {
        void onItemClick(int position);
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
3.java文件下编辑
package com.enjoy.mediademo;

import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class SoundActivity extends AppCompatActivity implements MyAdapter.OnItemClickListener {

    private SoundPool soundPool;

    static class Sound {
        String name;
        int soundId;

        public Sound(String name, int soundId) {
            this.name = name;
            this.soundId = soundId;
        }

        public int getSoundId() {
            return soundId;
        }

        public String getName() {
            return name;
        }
    }

    List<Sound> data;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        soundPool = new SoundPool.Builder().setMaxStreams(6).build();

        data = new ArrayList<>();
        data.add(new Sound("a4", soundPool.load(this, R.raw.a4, 1)));
        data.add(new Sound("a5", soundPool.load(this, R.raw.a5, 1)));
        data.add(new Sound("a6", soundPool.load(this, R.raw.a6, 1)));
        data.add(new Sound("a7", soundPool.load(this, R.raw.a7, 1)));
        data.add(new Sound("a8", soundPool.load(this, R.raw.a8, 1)));
        data.add(new Sound("a9", soundPool.load(this, R.raw.a9, 1)));
        MyAdapter myAdapter = new MyAdapter(data, recyclerView, this);
        myAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(myAdapter);

    }

    @Override
    public void onItemClick(int position) {
        Sound sound = data.get(position);
        soundPool.play(sound.getSoundId(),
                1.0f, 1.0f, 1, 0, 1.0f);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        for (Sound datum : data) {
            soundPool.unload(datum.getSoundId());
        }
        soundPool.release();
    }
}

五、一点小零碎

?怎么才能使得APP进入之后就显示权限的申请呢

1.首先在manifest申请权限
<uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
2.在activity的java代码下编辑
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //申请权限
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO},
                100);
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值