【问题标题】:Problems with the video player sample from Android Developers...¿how to specify the path of the video?Android Developers 的视频播放器示例问题...¿如何指定视频的路径?
【发布时间】:2011-11-24 13:01:22
【问题描述】:

我正在尝试使用 Android 开发人员提供的视频播放器示例播放视频:http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/VideoViewDemo.html

我有一个 mp4 视频 (1.mp4) 存储在 assets 文件夹中,但我不知道如何指定路径变量以指向该文件

这是我的代码:

public class DemoVideoActivity extends Activity {

/**
 * TODO: Set the path variable to a streaming video URL or a local media
 * file path.
 */
private String path = "";
private VideoView mVideoView;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.videoview);
    mVideoView = (VideoView) findViewById(R.id.surface_view);

    if (path == "") {
        // Tell the user to provide a media file URL/path.
        Toast.makeText(
                DemoVideoActivity.this,
                "Please edit VideoViewDemo Activity, and set path"
                        + " variable to your media file URL/path",
                Toast.LENGTH_LONG).show();

    } else {

        /*
         * Alternatively,for streaming media you can use
         * mVideoView.setVideoURI(Uri.parse(URLstring));
         */
        mVideoView.setVideoPath(path);
        mVideoView.setMediaController(new MediaController(this));
        mVideoView.requestFocus();

    }
}
}

【问题讨论】:

    标签: android video mp4


    【解决方案1】:

    好的,这是 /res/raw 文件夹中视频的解决方案,(实际上我是从 SO 的某个地方得到的,它适用于我的情况)

    1. 将视频复制到项目的 res/raw 文件夹中。
    2. 视频文件必须采用受支持的格式(3gp、wmv、mp4)。
    3. 通过生成的 R 静态数据进行引用 - 它将删除文件扩展名:R.raw.my_video_file
    VideoView videoView = (VideoView)this.findViewById(R.id.videoView)
    String uri = "android.resource://" + getPackageName() + "/" + R.raw.my_video_file;
    videoView.setVideoURI(Uri.parse(uri));
    videoView.start();
    

    来自资产的视频我从来没有尝试过,但是,如果您使用 MediaPlayer,那么我认为这会对您有所帮助,

    AssetFileDescriptor fileDes = getAssets().openFd(fileName); 
    player.setDataSource(fileDes.getFileDescriptor(),fileDes.getStartOffset(), fileDes.getLength());
    

    这里的 player 是媒体播放器的对象。

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 2016-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-18
      • 1970-01-01
      相关资源
      最近更新 更多