Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

java - Playing a video in android

Im implementing a video playback in android im completely new to android, and this is the bit of code i have gathered so far.

according to the logic it should play a video. don't know where im doing wrong.

package com.themetanoia.readfilefromsdc;

import java.io.File;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import android.widget.AdapterView.OnItemClickListener;

public class ReadFileFromSDCActivity extends ListActivity {
    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        //Get the file path for external storage
        String a = Environment.getExternalStorageDirectory().getAbsolutePath();
        final String finalPath = a+"/Videos";
        //Define new file function
        File f=new File(finalPath);

        if(f.isDirectory()){              
        //Define arrayadapter
            setListAdapter(new ArrayAdapter<String>(this, R.layout.listview,files));
           //Define listview
            ListView listview = getListView();
            listview.setTextFilterEnabled(true);

           //Onclick list item event
            listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {


                    //get selected items
                    String selectedValue = (String) getListAdapter().getItem(position);         
                    Uri uri =  Uri.parse(finalPath);

                    MediaController mediaController = new MediaController(getBaseContext());
                    mediaController.setAnchorView(mVideoView);

                    Uri video = Uri.parse(finalPath+"/"+selectedValue);
                    mVideoView.setMediaController(mediaController);
                    mVideoView.setVideoURI(video);
                    mVideoView.start();

                }


            });             

        }



}


}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<VideoView
  android:id="@+id/videoView1"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
   />


VideoView vv = (VideoView) findViewById(R.id.videoView1);
uri = "your video uri";
vv.setVideoURI(Uri.parse(uri));
vv.start();

vv.setOnCompletionListener(new OnCompletionListener() {
    @Override
    public void onCompletion(MediaPlayer mp) {
           finish();
  }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...