[英]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.