简体   繁体   中英

How to add a button to play video using MediaController?

Update on 2/18/2012

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!


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!


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!

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();
    }    
}


} 

Here are my layout files:

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/playVideo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Playing Video"/> 

</LinearLayout> 

Videoview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <VideoView 
        android:id="@+id/surface_view" 
        android:layout_width="320px"
        android:layout_height="240px"
    />

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM