Trying to play video from raw folder (VideoView)

前端 未结 4 1729
悲&欢浪女
悲&欢浪女 2020-12-05 10:45

I can play a video from the Internet by inserting the URL like below:

mPath   = Uri.parse(\"http://commonsware.com/misc/test2.3gp\");
mVid.setVideoURI(mPath)         


        
相关标签:
4条回答
  • 2020-12-05 11:20

    I had the same problem. This worked for me:

    Uri video = Uri.parse("android.resource://com.pac.myapp/raw/master");
    

    So as you see you have 3 parts of the uri: 1) "android.resource://" 2) "com.pac.myapp" 3) "/raw/master"

    "master" is the name of your video

    0 讨论(0)
  • 2020-12-05 11:36

    this works for me

     String videoName = nameWithoutFileExtention;
    
     int id = getResources().getIdentifier(videoName, "raw", getActivity().getPackageName());
    
     final String path = "android.resource://" + getActivity().getPackageName() + "/" + id;
    
     vvBgVideo.setVideoURI(Uri.parse(path));
    
    0 讨论(0)
  • 2020-12-05 11:38
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn=(Button)this.findViewById(R.id.playvideo);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    VideoView vid=(VideoView)findViewById(R.id.video);
                    vid.setMediaController(new MediaController(MainActivity.this));
                    Uri video = Uri.parse("android.resource://com.example.tenzinthinley.video/raw/ed");
                    vid.setVideoURI(video);
                    vid.requestFocus();
                    vid.start();
                }
            });
        }
    }
    

    Change my name if this doesn't works. 'ed' is the video file name.

    0 讨论(0)
  • 2020-12-05 11:43

    You just need to locate song in raw folder under resource folder. if it is link then

     private String urlVideo ="http://www.pocketjourney.com/downloads/pj/video/famous.3gp";
    
        //Make uri from song located in raw folder
            Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"
                    + R.raw.shakebooty);
            player.setUpVideoFrom(uri.toString());
    
            public void setUpVideoFrom(String source) throws IllegalArgumentException,
                    IllegalStateException, IOException {
    
                mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    
    //Only to check if you want to play song from url
                if (source.contains("http"))
                {
                mPlayer.setDataSource(source);
                }
    //If want to play song from uri you created from song in raw folder
            else {
                 mPlayer.setDataSource(ctx, source);
                 }
    
            }
    

    Enjoy playing video in surface view

    0 讨论(0)
提交回复
热议问题