如何在videoview中播放视频

时间:2017-12-14 06:58:04

标签: java android video android-videoview

我试过播放视频,但在VideoView中没有显示任何内容 这是我的代码:

 tvvid.setMediaController(new MediaController(AllPostActivity.this));
 tvvid.setVideoURI(Uri.parse("content://media/external/video/media/89962"));
 tvvid.requestFocus();
 tvvid.start();

我已将输出生成为视频file "content://media/external/video/media/89962" 如何使用上述视频路径在videoView中播放视频。

我已尝试通过此路径/storage/emulated/0/WhatsApp/Media/WhatsApp Video/VID-20171208-WA0004.mp4仍然没有显示任何内容

  

打开/ sys / class / power_supply / bms / resistance时出错                                                           java.io.FileNotFoundException:/ sys / class / power_supply / bms / resistance:   打开失败:ENOENT(没有这样的文件或目录)                                                               在libcore.io.IoBridge.open(IoBridge.java:452)                                                               在java.io.FileInputStream。(FileInputStream.java:76)                                                               在java.io.FileInputStream。(FileInputStream.java:103)                                                               在java.io.FileReader。(FileReader.java:66)                                                               在com.motorola.motocare.util.LineReader。(LineReader.java:20)                                                               在com.motorola.motocare.util.LineReader $ 1.(LineReader.java:53)                                                               在com.motorola.motocare.util.LineReader.firstLineReader(LineReader.java:53)                                                               在com.motorola.motocare.util.BatteryUtils.getBatteryResistance(BatteryUtils.java:130)                                                               在com.motorola.motocare.action.BatteryLevelAction.onReceiveImpl(BatteryLevelAction.java:42)                                                               at com.motorola.motocare.util.BackgroundReceiver $ 1.run(BackgroundReceiver.java:14)                                                               在android.os.Handler.handleCallback(Handler.java:746)                                                               在android.os.Handler.dispatchMessage(Handler.java:95)                                                               在android.os.Looper.loop(Looper.java:148)                                                               在android.os.HandlerThread.run(HandlerThread.java:61)                                                            引起:android.system.ErrnoException:open failed:ENOENT(没有这样的文件或   目录)                                                               at libcore.io.Posix.open(Native Method)                                                               在libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)

1 个答案:

答案 0 :(得分:0)

以下是完整的工作代码:

import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.MediaController;
import java.io.File;
public class VideoPlayerActivity extends AppCompatActivity implements View.OnClickListener {

    private File mainMashupVideoFileName = "your_file_path/file_name.mp4";

    private void playMyVideoView(File fileName, boolean isMainMashup) {
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        vv_main_video.setMediaController(mediaController);
        vv_main_video.setVideoURI(Uri.fromFile(fileName));

        vv_main_video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                //Resource has been loaded to the video view player and ready to play.
            }
        });

        vv_main_video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                //Can change play pause icon
            }
        });

        vv_main_video.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //playPauseVideos();
                return false;
            }
        });

        vv_main_video.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                //Do nothing
                return true;
            }
        });

        vv_main_video.requestFocus();
        vv_main_video.start();
    } //End of  run VideoView

    private void playPauseVideos() {
        if (vv_main_video.isPlaying()) {
            vv_main_video.pause();
        } else {
            vv_main_video.requestFocus();
            vv_main_video.start();
        }
    }
}

在清单中添加以下权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

必须检查是否已为您的应用启用了权限。

尝试告诉我您的反馈意见。