简体   繁体   中英

sorry, this video cannot be played in videoview?

freinds,

i am using following code to display a mp4 video in my application and facing following problems

i have seen so many posts related to this issue on google and stackoverflow but every one giving his own suggestions and there is no common answer.

1) i cannot see video in emulator 2) in different phone sometime rarly video is played and most of the time it give above message.

my code

VideoView myVideoView = (VideoView)findViewById(R.id.videoview);

      String viewSource ="http://dev.hpac.dev-site.org/sites/default/files/videos/about/mobile.mp4";

      myVideoView.setVideoURI(Uri.parse(viewSource));
      myVideoView.setMediaController(new MediaController(this));
      myVideoView.requestFocus();
      myVideoView.start();

any one guide me what is the solution to this problem any help would be appreciated.

you can make a output stream using your file and get absolute path of stream then put path to video view

    private String getDataSource(String path) throws IOException {
    if (!URLUtil.isNetworkUrl(path)) {
        return path;
    } else {
        URL url = new URL(path);
        URLConnection cn = url.openConnection();
        cn.connect();
        InputStream stream = cn.getInputStream();
        if (stream == null)
            throw new RuntimeException("stream is null");
        File temp = File.createTempFile("mediaplayertmp", "dat");
        temp.deleteOnExit();
        String tempPath = temp.getAbsolutePath();
        @SuppressWarnings("resource")
        FileOutputStream out = new FileOutputStream(temp);
        byte buf[] = new byte[128];
        do {
            int numread = stream.read(buf);
            if (numread <= 0)
                break;
            out.write(buf, 0, numread);
        } while (true);
        try {
            stream.close();
        } catch (IOException ex) {
            Log.e(TAG, "error: " + ex.getMessage(), ex);
        }
        return tempPath;
    }
}

and

public void initVideo() {
    try {

        if (!mVideoView.isPlaying()) {

            if (url > playList.size() - 1) {
                url = 0;
            }
            String[] playurl = (playList.get(url)).split("\\.");
            String urlExtention = playurl[playurl.length - 1];

            if (urlExtention.equals("mp4")) {
                playVideo(playList.get(url));
            } else if (urlExtention.equals("jpg")
                    || urlExtention.equals("jpeg")) {

                Intent intentShedule = new Intent(Default_Player.this,
                        ImagePlayer.class);

                intentShedule.putExtra("imagePath", playList.get(url));
                intentShedule.putExtra("urlValue", url);
                intentShedule.putExtra("playlistType", playlistType);
                intentShedule.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intentShedule);
                finish();
            } else {
                Intent intentShedule = new Intent(Default_Player.this,
                        WebContentView.class);

                intentShedule.putExtra("webPath", playList.get(url));
                intentShedule.putExtra("urlValue", url);
                intentShedule.putExtra("playlistType", playlistType);
                intentShedule.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                startActivity(intentShedule);
                finish();
            }

        }

        mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                System.out.println("set on error listner");

                //do somthing if alert this video can not be played

                return false;
            }
        });

        mVideoView
                .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                    public void onCompletion(MediaPlayer vmp) {

                        playnew();
                    }
                });

    } catch (Exception e) {

    }

    // TODO Auto-generated method stub

}

use on error listner if alert this video can not be played

in eclipse emulator video not displayed that link from website(internet). if you want to play a specific video. then make raw folder and give following path

String path1="android.resource://your package name/"+ R.raw.video name;

Uri uri=Uri.parse(path1);
videoView.setVideoURI(uri);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM