我想在Android应用中制作流式视频,但是在打开我的videoView页面时出现错误无法播放此视频。在这种情况下,我尝试从API获取视频URL,这是我的代码:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
final PremiumVideo vidx = (PremiumVideo) getIntent().getSerializableExtra("video");
mediaController = new MediaController(this);
mediaController.setAnchorView(mediaController);
String url_video = vidx.source_content; // here iam trying to get URL
Uri uri = Uri.parse(url_video);
videoview.setVideoURI(uri);
videoview.setMediaController(mediaController);
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
videoview.start();
mediaPlayer.setLooping(true);
}
});
videoview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
@Override
public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
return false;
}
});
}
这是我的layout.xml,在此布局中将播放视频:这是我的layout.xml,在此布局中将播放视频:这是我的layout.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="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:gravity="center_horizontal">
<include
android:id="@+id/toolbar"
layout="@layout/view_toolbar" />
<FrameLayout
android:id="@+id/iv_video"
android:layout_width="match_parent"
android:layout_height="210dp"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="@android:color/black"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<FrameLayout
android:id="@+id/frm_controller"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<VideoView
android:id="@+id/vv_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="duration"
android:textColor="@color/white"
android:elevation="2dp"
android:id="@+id/txtDuration"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="bottom|right"
android:background="@color/colorPrimary"
android:alpha=".7"/>
<ImageView
android:id="@+id/imgPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:orientation="vertical"
android:paddingLeft="5dp"
android:background="@color/grey_e4e4e4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nama Artis"
android:textColor="@android:color/black"
android:textSize="20sp"
android:id="@+id/txtNamaPreview"
android:layout_marginTop="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Judul Video"
android:textSize="16sp"
android:id="@+id/txtJudulPreview"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Harga"
android:layout_marginTop="3dp"
android:textSize="16sp"
android:textColor="@android:color/holo_blue_dark"
android:layout_marginBottom="10dp"
android:id="@+id/txtHargaPreview"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:gravity="center"
android:layout_marginTop="10dp">
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:gravity="center"
android:id="@+id/btnBuy"
android:text="Subscribe"
android:textAllCaps="false"
android:textSize="16sp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:background="@drawable/selector_rounded_grey_868686"
android:textColor="@color/black" />
</LinearLayout>
</LinearLayout>