一.实现最简单的播放功能
xml文件效果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Video Player"
android:textSize="20dp"
android:layout_marginTop="20dp"/>
<VideoView
android:id="@+id/videoview"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="40dp"/>
<Button
android:id="@+id/playbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play"
android:layout_gravity="center"
android:textSize="25sp" />
<Button
android:id="@+id/stopbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:textSize="25sp"
android:layout_gravity="center"/>
<TextView
android:text="你的评论:"
android:textSize="25dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/edittext"
android:hint="请输入..."
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/finnishbutton"
android:text="完成"
android:textSize="20dp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
mainactivity文件
在这里使用的播放视频的组件是videoview,实际上videoview还可以实现音频播放,使用也很简单。
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private VideoView videoView;
private Button playButton,stopButton,finnishbutton;
MediaController mediaController;//视频控制条
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
videoView = new VideoView(this);
videoView = (VideoView) findViewById(R.id.videoview);//连接video
mediaController = new MediaController(this);
playButton = (Button) findViewById(R.id.playbutton);
stopButton = (Button) findViewById(R.id.stopbutton);
finnishbutton=(Button)findViewById(R.id.finnishbutton);
editText=(EditText)findViewById(R.id.edittext);
playButton.setOnClickListener(this);
stopButton.setOnClickListener(this);
finnishbutton.setOnClickListener(this);
String uri="android.resource://"+getPackageName()+"/"+R.raw.one;
videoView.setVideoURI(Uri.parse(uri));//绑定视频资源
videoView.setMediaController(mediaController);//绑定进度条
if(v==stopButton)
videoView.stopPlayback();
else if (v==playButton)
videoView.start();
else
Toast.makeText(MainActivity.this, "您的评论:"+str+" 我们已采纳!", Toast.LENGTH_SHORT).show();
}
}
至此,最简单的视频播放功能就完成了。
那么还可以尝试加入ListView组件来选择需要播放的视频,实际上就是对ListView实现一个监听。
加入ListView组件
xml文件源码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity2">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
activity文件源码
这里因为给ListView的item加入了图片,所以,最好是重新定义一个adapter。
package com.example.video;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity2 extends AppCompatActivity {
private List<VideoList> videoLists=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
initVideoList();
VideoListAdapter adapter=new VideoListAdapter(MainActivity2.this,R.layout.videolist,videoLists);
ListView listView=(ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
VideoList videoList=videoLists.get(position);
String name=videoList.getName();
Intent intent =new Intent(MainActivity2.this,MainActivity.class);
intent.putExtra("name",name);
startActivity(intent);
}
});
}
private void initVideoList(){
for (int i=0;i<1;i++){
VideoList a=new VideoList("one", R.drawable.a);
videoLists.add(a);
VideoList b=new VideoList("two",R.drawable.b);
videoLists.add(b);
VideoList c=new VideoList("three",R.drawable.c);
videoLists.add(c);
VideoList d=new VideoList("four",R.drawable.d);
videoLists.add(d);
VideoList e=new VideoList("five",R.drawable.e);
videoLists.add(e);
VideoList f=new VideoList("six",R.drawable.f);
videoLists.add(f);
VideoList g=new VideoList("seven",R.drawable.g);
videoLists.add(g);
VideoList h=new VideoList("eight",R.drawable.h);
videoLists.add(h);
}
}
}
VideoList文件
主要是用来管理item中的图标和标签名
public class VideoList {
public int imageId;
public String name;
public VideoList(String name, int imageId) {
this.name = name;
this.imageId = imageId;
}
public String getName() {
return name;
}
public int getImageId() {
return imageId;
}
}
VideoListAdapter文件
自定义一个适配器
public class VideoListAdapter extends ArrayAdapter<VideoList> {
private int resourceId;
public VideoListAdapter(Context context,
int textViewResourceId,
List<VideoList> object){
super(context,textViewResourceId,object);
resourceId=textViewResourceId;
}
public View getView(int position, View coverView, ViewGroup parent){
VideoList videoList=getItem(position);
View view= LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
ImageView imageView=(ImageView) view.findViewById(R.id.imageview);
TextView textView=(TextView) view.findViewById(R.id.textview);
imageView.setImageResource(videoList.getImageId());
textView.setText(videoList.getName());
return view;
}
}
最后,为了实现视频和音频的分开播放,还加了一个菜单,实际上也没啥必要,主要是老师要求这样,真是为难一个CTFer了。本人小菜狗一枚,主业是CTF web狗,大佬勿喷。
项目传送门:地址