如何在android中播放视频文件?

发布于 2024-11-08 03:00:47 字数 648 浏览 1 评论 0原文

我将视频 MP4 放置到我的域空间中。我有它的公共 URL,现在我想在我的 Android 应用程序中播放它;但不知道我该怎么做。我使用了以下代码,但它不起作用。轨道控制器正在移动,但我在屏幕上看不到任何视频。

public class MPlayer extends Activity{
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playvideo);
    VideoView videoView = new VideoView(MPlayer.this);
    videoView.setMediaController(new MediaController(this));
    videoView.setVideoURI(Uri.parse("http://www.semanticdevlab.com/abc.mp4"));
    videoView.requestFocus();
    videoView.start();
    LinearLayout l = (LinearLayout)findViewById(R.id.mplayer);
    l.addView(videoView);
}
}

I am placed video MP4 to my domain space. I have its public URL, Now i want to play it in my android app; but don't know how can I do this. I used following code which is not working. Track controller is moving but I can't see any video on screen.

public class MPlayer extends Activity{
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playvideo);
    VideoView videoView = new VideoView(MPlayer.this);
    videoView.setMediaController(new MediaController(this));
    videoView.setVideoURI(Uri.parse("http://www.semanticdevlab.com/abc.mp4"));
    videoView.requestFocus();
    videoView.start();
    LinearLayout l = (LinearLayout)findViewById(R.id.mplayer);
    l.addView(videoView);
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

筱武穆 2024-11-15 03:00:47

VideoView 类可以从各种来源(例如资源或内容提供程序)加载图像,负责计算视频的测量值,以便可以在任何布局管理器中使用,并提供各种显示选项,例如缩放和着色。

代码:

