LenseOnLife
LenseOnLife

Reputation: 179

Want to package a small video in the package for deployment

Is there any way to reference programmatically a very small video file adn include it in teh package - i.e. I don't want to have it separate on the SD card. I am thinking of putting it in the 'raw' package directory.

E.g. MPEG4 called 'video' in 'raw'

Am trying to work out what the correct format for Uri.parse() but it has beaten me. I thought it should be something like R.raw (as used when setting up a media player for audio myMediaPlayer = MediaPlayer.create(this, R.raw.audiocameralive1) - but it doesn't seem to be.

Any suggestions

Oliver

Upvotes: 0

Views: 250

Answers (2)

LenseOnLife
LenseOnLife

Reputation: 179

I see there have been a number of views, so in case anyone is looking for a solution, this is what I eventually did - and seems to work fine. There is probably cleaner way of doing the same but, this one makes sense to me ...

Oliver

public class ShowVideoActivity extends Activity 
    implements SurfaceHolder.Callback, 
    OnErrorListener,
    OnPreparedListener,
    OnCompletionListener
{

/** Called when the activity is first created. */
private MediaPlayer myMediaPlayer;
boolean bolMediaPlayerIsReleased = false;

// The SurfaceHolder and SurfaceView are used to display the video
// By implementing the SurfaceHolder.Callback interface means that we have
// to implement surfaceChanged(), surfaceCreated() and surfaceDestroyed()
private SurfaceView videoSurface;
private SurfaceHolder videoHolder;

Display currentDisplay;

@Override
public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.showvideo);  // Inflate ShowVideo

    // Identify the Surface that will be used to hold the camera image
    videoSurface = (SurfaceView)findViewById(R.id.videosurface);
    // The SurfaceHolder 'monitors' activity on the Surface
    videoHolder = videoSurface.getHolder();
    videoHolder.setKeepScreenOn(true);

    // Data will be Pushed onto the buffers external to the surface
    videoHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    videoHolder.setKeepScreenOn(true);
    // Let the monitor know that 'this' activity is responsible for
    // all the callback functions.
    videoHolder.addCallback(this);
    // It is now up to the 'callbacks' to do any further processing

    myMediaPlayer = MediaPlayer.create(this,R.raw.filename);
    myMediaPlayer.setOnCompletionListener(this);
    myMediaPlayer.setOnErrorListener(this);
    myMediaPlayer.setOnPreparedListener(this);
    myMediaPlayer.setOnCompletionListener(this);
    currentDisplay = getWindowManager().getDefaultDisplay();
    }

// Set up a listener to wait for MediaPlayer End  (Is this PlaybackCompleted()?)
public void onCompletion(MediaPlayer mp)
    {
    Wrapup(mp);
    }

public void surfaceCreated(SurfaceHolder CreatedHolder) {
    // Surface created, now it is possible to set the preview
    myMediaPlayer.setDisplay(CreatedHolder);
    }

public void surfaceDestroyed(SurfaceHolder DestroyedHolder) 
    {
    if (myMediaPlayer != null)
        {
        if (myMediaPlayer.isPlaying() )
            myMediaPlayer.stop();
        myMediaPlayer.release();
        bolMediaPlayerIsReleased = true;
        }
    }

public void surfaceChanged(SurfaceHolder ChangedHolder, int intFormat, int intWidth, int intHeight) 
    {
    if (myMediaPlayer.isPlaying())
        return;
    else
        {
        setVideoSurfaceSize(myMediaPlayer);
        myMediaPlayer.start();
        }
}

public boolean onError(MediaPlayer mPlayer, int intError, int intExtra)
    {
    return false;
    }

public void onPrepared(MediaPlayer mPlayer)
    {
    setVideoSurfaceSize(mPlayer);
    mPlayer.start();
    // From the 'Started' mode, the player can either be 'Stopped', 'Paused' or PlaybackCompleted'
    }  // End onPrepared


public void Wrapup(MediaPlayer mp)
    {
    if (mp != null)
        {
        if (myMediaPlayer.isPlaying() )
            mp.stop();
        mp.release();
        bolMediaPlayerIsReleased = true;
        }

    // Now clean up before terminating.  This is ESSENTIAL
    // If cleanup is NOT done then the surfaceDestroyed will get called
    // and screw up everything
    // Firstly remove the callback
    videoHolder.removeCallback(this);  // Prevents callbacks when the surface is destroyed

    ShowVideoActivity.this.finish();                    
    }
}

Upvotes: 1

techi.services
techi.services

Reputation: 8533

Use Activity.getAssets() to get an AssetManager. The load the file with open.

Upvotes: 0

Related Questions