如何添加全屏按钮并设置MediaController以在我的应用程序的本地视频中显示?

时间:2018-10-07 20:48:47

标签: android android-mediaplayer fullscreen

我知道有人问过它,但是相信我,我尝试了很多次向我的应用添加不同的代码,但是它总是让我出错,所以我想添加以下内容:

    在视频中显示的
  1. 全屏按钮。
  2. MediaController在视频布局中显示,而不是在应用程序底部

请注意,当我尝试添加以下代码时,它也给我带来了错误,我没有使用主要活动,而是列出了视图项活动

我的应用信息:buildToolsVersion“ 28.0.2”&android.tools.build:gradle:3.2.0'  &gradle包装器版本为gradle 4.6

    <android.support.constraint.ConstraintLayout                                 
    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"
    tools:context="com.codinginflow.videoviewexample.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <VideoView
            android:id="@+id/video_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </FrameLayout>

    </android.support.constraint.ConstraintLayout>

我的原始代码listviewsitem.java

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;


import com.xxx.xxx.config.admob;
import com.xxx.xxx.R;
import com.xxx.xxx.adapter.ListItemAdapter;
import com.xxx.xxx.database.DataBaseHelper;
import com.xxx.xxx.module.Item;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;


public class ListViewsItems extends Activity {

    InterstitialAd  mInterstitialAd;
    AdRequest adRequest;
    private ListView lvItem;
    private ListItemAdapter adapter;
    private List<Item> mItemList;
    private DataBaseHelper mDBHelper;

    Button rateus, shareapp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);

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

        videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.video);

        MediaController mediaController = new MediaController(this);

        mediaController.setAnchorView(videoView);

        videoView.setMediaController(mediaController);

        videoView.start();

        //View adContainer = findViewById(R.id.unitads);
        final String packageName = this.getPackageName();

        LinearLayout linearlayout = (LinearLayout)findViewById(R.id.unitads);
        admob.admobBannerCall(this, linearlayout);

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(SettingsApp.Interstitial);
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                requestNewInterstitial();
            }
        });

        requestNewInterstitial();

        rateus = (Button)findViewById(R.id.rateus2);
        shareapp= (Button) findViewById(R.id.play2);

        rateus.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AppRater.rateLink(ListViewsItems.this);

            }
        });

        shareapp.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ShareApp();
            }
        });

        mDBHelper = new DataBaseHelper(this);
        lvItem = (ListView)findViewById(R.id.listViewtest);
        //Check exists database
        File database = getApplicationContext().getDatabasePath(DataBaseHelper.DBNAME);
        if(false == database.exists()) {
            mDBHelper.getReadableDatabase();
            //Copy db
            if(copyDatabase(this)) {
                //Toast.makeText(this, "Copy database succes", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "Copy data error"+DataBaseHelper.DBLOCATION, Toast.LENGTH_LONG).show();
                return;
            }
        }
        //Get product list in db when db exists
        mItemList = mDBHelper.getListItem();
        //Init adapter
        adapter = new ListItemAdapter(this, mItemList);
        //Set adapter for listview
        lvItem.setAdapter(adapter);
        itemSelected();
    }

    private void ShareApp(){
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");
        String shareBody = "Hey my friend check out this app\n https://play.google.com/store/apps/details?id="+ getPackageName() +" \n";
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here");
        sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody);
        startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }

    private boolean copyDatabase(Context context) {
        try {

            InputStream inputStream = context.getAssets().open(DataBaseHelper.DBNAME);
            String outFileName = DataBaseHelper.DBLOCATION + DataBaseHelper.DBNAME;
            OutputStream outputStream = new FileOutputStream(outFileName);
            byte[]buff = new byte[1024];
            int length = 0;
            while ((length = inputStream.read(buff)) > 0) {
                outputStream.write(buff, 0, length);
            }
            outputStream.flush();
            outputStream.close();
            Log.w("MainActivity","DB copied");
            return true;
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }


    @Override
    public void onBackPressed() {
        AppRater.app_launched(this);
    }

    private void requestNewInterstitial() {
         AdRequest adRequest = new AdRequest.Builder().build();
         mInterstitialAd.loadAd(adRequest);
    } 

    private void itemSelected() {

        lvItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
             Item itemClicked = mItemList.get(position) ;

                try {
                    //Class classe= Class.forName(getPackageName()+".Details");
                    Intent intent=new Intent(ListViewsItems.this,Details.class);
                    intent.putExtra("detail",""+itemClicked.getText());
                    intent.putExtra("title",""+itemClicked.getTitle());
                    startActivityForResult(intent, 1);
                    //startActivity(intent);
                    //finish();
                    if(admob.mCount == admob.nbShowInterstitial) {
                        if (mInterstitialAd.isLoaded()) 
                            mInterstitialAd.show();
                            admob.mCount=0;
                    }
                    ++admob.mCount;
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            };
        });
    }

}

和activity_list_view

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#f4f4f4"
    tools:ignore="HardcodedText" >

    <LinearLayout
        android:id="@+id/liner_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:ignore="UselessParent"
        android:paddingBottom="50dp">

        <VideoView
            android:id="@+id/videoView"
            android:layout_width="wrap_content"
            android:layout_height="360dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/rateus2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="50"
                android:background="#241823"
                android:drawableLeft="@drawable/ic_star"
                android:drawablePadding="2dip"
                android:lineSpacingExtra="10sp"
                android:paddingLeft="30dip"
                android:paddingRight="26dip"
                android:singleLine="true"
                android:text="@string/rate_us"
                android:textColor="@color/menuTextcolor"
                android:textSize="24sp"
                tools:ignore="ButtonStyle" />

            <Button
                android:id="@+id/play2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="50"
                android:background="#260216"
                android:drawableLeft="@drawable/ic_share"
                android:drawablePadding="2dip"
                android:paddingLeft="30dip"
                android:paddingRight="26dip"
                android:singleLine="true"
                android:text="@string/share_it"
                android:textColor="@color/menuTextcolor"
                android:textSize="24sp"
                tools:ignore="ButtonStyle" />
        </LinearLayout>

        <ListView
            android:id="@+id/listViewtest"
            android:layout_width="match_parent"
            android:layout_height="80dp"
            android:background="#f4f4f4"
            android:divider="#f4f4f4"
            android:dividerHeight="6dp"
            android:paddingLeft="8dp"
            android:paddingTop="8dp"
            android:paddingRight="8dp"
            tools:ignore="InefficientWeight"></ListView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/unitads"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

0 个答案:

没有答案