I have the following error:
Can't play this video.
as you can see in the image below:
LoginAppActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_app);
emailInput = (EditText) findViewById(R.id.Email);
passwordInput = (EditText) findViewById(R.id.Password);
btnLogin = (Button) findViewById(R.id.buttonlogin);
videoview = (VideoView) findViewById(R.id.bgVideo);
String videoPath = "android:resource//"+getPackageName()+"/"+R.raw.bg3;
String vidAddress = "https://archive.org/download/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4";
Uri vidUri = Uri.parse(vidAddress);
Uri uri = Uri.parse(videoPath);
videoview.setVideoURI(uri);
videoview.requestFocus();
videoview.start();
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.setLooping(true);
}
});
this for activity login app xml activity_login_app.xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView
android:id="@+id/bgVideo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_gravity="center"
/>
</RelativeLayout>
Yes I agreed that in VdeoView we can not see all videos and I get an error
Can't play this video
So, I have alternative ways. These are steps to load video:
Step 1
Add this dependency in app level build.gradle
:
// Exo-Player
implementation 'com.google.android.exoplayer:exoplayer:2.15.0'
Step 2
Add this view to your activity/fragment .xml
file:
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/playerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#A6000000" />
Step 3
Add this code in your .java
file to load/show video:
// Declare variables
private PlayerView playerView;
private SimpleExoPlayer player;
// Initialize variable
playerView = findViewById(R.id.playerView);
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector =
new DefaultTrackSelector(videoTrackSelectionFactory);
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
// Set player and play video
playerView.setPlayer(player);
MediaSource mVideoSource = buildMediaSource(Uri.parse(videoUri)); // videoUri is URL.
player.prepare(mVideoSource); // set ready
player.setPlayWhenReady(true); // set true to play when our player is reay to use.
Add below method in the same file:
private MediaSource buildMediaSource(Uri mUri) {
// Measures bandwidth during playback. Can be null if not required.
DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, getString(R.string.app_name)), bandwidthMeter);
// This is the MediaSource representing the media to be played.
return new ProgressiveMediaSource.Factory(dataSourceFactory)
.createMediaSource(uri);
}
I hope it will work like a charm.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.