0
点赞
收藏
分享

微信扫一扫

Android之视频播放器

生活的美不止体现在声音上,也呈现在我们的眼睛中,视频播放器的诞生,让我们不仅可以记录下生活中的美好瞬间,还可以收纳世界万物的动听的声音!让我们的五感得到极大的释放,使疲惫的我们放松紧张的心!

所以,今天我想与大家分享一个使用AndroidStudio编写的视频播放器的程序案例,邀大家一起完成哦!

首先,看看它的效果图吧🥳🥳🥳

Android之视频播放器_android

该案例有三个功能,分别是:播放,暂停和停止,并且该案例的视频资源是从本地加载的哦!

它的UI布局代码代码如下:

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

    <SurfaceView
        android:id="@+id/show"
        android:layout_width="match_parent"
        android:layout_height="500dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="40dp"
        android:orientation="horizontal">
        <Button
            android:id="@+id/begin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="播放"/>
        <Button
            android:id="@+id/pause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="暂停"/>
        <Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="停止"/>
    </LinearLayout>
</LinearLayout>

非常简单,大家可以自行简化的哦!😊😊😊

它的java代码如下:

package com.application.animal.Music_Video;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.application.animal.R;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class VideoPlay extends AppCompatActivity implements View.OnClickListener, SurfaceHolder.Callback {

    private MediaPlayer player=null;
    private SurfaceView show;
    private SurfaceHolder holder;  // holder:持有者
    private Button begin,pause,stop;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_play);

        show=findViewById(R.id.show);
        begin=findViewById(R.id.begin);
        pause=findViewById(R.id.pause);
        stop=findViewById(R.id.stop);

        show.setOnClickListener(this);
        begin.setOnClickListener(this);
        pause.setOnClickListener(this);
        stop.setOnClickListener(this);

        holder=show.getHolder();
        holder.addCallback(this);
        holder.setFixedSize(320,220); // 设置视频的分辨率
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.begin:
                player.start();
                break;
            case R.id.pause:
                player.pause();
                break;
            case R.id.stop:
                player.stop();
                break;
            default:break;
        }
    }

    @Override
    public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {

        player=MediaPlayer.create(VideoPlay.this,R.raw.snow);
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setDisplay(holder);
    }

    @Override
    public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (player.isPlaying()){
            player.stop();
        }
        player.release();
    }

//    @Override
//    protected void onPause() {
//        super.onPause();
//        SharedPreferences preferences = getSharedPreferences("audio_data", MODE_PRIVATE);
//        SharedPreferences.Editor editor = preferences.edit();
//        editor.putString("audio", audioData);
//        editor.apply();
//    }
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        SharedPreferences preferences = getSharedPreferences("audio_data", MODE_PRIVATE);
//        String audioData = preferences.getString("audio", null);
//        if (audioData != null) {
//            // 加载音频数据
//        }
//    }
}

这个代码有部分被我注释掉了,大家猜猜它的作用是什么呢?🤔以及我为啥注释他了呢?🤔欢迎大家在评论区留言哦!😎😎😎

举报

相关推荐

0 条评论