无法使用视频视图播放视频



我想播放4个不同的视频(按下4个不同的按钮时(,这些视频位于手机的内部存储中。所有按钮都在DashBoardActivity.按下Button时,我通过创建Intent并为每次按钮单击放置一个唯一的整数来启动一个新VideoActivity。这是我的代码DashBoardActivty

Button b1= findViewById(R.id.education_button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(DashboardActivity.this,VideoActivity.class);
i.putExtra(sBUTTON_,1);
startActivity(i);
}
});

这是我VideoActivity中的代码

public class VideoActivity extends AppCompatActivity {
int j;
VideoView vid;
Uri uri;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
Intent i = getIntent();
j = i.getIntExtra(DashboardActivity.sBUTTON_,1);
vid=(VideoView) findViewById(R.id.video);
if(j==1)
uri=Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Movies/MoShow/Educations.mp4");
runOnUiThread(new Runnable() {
@Override
public void run() {
vid.setVideoURI(uri);
vid.start();

}
});
}
}//end of class

当我在物理设备上安装该应用程序时.我收到错误

无法播放此视频

但是,当我看到logcat时,它显示的错误是

?E/ViewRootImpl:非 UI 线程上的辅助功能内容更改。未来的Android版本将抛出异常。 android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。

我很困惑这是否是视频格式(编解码器(问题(因为我在手机屏幕上看到"无法播放此视频"(。我尝试按照这篇文章来解决

CalledFromWrongThreadException

但我得到同样的错误。以下是我的 XML 文件供VideoActivity

<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="#000000"
tools:context="com.example.srini.animtest.VideoActivity">

<VideoView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/video"/>
</RelativeLayout>

试试这个:

vid.setVideoURI(uri);
vid.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
vid.start();
}
});

最新更新