12
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VideoViewでres/drawable内のファイルを再生する

Posted at

androidでVideoViewで動画を再生しようとしたところ「この動画を再生できません」と表示されはまった。

mVideoView.setVideoURI(Uri.parse("res/drawable/ride.mp4"));

などと入力していたが、uriは下記のように指定しなければならないらしい。

mVideoView.setVideoURI(Uri.parse("android.resource://" + this.getPackageName() + "/" + R.drawable.ride));

setVideoPathを使うならUri.parseがいらない。

mVideoView.setVideoPath("android.resource://" + this.getPackageName() + "/" + R.drawable.ride);

一応ソースコードも乗っけておく。
MainActivity.java
public class MainActivity extends Activity {
    private VideoView mVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mVideoView = (VideoView)findViewById(R.id.videoView00);
        mVideoView.setVideoURI(Uri.parse("android.resource://" + this.getPackageName() + "/" + R.drawable.ride));
        mVideoView.start();
    }
}

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <VideoView
        android:id="@+id/videoView00"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_height="wrap_content"
        android:layout_width="match_parent" />
</LinearLayout>
12
11
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?