Android :tutoriel sur la Video Streaming en Android
Création de la base Mysql
Dans ce tutoriel, nous allons voir comment diffuser une vidéo en streaming en utilisant un MediaController .Pour se faire il faut afficher notre video dans un VideoView dans notre application Android.
Création de la base Mysql
MediaController est un contrôleur pour Media Player, il permet de faire le contrôle de notre video à travers les fonctions comme lecture / pause, avance rapide etc. Les vidéos peuvent être chargées à partir de diverses sources telles que les serveurs distants et les cartes SD interne.
Nous allons créer un bouton et après le clic nous allons commencer à diffuser une vidéo à distance dans une nouvelle activité.
Créez un nouveau projet dans Eclipse File> New> Android Application Project.
Remplissez les détails et le nom de votre projet.
Nom de l'application: VideoStream
Nom du projet: VideoStream
Nom du package: com.tuto.videostream
Ouvrez votre MainActivity.java et collez le code suivant.
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from video_main.xml
setContentView(R.layout.activity_main);
// Locate the button in activity_main.xml
button = (Button) findViewById(R.id.MyButton);
// Capture button clicks
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,VideoViewActivity.class);
startActivity(myIntent);
}
});} }
Dans cette activité, nous avons créé un bouton et après le clic il va ouvrir une nouvelle activité.
Dans ce tutoriel, nous allons lancer un fichier vidéo hébergé sur ce serveur:
http://www.androidbegin.com/tutorial/AndroidCommercial.3gp
Ensuite, créez un fichier XML pour votre MainActivity
Nommez votre fichier xml activity_main.xml et collez le code suivant.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/button" />
</RelativeLayout>
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoViewActivity extends Activity {
// Declare variables
ProgressDialog pDialog;
VideoView videoview;
// Insert your Video URL
String VideoURL = "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the layout from video_main.xml
setContentView(R.layout.videoview_main);
// Find your VideoView in your video_main.xml layout
videoview = (VideoView) findViewById(R.id.VideoView);
// Execute StreamVideo AsyncTask
// Create a progressbar
pDialog = new ProgressDialog(VideoViewActivity.this);
// Set progressbar title
pDialog.setTitle("Video Streaming");
// Set progressbar message
pDialog.setMessage("Buffering...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
// Show progressbar
pDialog.show();
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
VideoViewActivity.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pDialog.dismiss();
videoview.start();
}
});
}}
Création de la base Mysql
Dans cette activité, nous allons montrer une barre de progression jusqu'à ce que la vidéo sera partiellement chargé puis la vidéo sera lancé dans un VideoView. Nous avons mis en place un MediaController pour permettre aux utilisateurs de faire Lecture / Pause, Retour, Avance rapide et un curseur de progression pour contrôler la vidéo.
Ensuite, créez un fichier XML pour votre VideoViewActivity.Aller à res> layout> Clic droit sur le dossier layout> New> Android XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<VideoView
android:id="@+id/VideoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<resources>
<string name="app_name">Video
Stream</string>
<string name="hello_world">Hello
world!</string>
<string name="menu_settings">Paramètres</string>
<string name="button">Stream
Video</string>

Commentaires
Enregistrer un commentaire
Your comment will be reviewed by moderators