android 怎样实现锁屏界面上有歌曲的快进暂停下一首的按钮控制,Android VideoView播放视频控制:开始、暂停、快进(3)...

Android VideoView播放视频控制:开始、暂停、快进(3)

本文在附录参考文章(1)的基础上增加的Android VideoView播放视频时候的控制。控制主要依赖VideoView的start(开始),pause(暂停),seekTo(快进,跳到某一个时间点开始)

先写一个布局文件(增加控制按钮):

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.example.videoview.MainActivity" >

android:id="@+id/videoView"

android:layout_width="match_parent"

android:layout_height="match_parent" />

android:id="@+id/linearLayout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="bottom"

android:orientation="horizontal" >

android:id="@+id/start"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="开始" />

android:id="@+id/pause"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="暂停" />

android:id="@+id/seekto"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="3秒处" />

Java代码:

package com.example.videoview;

import java.io.File;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.widget.Button;

import android.widget.VideoView;

public class MainActivity extends Activity implements Button.OnClickListener {

private VideoView videoView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

videoView = (VideoView) findViewById(R.id.videoView);

// 获得的path等于:/storage/emulated/0/DCIM

File path = Environment

.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

// 拼接完整路径

File f = new File(path, "/Camera/test.mp4");

// 此时的f.getAbsolutePath()=/storage/emulated/0/DCIM//\Camera/test.mp4

videoView.setVideoPath(f.getAbsolutePath());

// 开始播放视频

// videoView.start();

// VideiView获焦点

// videoView.requestFocus();

Button start = (Button) findViewById(R.id.start);

start.setOnClickListener(this);

Button pause = (Button) findViewById(R.id.pause);

pause.setOnClickListener(this);

Button seekto = (Button) findViewById(R.id.seekto);

seekto.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

// 开始播放

case R.id.start:

videoView.start();

break;

// 暂停

case R.id.pause:

videoView.pause();

break;

// 3秒处

case R.id.seekto:

videoView.seekTo(3 * 1000);

break;

default:

break;

}

}

}

附录参考文章:

【1】《Android VideoView播放视频(1)》链接地址:http://blog.csdn.net/zhangphil/article/details/49885697

【2】《Android VideoView播放在线视频(2)》链接地址:http://blog.csdn.net/zhangphil/article/details/49885995

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些Android Studio代码来实现您所需的功能。在这个程序中,我们将使用一个VideoView控件来播放视频,并在界面上添加三个按钮播放暂停和停止。请按照以下步骤操作: 首先,在您的布局文件中,添加一个VideoView控件和三个按钮: ``` <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnPlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play" /> <Button android:id="@+id/btnPause" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pause" /> <Button android:id="@+id/btnStop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop" /> ``` 接下来,在您的Java代码中,获取VideoView按钮控件的引用,并为每个按钮添加单击事件监听器。在单击事件监听器中,实现相应的视频播放暂停和停止功能: ``` public class MainActivity extends AppCompatActivity { private VideoView videoView; private Button btnPlay, btnPause, btnStop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); videoView = findViewById(R.id.videoView); btnPlay = findViewById(R.id.btnPlay); btnPause = findViewById(R.id.btnPause); btnStop = findViewById(R.id.btnStop); btnPlay.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { videoView.start(); } }); btnPause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { videoView.pause(); } }); btnStop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { videoView.stopPlayback(); } }); } } ``` 现在您已经可以在Android Studio中创建一个具有视频播放暂停和停止功能的程序了。希望这对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值