简体   繁体   English

我在android studio中打开视频时出现问题

[英]problem when i open video in android studio

i used this way to make a video layout but the problem is that whenever i try to open the video it says "can't be opened", but when i open the layout the music in my phone stop working,and i checked the format of the video made it the same as the one android phone uses is there another way ? 我使用这种方法制作视频布局,但是问题是,每当我尝试打开视频时,它都会显示“无法打开”,但是当我打开布局时,手机中的音乐停止工作,并且我检查了格式的视频使其与一部Android手机使用的视频相同,还有另一种方法吗? java java的

package com.example.kingbio;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.VideoView;
import androidx.appcompat.app.AppCompatActivity;
public class Main3Activity extends AppCompatActivity  {
VideoView videoView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);
    ImageButton imageButton3 = findViewById(R.id.imageButton3);
    imageButton3.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            openMainActivity();
        }
    });
    videoView =findViewById(R.id.videoView);
    String videopath ="android.resource//"+getPackageName() + "/" 
    +R.raw.first;
    Uri uri= Uri.parse(videopath);
    videoView.setVideoURI(uri);
    videoView.start();
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    intent.setDataAndType(uri, "first/mp4");
    videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            Log.d("video", "setOnErrorListener ");
            return true;
        }
    });
    MediaController mediaController =new MediaController(this);
    videoView.setMediaController(mediaController);
    mediaController.setAnchorView(videoView);
}
public void openMainActivity(){
    Intent intent = new Intent(this,MainActivity.class);
    startActivity(intent);
}
}

xml XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fbf2da"
tools:context="com.example.kingbio.Main3Activity">
<TextView
    android:id="@+id/textView2"
    android:layout_width="136dp"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:text="@string/vid"
    android:textColor="#aa926f"
    android:textSize="50sp" />
<ImageButton
    android:id="@+id/imageButton"
    android:layout_width="129dp"
    android:layout_height="77dp"
    android:layout_toLeftOf="@id/textView2"
    android:layout_below="@id/videoView"
    android:background="#fbf2da"
    app:srcCompat="@drawable/ic_chevron_left_black_24dp"
    tools:ignore="VectorDrawableCompat"
    android:contentDescription="@string/previous"
    android:layout_toStartOf="@id/textView2" />
<ImageButton
    android:id="@+id/imageButton2"
    android:layout_width="129dp"
    android:layout_height="77dp"
    android:background="#fbf2da"
    android:contentDescription="@string/next"
    android:layout_below="@id/videoView"
    android:layout_toRightOf="@id/textView2"
    app:srcCompat="@drawable/ic_chevron_right_black_24dp"
    tools:ignore="MissingConstraints,VectorDrawableCompat"
    android:layout_toEndOf="@id/textView2" />
<VideoView
    android:id="@+id/videoView"
    android:layout_width="match_parent"
    android:layout_height="569dp"
    android:layout_centerInParent="true"
    app:layout_constraintTop_toBottomOf="@+id/textView2"
    app:layout_constraintVertical_bias="0.0" />
<ImageButton
    android:id="@+id/imageButton3"
    android:layout_width="119dp"
    android:layout_height="70dp"
    android:layout_above="@id/videoView"
    android:layout_marginEnd="19dp"
    android:layout_marginRight="19dp"
    android:layout_marginBottom="45dp"
    android:layout_toStartOf="@id/textView2"
    android:layout_toLeftOf="@id/textView2"
    android:background="#fbf2da"
    android:contentDescription="@string/back"
    app:srcCompat="?attr/actionModeCloseDrawable" />
 </RelativeLayout>

i really need some help i don't know what i'm doing wronging, if there is an easy why please help. 我真的需要一些帮助,我不知道我在做什么,如果有一个简单的为什么请帮助。

hi if you have problem in playing video of format .mp4 try this piece of code 您好,如果您在播放.mp4格式的视频时遇到问题,请尝试以下代码

VideoView view = (VideoView)findViewById(R.id.videoView);
String path = "android.resource://" + getPackageName() + "/" + R.raw.video_file;
view.setVideoURI(Uri.parse(path));
view.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试从一个按钮打开另一个 window 时出现问题应用程序完全关闭“Android Studio” - Problem when I try to open another window from a button the app closes completely "Android Studio" 当我在我的 Android Studio 中打开我的模拟器时,构建 output 会弹出并不断显示这个问题 - When I open my emulator in my Android Studio then the build output pop up and showing continuously this problem 写入文件时 Android Studio 中的渲染问题 - Rendering problem in Android Studio when writing files 我在 Android Studio 中切换时遇到问题 - I have a problem with switch in Android Studio 我在制作计时器的Android Studio中遇到问题 - I have problem in android studio with making timer Android Studio - Android Studio 中的问题 - Android Studio - Problem in Android Studio 我是 android 开发新手,在 android 工作室遇到问题 - I am new to android development and I have a problem in android studio 如何在Android Studio中打开开源项目? - How do I open open-source projects in Android Studio? Android Studio中的视频编辑 - Video editing in android studio 当我单击按钮(将订单添加到购物车)时,它可以工作,但是当我尝试打开购物车时,Android Studio出现错误 - When i click on the button (add order to cart) it work but when i try to open the cart, Android Studio gives an error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM