播放视频

时间:2018-04-21 20:20:27

标签: android video mobile android-videoview

我是Android编程的新手。

我想学习如何在我的应用程序中添加视频。

这是我的代码:

package com.example.user_pc.popup;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    VideoView videoViewId = findViewById(R.id.videoViewId);

    videoViewId.setVideoPath("android.resources://" + getPackageName() + "/" + R.raw.video);

     MediaController mController = new MediaController(this);
     mController.setAnchorView(videoViewId);
     videoViewId.setMediaController(mController);

     videoViewId.start();
}
}

我编写此代码来播放视频,但它无效。

有人可以帮我这个吗?

@Kaushal Gosaliya,我想它会对你有所帮助,谢谢!

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<VideoView
    android:id="@+id/videoViewId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user_pc.popup">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Here is my path file

Here is video, I do not know how to share path file with extension like "video.mp4", so I have to send link, if you can convert it to .mp4

2 个答案:

答案 0 :(得分:0)

每当我使用VideoView而不添加互联网权限时,我会收到视频无法播放的错误,只要您的代码或文件路径正确,就会通过此权限解决。

  

android.resources

更改android.resource

简化的Java代码:

VideoView video = (VideoView) findViewById(R.id.video);

android.net.Uri uri = android.net.Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video);

video.setVideoURI(uri);

video.setMediaController(new MediaController(this));

video.start();

在Android Manifiest.xml中添加此权限:

... package="com.example.user_pc.popup">

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

<application ...

第二个选项:

Uri uri = Uri.parse("android.resource://" + getPackageName() + "/raw/" + "video.mp4");

答案 1 :(得分:0)

在您的MainActivity中替换以下行:

videoViewId.setVideoPath("android.resources://" + getPackageName() + "/" + R.raw.video);

用这个:

vv.setVideoPath("android.resource://com.example.user_pc.popup/"+R.raw.panda);