记录一下,Android Studio通过使用VideoView控件播放本地的视频工鞥呢
注意两点,是一个是视频的宽度和高度,在VideoView中,有一个初始设置,但是最终还是要跟内部的视频一致
第二个点,不建议存放过大的资源视频视频文件
demo的功能除了播放视频文件之外,还有继续播放的功能
布局代码
java
package com.example.aeke.Media;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import com.example.aeke.Device.Com.SerialPort;
import com.example.aeke.Device.MainMenu;
import com.example.aeke.R;
import androidx.appcompat.app.AppCompatActivity;
public class Video_Player extends AppCompatActivity {
VideoView videoView264;
MediaController MediaController264;
Button btn_DEMO1;
Button btn_DEMO2;
Button btn_DEMO3;
Button btn_DEMO4;
Button btn_DEMO5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.media_video_player);
btn_DEMO1 = (Button)findViewById(R.id.mdeia_video_player__btn_DEMO1);
btn_DEMO2 = (Button)findViewById(R.id.mdeia_video_player__btn_DEMO2);
btn_DEMO3 = (Button)findViewById(R.id.mdeia_video_player__btn_DEMO3);
btn_DEMO4 = (Button)findViewById(R.id.mdeia_video_player__btn_DEMO4);
btn_DEMO5 = (Button)findViewById(R.id.mdeia_video_player__btn_DEMO5);
Init_btn();
}
public void Init_btn()
{
btn_DEMO1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PlayDEMO(1);
}
});
btn_DEMO2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PlayDEMO(2);
}
});
btn_DEMO3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PlayDEMO(3);
}
});
btn_DEMO4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PlayDEMO(4);
}
});
btn_DEMO5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PlayDEMO(5);
}
});
}
public void PlayDEMO(int i)
{
videoView264= (VideoView) findViewById(R.id.mdeia_video_player__videoView264);
String file= Environment.getExternalStorageDirectory().getPath()+ "/raw/h264.mp4";//oppo.3gp视频播放的名字
String uri = "android.resource://" + getPackageName() + "/" + R.raw.demo_1;
switch (i)
{
case 1:
uri = "android.resource://" + getPackageName() + "/" + R.raw.demo_1;
break;
case 2:
uri = "android.resource://" + getPackageName() + "/" + R.raw.demo_2;
break;
case 3:
uri = "android.resource://" + getPackageName() + "/" + R.raw.demo_3;
break;
case 4:
uri = "android.resource://" + getPackageName() + "/" + R.raw.demo_4;
break;
case 5:
uri = "android.resource://" + getPackageName() + "/" + R.raw.demo_5;
break;
}
//String uri = "android.resource://" + getPackageName() + "/" +file;
videoView264.setVideoURI(Uri.parse(uri));
//MediaController mc = new MediaController(this);
MediaController264= new MediaController(this);
MediaController264.setAnchorView(videoView264);//设置控制器 控制的是那一个videoview
videoView264.setMediaController(MediaController264);//设置videoview的控制器为MediaController
videoView264.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
switch (i)
{
case 1:
case 4:
case 2:
case 3:
ShowToast("下一首");
PlayDEMO(i+1);
break;
case 5:
ShowToast("播放完毕");
break;
}
//播放结束后的动作
}
});
videoView264.start();
}
/**
* 提示输出
*/
public void ShowToast(String Message) {
Toast.makeText(this,Message, Toast.LENGTH_LONG).show();
}
}
编写于:2022/12/16 16:19:29
发布 IP 属地:
版权声明
阅读:795 点赞:0 留言:0
记录一下,Android Studio通过使用VideoView控件播放本地的视频工鞥呢
注意两点,是一个是视频的宽度和高度,在VideoView中,有一个初始设置,但是最终还是要跟内部的视频一致
第二个点,不建议存放过大的资源视频视频文件
demo的功能除了播放视频文件之外,还有继续播放的功能
布局代码
java
package com.example.aeke.Media;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import com.example.aeke.Device.Com.SerialPort;
import com.example.aeke.Device.MainMenu;
import com.example.aeke.R;
import androidx.appcompat.app.AppCompatActivity;
public class Video_Player extends AppCompatActivity {
VideoView videoView264;
MediaController MediaController264;
Button btn_DEMO1;
Button btn_DEMO2;
Button btn_DEMO3;
Button btn_DEMO4;
Button btn_DEMO5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.media_video_player);
btn_DEMO1 = (Button)findViewById(R.id.mdeia_video_player__btn_DEMO1);
btn_DEMO2 = (Button)findViewById(R.id.mdeia_video_player__btn_DEMO2);
btn_DEMO3 = (Button)findViewById(R.id.mdeia_video_player__btn_DEMO3);
btn_DEMO4 = (Button)findViewById(R.id.mdeia_video_player__btn_DEMO4);
btn_DEMO5 = (Button)findViewById(R.id.mdeia_video_player__btn_DEMO5);
Init_btn();
}
public void Init_btn()
{
btn_DEMO1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PlayDEMO(1);
}
});
btn_DEMO2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PlayDEMO(2);
}
});
btn_DEMO3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PlayDEMO(3);
}
});
btn_DEMO4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PlayDEMO(4);
}
});
btn_DEMO5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PlayDEMO(5);
}
});
}
public void PlayDEMO(int i)
{
videoView264= (VideoView) findViewById(R.id.mdeia_video_player__videoView264);
String file= Environment.getExternalStorageDirectory().getPath()+ "/raw/h264.mp4";//oppo.3gp视频播放的名字
String uri = "android.resource://" + getPackageName() + "/" + R.raw.demo_1;
switch (i)
{
case 1:
uri = "android.resource://" + getPackageName() + "/" + R.raw.demo_1;
break;
case 2:
uri = "android.resource://" + getPackageName() + "/" + R.raw.demo_2;
break;
case 3:
uri = "android.resource://" + getPackageName() + "/" + R.raw.demo_3;
break;
case 4:
uri = "android.resource://" + getPackageName() + "/" + R.raw.demo_4;
break;
case 5:
uri = "android.resource://" + getPackageName() + "/" + R.raw.demo_5;
break;
}
//String uri = "android.resource://" + getPackageName() + "/" +file;
videoView264.setVideoURI(Uri.parse(uri));
//MediaController mc = new MediaController(this);
MediaController264= new MediaController(this);
MediaController264.setAnchorView(videoView264);//设置控制器 控制的是那一个videoview
videoView264.setMediaController(MediaController264);//设置videoview的控制器为MediaController
videoView264.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
switch (i)
{
case 1:
case 4:
case 2:
case 3:
ShowToast("下一首");
PlayDEMO(i+1);
break;
case 5:
ShowToast("播放完毕");
break;
}
//播放结束后的动作
}
});
videoView264.start();
}
/**
* 提示输出
*/
public void ShowToast(String Message) {
Toast.makeText(this,Message, Toast.LENGTH_LONG).show();
}
}
编写于:2022/12/16 16:19:29
发布 IP 属地:
版权声明
本站内容均来自网络转载或网友提供,如有侵权请及时联系我们删除!本站不承担任何争议和法律责任!
每一个童年的梦想都值得用青春去捍卫!