想在包中打包一个小视频进行部署

发布于 2024-10-31 19:21:23 字数 279 浏览 0 评论 0原文

有没有什么方法可以以编程方式引用一个非常小的视频文件并将其包含在包中 - 即我不想将其单独放在 SD 卡上。我正在考虑将其放入“原始”包目录中。

例如,MPEG4 在“原始”中称为“视频”

我试图找出 Uri.parse() 的正确格式,但它打败了我。我认为它应该类似于 R.raw (在设置音频媒体播放器时使用 myMediaPlayer = MediaPlayer.create(this, R.raw.audiocameralive1) - 但它似乎不是。

任何建议

奥利弗

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜宝宝 2024-11-07 19:21:23

我看到有很多观点,所以如果有人正在寻找解决方案,这就是我最终所做的 - 并且似乎工作得很好。可能有更干净的方法来做同样的事情,但是,这对我来说很有意义......

奥利弗

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

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();                    
    }
}
静赏你的温柔 2024-11-07 19:21:23

使用 Activity.getAssets() 获取 AssetManager。使用open加载文件。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文