With the MediaController set, tap on the video (or the VideoView) to display the controller.
Example code:
package com.example.androidvideoview; import java.io.File; import android.support.v7.app.ActionBarActivity; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.MediaController; import android.widget.Toast; import android.widget.ToggleButton; import android.widget.VideoView; import android.os.Bundle; import android.os.Environment; public class MainActivity extends ActionBarActivity { ToggleButton enableMediaController; VideoView myVideoView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); enableMediaController = (ToggleButton)findViewById(R.id.enableMediaController); myVideoView = (VideoView)findViewById(R.id.myvideoview); myVideoView.setVideoPath(getViewSrc()); myVideoView.requestFocus(); myVideoView.start(); setMediaController(); enableMediaController.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { setMediaController(); }}); } private void setMediaController(){ if(enableMediaController.isChecked()){ myVideoView.setMediaController(new MediaController(this)); }else{ myVideoView.setMediaController(null); } } private String getViewSrc(){ File extStorageDirectory = Environment.getExternalStorageDirectory(); String s = extStorageDirectory.getAbsolutePath() + "/test.mp4"; Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show(); return s; } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.androidvideoview.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="lesapplication.blogspot.com" android:textSize="24dp" android:textStyle="bold" /> <ToggleButton android:id="@+id/enableMediaController" android:layout_width="match_parent" android:layout_height="wrap_content" android:textOn="Disable MediaController" android:textOff="Enable MediaController" android:checked="true"/> <VideoView android:id="@+id/myvideoview" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
To play the file in SD Card, <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> is needed in AndroidManifest.xml, otherwise "Can't play this video".
0 Response to "Using VideoView to play mp4, with MediaController enable/disable"
Posting Komentar