android学习(一个简单的播放器)


一.实现最简单的播放功能

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 &#123;
    private VideoView videoView;
    private Button playButton,stopButton,finnishbutton;
    MediaController mediaController;//视频控制条
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) &#123;
        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();
    &#125;
&#125;
    

至此,最简单的视频播放功能就完成了。
那么还可以尝试加入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 &#123;
    private List<VideoList> videoLists=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) &#123;
        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() &#123;
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) &#123;
                VideoList videoList=videoLists.get(position);
                String name=videoList.getName();
                Intent intent =new Intent(MainActivity2.this,MainActivity.class);
                intent.putExtra("name",name);
                startActivity(intent);
            &#125;
        &#125;);

    &#125;
    private void initVideoList()&#123;
        for (int i=0;i<1;i++)&#123;
            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);

        &#125;
    &#125;

&#125;

VideoList文件

主要是用来管理item中的图标和标签名

public class VideoList &#123;
    public  int imageId;
    public  String name;


    public VideoList(String name, int imageId) &#123;
        this.name = name;
        this.imageId = imageId;
    &#125;

    public  String getName() &#123;
        return name;
    &#125;

    public  int getImageId() &#123;
        return imageId;
    &#125;
&#125;

VideoListAdapter文件

自定义一个适配器

public class VideoListAdapter extends ArrayAdapter<VideoList> &#123;
    private int resourceId;
    public VideoListAdapter(Context context,
                            int textViewResourceId,
                            List<VideoList> object)&#123;
        super(context,textViewResourceId,object);
        resourceId=textViewResourceId;
    &#125;
    public View getView(int position, View coverView, ViewGroup parent)&#123;
        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;
    &#125;


&#125;

最后,为了实现视频和音频的分开播放,还加了一个菜单,实际上也没啥必要,主要是老师要求这样,真是为难一个CTFer了。本人小菜狗一枚,主业是CTF web狗,大佬勿喷。
项目传送门:地址


文章作者: kento
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 kento !
评论
  目录