Site icon IT Tutorial

Android Programlama -37 Arkaplan Video Oynatma

Merhaba arkadaşlar bu gün ki makalemde uygulamanın her hangi bir sayfasında background olarak nasıl video oynata bileceğimizi göreceğiz.

Önceki makalelerime aşağıdaki link’den ulaşabilirsiniz.

https://ittutorial.org/android-programlama-dersleri/

package com.example.user.keepingmeontrack;

import android.widget.Toast;
import android.widget.VideoView;

public class RegisterActivity extends BaseActivity {

private VideoView videoBG;
MediaPlayer mMediaPlayer;
int mCurrentVideoPosition;

@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
//updateUI(currentUser);
}

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);

// Hook up the VideoView to our UI.
videoBG = (VideoView) findViewById(R.id.videoView);

// Build your video Uri
Uri uri = Uri.parse("android.resource://" // First start with this,
+ getPackageName() // then retrieve your package name,
+ "/" // add a slash,
+ R.raw.sec_4); // and then finally add your video resource. Make sure it is stored
// in the raw folder.

// Set the new Uri to our VideoView
videoBG.setVideoURI(uri);
// Start the VideoView
videoBG.start();

// Set an OnPreparedListener for our VideoView. For more information about VideoViews,
// check out the Android Docs: https://developer.android.com/reference/android/widget/VideoView.html
videoBG.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mMediaPlayer = mediaPlayer;
// We want our video to play over and over so we set looping to true.
mMediaPlayer.setLooping(true);
// We then seek to the current posistion if it has been set and play the video.
if (mCurrentVideoPosition != 0) {
mMediaPlayer.seekTo(mCurrentVideoPosition);
mMediaPlayer.start();
}
}
});

// I override onPause(), onResume(), and onDestroy() to properly handle the video when the user get back to app when he exits.

@Override
protected void onPause() {
super.onPause();
// Capture the current video position and pause the video.
mCurrentVideoPosition = mMediaPlayer.getCurrentPosition();
videoBG.pause();
}

@Override
protected void onResume() {
super.onResume();
// Restart the video when resuming the Activity
videoBG.start();
}

@Override
protected void onDestroy() {
super.onDestroy();
// When the Activity is destroyed, release our MediaPlayer and set it to null.
mMediaPlayer.release();
mMediaPlayer = null;
}
}

Oncreate methodun da video ile ilgili ilk düzenlemelerimizi yapıyoruz. Video’nun url’ni ayarlıyoruz ve sonra start methodu ile videoyu başlatıyoruz.  Gerekli kontroller ile videonun döngü içerisinde çalışmasını sağlıyor. Bu konuda ilk derslerimizde gördüğümüz Activity’lerin yaşam döngülerini  tekrardan görüyoruz. OnResume, OnPause ve OnDestroy durumlarında videonun tekrardan çalışması için tekrardan ayarlamaları yapıyoruz.

Bir makalenin daha sonuna geldik, esenle kalın..

Exit mobile version