Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add a button to play video using MediaController?
    primarykey
    data
    text
    <p>Update on 2/18/2012</p> <p>I received a reply from my busy instructor on what the button should be and he said it should be the MediaController buttons (Play, Previous, and Next) and I believe it should be shown always on the screen and not a custom button. I don't think there is an onclicklistener for the Play button. Is there a listener for the Play button click (other than onCreate activity in my code examples). I am trying not to have to use intent to start another activity. Thanks!</p> <hr> <p>My homework project is to modify an existing project (which capture audio using MediaPlayer and MediaRecorder classes) by adding a button (I assume this is something I have to create and not the Play button when MediaController gets displayed) to play video when clicked using MediaController. I attempted to do this but the code I added fail to play video. The examples in my class material uses MediaController's Play button, so I would like to learn how to have a custom made button to play video first. Then deal with integrating it into the existing project later. Please point me to existing sample code or guide me in this endeavor. Thanks! </p> <hr> <p>Today, I went ahead and created a separate project that only have a button to play video using MediaController. As expected, it still does not work (Failed to start video, NullPointerException). I present project files below. I am clueless right now. Please point out a thing or two to get me started solving the problem. Thanks again!</p> <pre><code>package com.mypackage; import java.io.File; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.MediaController; import android.widget.Toast; import android.widget.VideoView; import android.content.Context; import android.os.Environment; public class MediaActivity extends Activity { private String path; J private VideoView vd; //private Context context; private String TAG = " "; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //context = this; Button playVideoBtn =(Button)findViewById(R.id.playVideo); playVideoBtn.setOnClickListener(new OnClickListener(){ public void onClick(View v){ try { playVideo(); } catch (Exception ex) { Log.e(TAG, "Failed to Start Playing the video", ex); } } }); } private void playVideo() throws Exception { vd = (VideoView) findViewById(R.id.surface_view); File directoryPath = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); directoryPath.mkdirs(); path = directoryPath.toString() + "/Familyguy_Has_Own_Orbit.3gp"; if (path == "") { // Tell the user to provide a media file URL/path. Toast.makeText(MediaActivity.this, "Please edit MediaActivity, and set path" + " variable to your media file URL/path", Toast.LENGTH_LONG).show(); } else { vd.setVideoPath(path); vd.setMediaController(new MediaController(this)); vd.requestFocus(); vd.start(); } } } </code></pre> <p>Here are my layout files:</p> <p>Main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;Button android:id="@+id/playVideo" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Playing Video"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>Videoview.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" &gt; &lt;VideoView android:id="@+id/surface_view" android:layout_width="320px" android:layout_height="240px" /&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload