Android,自定义内容提供商

时间:2012-03-22 10:24:58

标签: android

我有一个可以播放RAW文件夹中的视频文件的VideoActivity,

我还希望能够播放Assets文件夹中的文件及其子文件夹

让我们说Assets / Media / file01.mp4

我知道我需要实现自定义内容提供程序才能这样做..

任何人都可以告诉我如何这样做的方式?

由于

我的视频活动:

package com.qualcomm.QCARSamples.ImageTargets;

import java.io.IOException;

import com.vstory.R;
import com.vstory.util.ResourcesLoader;
import com.vstory.util.MediaManager;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoActivity extends Activity {



    private VideoView _videoView;
    private String value;
    ProgressDialog _dialog;

    private String titleFolder;
    private SurfaceHolder holder;
    protected void onCreate(Bundle savedInstanceState)
    {
        Log.d("VideoActivity::onCreate","Started");
        super.onCreate(savedInstanceState);

        View myPreview = new View(this); 

        // Create Loading Box
        try
        {
            myPreview.setBackgroundColor(android.R.color.black);
            _dialog = ProgressDialog.show(this, "", 
                    "Loading. Please wait...", true);
            setContentView(myPreview);
        }
        catch(Exception e)
        {
            Log.e("VideoActivity::onCreate",e.getMessage());
        }

        // Add Back Button Event While Loading
        _dialog.setOnKeyListener(new OnKeyListener() {

            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_BACK )
                    finish();
                // TODO Auto-generated method stub

                return false;
            }
        });

        // Add Video layout
        setContentView(R.layout.video);
        Log.d("VideoActivity::onCreate","Created");

        _videoView = (VideoView)findViewById(R.id.video_view);


        // Init Player
        InitializeVideoPlayer();
        final Bundle extras = getIntent().getExtras();
        if (extras == null) 
        {   
            Log.e("VideoActivity","extras == null");
            super.finish();
            return;
        }

        titleFolder = extras.getString("titleFolder");



        value = extras.getString("value");

        if (value == null) 
        {   
            Log.e("VideoActivity","value == null");
            super.finish();
            return;
        }

        if ( value.contains("http"))
        {
                Log.d("VideoActivity::onCreate","Url Video");
                Uri uri = Uri.parse(value);
                _videoView.setVideoURI(uri);
                Play(); 
        }
        else
        {
            // Local Resource
            Log.d("VideoActivity::onCreate","Local Video");

            if (titleFolder == null) 
            {
                Log.e("VideoActivity","titleFolder == null");
                super.finish();
                return;
            }


            // ----------------------
            /*
            this.setContentView(R.layout.video);        
           // mVideoView = (VideoView) this.findViewById(R.id.surface_view);        
            holder = _videoView.getHolder();

            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            MediaPlayer player = new MediaPlayer();
            //player.setDisplay(holder);        
            AssetFileDescriptor afd;
            try {
                afd = getAssets().openFd(titleFolder+"/"+value);        
                player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
                player.prepareAsync();
                player.setOnPreparedListener(new OnPreparedListener() {

                 public void onPrepared(MediaPlayer mp) {
                     _dialog.cancel();
                    mp.start();
                 }
              });

            } catch (Exception e) { e.printStackTrace();} 
            */
            // ----------------------
            //player = new MediaPlayer();
            //player.setDataSource(afd.getFileDescriptor());
            //player.prepare();
            //player.start();

            String path;

            value = value.substring(0, value.lastIndexOf('.'));
            int id = getResources().getIdentifier(getPackageName()+":raw/"+value.toLowerCase(), null, null);
            path = "android.resource://" + getPackageName() + "/" + id ;
            //path = loader.LoadFile(value, true);
            _videoView.setVideoPath(path);
            Play();
        }



    }

    private void InitializeVideoPlayer() {
         _videoView.setOnCompletionListener(new OnCompletionListener() {

                public void onCompletion(MediaPlayer mp) {
                    finish();

                }
            });

         _videoView.setOnPreparedListener(new OnPreparedListener() {

                public void onPrepared(MediaPlayer mp) {

                    _dialog.cancel();


                }
            });


         _videoView.setOnErrorListener(new OnErrorListener() {

                public boolean onError(MediaPlayer mp, int what, int extra) {

                    Log.e("VideoActivity::onError","Error in loading movie looking for another source");

                    return false;
                }
            });



    }

    public void Play(){

         try{
             _videoView.setMediaController(new MediaController(this));
             _videoView.requestFocus();              
             _videoView.postInvalidateDelayed(100);
             _videoView.start();
         }
         catch(Exception e){
             Log.e("Video::Play",e.getMessage());
         }
    }


    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        Log.d("VideoActivitiy","onPause");
        super.onPause();
        finish();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        Log.d("VideoActivitiy","onStop");
        super.onStop();
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK){

            if (_videoView.isPlaying() )
                _videoView.stopPlayback();
        }

       this.finish();
       return false;


    }
}

1 个答案:

答案 0 :(得分:1)