基于JAVA的安卓APP设计与开发(实验二 界面跳转和VR视频播放)

本文用于记录自己的实验代码。

实验要求:编写代码,点击第一个界面中的全屏播放跳转到第二个界面,在第二个界面全屏播放视频并设置返回按钮返回半屏播放。

MainActivity代码:

package com.example.video_player;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;

public class MainActivity extends ActionBarActivity {

    private VideoView videoView; // 声明 VideoView 对象用于播放视频
    private Button playButton; // 声明播放按钮
    private Button pauseButton; // 声明暂停按钮
    private Button fullScreenButton; // 声明全屏按钮

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); // 设置当前活动的布局为 activity_main

        videoView = (VideoView) findViewById(R.id.videoView); // 获取布局中的 VideoView 控件
        playButton =(Button) findViewById(R.id.play); // 获取布局中的播放按钮
        pauseButton =(Button) findViewById(R.id.pause); // 获取布局中的暂停按钮
        fullScreenButton =(Button) findViewById(R.id.full); // 获取布局中的全屏按钮

        // 设置视频播放位置
        final Uri videoUri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.myvideo); // 获取视频资源的 URI
        videoView.setVideoURI(videoUri); // 设置 VideoView 播放的视频 URI

        // 为播放按钮设置点击事件监听器
        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                videoView.start(); // 开始播放视频
            }
        });

        // 为暂停按钮设置点击事件监听器
        pauseButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                videoView.pause(); // 暂停播放视频
            }
        });

        // 为全屏按钮设置点击事件监听器
        fullScreenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 创建跳转到全屏播放页面的 Intent
                Intent fullScreenIntent = new Intent(MainActivity.this, FullScreenActivity.class);
                fullScreenIntent.putExtra("videoUri", videoUri.toString()); // 将视频 URI 作为额外数据添加到 Intent 中
                startActivity(fullScreenIntent); // 启动全屏播放页面
            }
        });
    }
}

FullScreenActivity代码:

package com.example.video_player;

import java.io.IOException;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
import android.widget.SeekBar;
import android.view.View;

public class FullScreenActivity extends ActionBarActivity implements SurfaceHolder.Callback {

    private MediaPlayer mediaPlayer; // MediaPlayer 对象用于播放视频
    private SurfaceView surfaceView; // SurfaceView 用于显示视频画面
    private SurfaceHolder surfaceHolder; // SurfaceHolder 用于管理 SurfaceView 的状态
    private Button returnButton; // 返回按钮
    private SeekBar seekBar; // SeekBar 用于显示和调整视频播放进度
    private Handler mHandler = new Handler(); // Handler 用于定时更新 SeekBar 进度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_screen); // 设置当前活动的布局为 activity_full_screen

        surfaceView = (SurfaceView) findViewById(R.id.surfaceView1); // 获取布局中的 SurfaceView 控件
        seekBar = (SeekBar) findViewById(R.id.seekBar1); // 获取布局中的 SeekBar 控件
        surfaceHolder = surfaceView.getHolder(); // 获取 SurfaceView 的 SurfaceHolder 对象
        surfaceHolder.addCallback(this); // 设置 SurfaceHolder 的回调接口为当前 Activity

        returnButton = (Button) findViewById(R.id.quit); // 获取布局中的返回按钮
        returnButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish(); // 点击返回按钮时结束当前 Activity,返回到上一个界面
            }
        });

        // 为 SeekBar 设置监听器,实现对视频播放进度的调整
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (mediaPlayer != null && fromUser) {
                    mediaPlayer.seekTo(progress); // 当用户调整进度时,调用 MediaPlayer 的 seekTo 方法调整播放进度
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    mediaPlayer.pause(); // 当用户触摸 SeekBar 时,暂停视频播放
                }
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (mediaPlayer != null) {
                    mediaPlayer.start(); // 当用户停止触摸 SeekBar 时,恢复视频播放
                }
            }
        });
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mediaPlayer = new MediaPlayer(); // 创建新的 MediaPlayer 对象
        mediaPlayer.setDisplay(surfaceHolder); // 设置 MediaPlayer 的显示区域为 SurfaceView
        // 获取Intent中的视频路径
        Intent intent = getIntent();
        Uri videoUri = Uri.parse(intent.getStringExtra("videoUri")); // 获取 Intent 中的视频 URI

        try {
            mediaPlayer.setDataSource(this, videoUri); // 设置 MediaPlayer 的数据源为视频 URI
            mediaPlayer.prepareAsync(); // 异步准备 MediaPlayer,避免阻塞 UI 线程
        } catch(IOException e) {
            e.printStackTrace(); // 打印异常信息
        }

        // 设置 MediaPlayer 的准备监听器,准备完成后开始播放视频,并设置 SeekBar 的最大值为视频总时长
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mediaPlayer.start(); // 准备完成后开始播放视频
                seekBar.setMax(mediaPlayer.getDuration()); // 设置 SeekBar 的最大值为视频总时长
                updateProgress(); // 开始定时更新 SeekBar 进度
            }
        });

        // 设置 MediaPlayer 的播放完成监听器,确保播放完成时 SeekBar 的进度达到最大值
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                seekBar.setProgress(mediaPlayer.getDuration()); // 播放完成时将 SeekBar 的进度设置为最大值
            }
        });
    }

    // 更新 SeekBar 进度的方法
    private void updateProgress() {
        if (mediaPlayer == null) {
            return; // 若 MediaPlayer 为 null,则返回
        }

        seekBar.setProgress(mediaPlayer.getCurrentPosition()); // 更新 SeekBar 进度为当前播放位置
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                updateProgress(); // 通过 Handler 定时调用自身,实现定时更新 SeekBar 进度
            }
        }, 1000); // 每隔 1000 毫秒更新一次进度
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // 这个方法可以由你来处理任何需要的特殊操作,如 resolution changes
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if(mediaPlayer != null) {
            mediaPlayer.release(); // 释放 MediaPlayer 对象所占资源
            mediaPlayer = null; // 将 MediaPlayer 对象置为 null
        }
    }
}

activity_main.xml:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.video_player.MainActivity" >

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="182dp" />

    <Button
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/videoView1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="128dp"
        android:text="播放" />

    <Button
        android:id="@+id/full"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/play"
        android:layout_alignBottom="@+id/play"
        android:layout_toLeftOf="@+id/pause"
        android:layout_toRightOf="@+id/play"
        android:text="全屏播放" />

    <Button
        android:id="@+id/pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/full"
        android:layout_alignBottom="@+id/full"
        android:layout_alignRight="@+id/videoView"
        android:text="暂停" />

</RelativeLayout>

activity_full_screen.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context="com.example.video_player.MainActivity" >

    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true" />

    <Button
        android:id="@+id/quit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/surfaceView1"
        android:layout_alignRight="@+id/surfaceView1"
        android:text="返回" />

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true" />

</RelativeLayout>

  • 9
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值