videoView = (VideoView)findViewById(R.id.ViewVideo);
videoView.setVideoURI(Uri.parse(“android.resource://” + getPackageName() +”/”+R.raw.video));
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();

如果您想查看源代码:使用 VideoView 播放视频文件在 Android 中

The VideoView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the video so that it can be used in any layout manager, and provides various display options such as scaling and tinting.

Code:

videoView = (VideoView)findViewById(R.id.ViewVideo);
videoView.setVideoURI(Uri.parse(“android.resource://” + getPackageName() +”/”+R.raw.video));
videoView.setMediaController(new MediaController(this));
videoView.requestFocus();
videoView.start();

if you want see source code : Play video file using VideoView in Android

沧笙踏歌 2024-11-15 03:00:47

大多数时候,我使用以下代码:

MediaPlayer mp = new MediaPlayer();
    mp.setDataSource(PATH_TO_FILE);
    mp.prepare();
    mp.start();

有关更多信息,请查看此页面: http://developer.android.com/guide/topics/media/index.html

http://developer.android .com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html

Most of the time, I'm using following code:

MediaPlayer mp = new MediaPlayer();
    mp.setDataSource(PATH_TO_FILE);
    mp.prepare();
    mp.start();

for more information look at this page: http://developer.android.com/guide/topics/media/index.html
and
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/media/MediaPlayerDemo_Video.html

玩物 2024-11-15 03:00:47

我认为这可能会帮助您找到一些解决方案。

mp=new MediaPlayer();                    
mp.setDataSource(path);
mp.setScreenOnWhilePlaying(true);
mp.setDisplay(holder);
mp.prepare();
mp.start();

I think this may help you find some solution.

mp=new MediaPlayer();                    
mp.setDataSource(path);
mp.setScreenOnWhilePlaying(true);
mp.setDisplay(holder);
mp.prepare();
mp.start();
叫思念不要吵 2024-11-15 03:00:47

如果您在模拟器中尝试此操作,则可能必须在真实设备中尝试它,因为有时我也遇到同样的问题。我将无法在模拟器中观看视频,但视频在手机中播放没有任何问题。问题是,我认为是模拟器,而不是你的代码。

If you are trying this in your emulator, you might have to try it in a real device, because sometimes I too use face the same problem. I will not be able to view the video in emulator, but the video will play without any problem in the mobile. the problem is, I think with the emulator, not with your code.

毁梦 2024-11-15 03:00:47

这是我在项目中从网络播放视频文件的方式

需要 Kotlin、AndroidX

在文件缓冲时显示加载对话框,然后开始播放:

private fun playVideo(videopath: String) {
    Log.e("Playing Video File: ", "" + videopath);
    try {
        //Show Loader
        val builder: AlertDialog.Builder = AlertDialog.Builder(this@ScreenCaptureImageActivity);
        builder.setCancelable(false); // if you want user to wait for some process to finish,
        builder.setView(R.layout.layout_loading_dialog);
        progressDialog = builder.create();

        //add Controller
        val mediaController = MediaController(this@ScreenCaptureImageActivity);
        videoView.setMediaController(mediaController)
        //Add URI

        //Uncomment to play from local path
        //videoView.setVideoURI(Uri.parse(videopath))

        //Example Play from Internet
        videoView.setVideoPath("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4")

        videoView.setOnPreparedListener {
            progressDialog!!.dismiss();
            //Start Playback
            videoView.start()
            Log.e(TAG, "Video Started");
        }
    } catch (e: Exception) {
        progressDialog!!.dismiss();
       Log.e(TAG, "Video Play Error :" + e.localizedMessage);
    }
}

Loader XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="20dp">
    <ProgressBar
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:gravity="center"
        android:text="Please wait! This may take a moment." />
</LinearLayout>

**对于网络访问,请在清单,来自 Android P 其必需 **

 <application
        ...
        android:networkSecurityConfig="@xml/network_security_config"
        >

在 res/xml 中添加 network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

在此处输入图像描述

This is how I played a video file from Network in my project

Required Kotlin, AndroidX

Show a loading dialog while the file is buffering and then start playback:

private fun playVideo(videopath: String) {
    Log.e("Playing Video File: ", "" + videopath);
    try {
        //Show Loader
        val builder: AlertDialog.Builder = AlertDialog.Builder(this@ScreenCaptureImageActivity);
        builder.setCancelable(false); // if you want user to wait for some process to finish,
        builder.setView(R.layout.layout_loading_dialog);
        progressDialog = builder.create();

        //add Controller
        val mediaController = MediaController(this@ScreenCaptureImageActivity);
        videoView.setMediaController(mediaController)
        //Add URI

        //Uncomment to play from local path
        //videoView.setVideoURI(Uri.parse(videopath))

        //Example Play from Internet
        videoView.setVideoPath("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4")

        videoView.setOnPreparedListener {
            progressDialog!!.dismiss();
            //Start Playback
            videoView.start()
            Log.e(TAG, "Video Started");
        }
    } catch (e: Exception) {
        progressDialog!!.dismiss();
       Log.e(TAG, "Video Play Error :" + e.localizedMessage);
    }
}

Loader XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="20dp">
    <ProgressBar
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4"
        android:gravity="center"
        android:text="Please wait! This may take a moment." />
</LinearLayout>

**For Network Access add network config in the manifest, from ANdroid P its required **

 <application
        ...
        android:networkSecurityConfig="@xml/network_security_config"
        >

Add network_security_config.xml in res/xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

enter image description here

浴红衣 2024-11-15 03:00:47

您应该在 onResume 中执行此操作,因为在 onCreate 中,VideoView 不知道其大小,无法创建正确的表面来显示视频。

public class MPlayer extends Activity{

VideoView videoView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playvideo);
    videoView = new VideoView(MPlayer.this);
    videoView.setMediaController(new MediaController(this));
    LinearLayout l = (LinearLayout)findViewById(R.id.mplayer);
    l.addView(videoView);
   }

    @Override
    protected void onResume() {
        super.onResume();
videoView.setVideoURI(Uri.parse("http://www.semanticdevlab.com/abc.mp4"));
        videoView.start();
}

You should do it in onResume, because in onCreate VideoView does not knows its size and can't create properly surface to display video.

public class MPlayer extends Activity{

VideoView videoView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playvideo);
    videoView = new VideoView(MPlayer.this);
    videoView.setMediaController(new MediaController(this));
    LinearLayout l = (LinearLayout)findViewById(R.id.mplayer);
    l.addView(videoView);
   }

    @Override
    protected void onResume() {
        super.onResume();
videoView.setVideoURI(Uri.parse("http://www.semanticdevlab.com/abc.mp4"));
        videoView.start();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文