Affichage des articles dont le libellé est Media. Afficher tous les articles
Affichage des articles dont le libellé est Media. Afficher tous les articles

How to play YouTube video in Android application ?


As we know playing a video stored in user device will never be a solutions specially if you app contains a large amount of information’s like a city guide, e-learning app... and so on and storing it locally we surely abuse the user and push him to uninstalled. So in this tutorial we are going to play a YouTube video with tow different methods and we are going also to play a video from an SDCARD as a demonstration of the Video View component

 Very important playing a video with android emulator will never work properly you must use a real android device for testing This method will launch the YouTube app and not in a videoview All you have to do is to paste this code inside the event of your button and don't forget to replace video_id by the located on the URL of YouTube video you want to play

String video_id = "2vFLW-idOxk";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + video_id);
startActivity(intent);
you can also use this method for clean and nice coding


private void startVideo(String videoID) {
 // default youtube app
 Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + videoID));
 List list = getPackageManager().queryIntentActivities(i, PackageManager.MATCH_DEFAULT_ONLY);
 if (list.size() == 0) {
  // default youtube app not present or doesn't conform to the standard we know
  // use our own activity
  i = new Intent(getApplicationContext(), YouTube.class);
  i.putExtra("VIDEO_ID", videoID);
 }
 startActivity(i);
}
Add the following permissions in the manifest file


Android radio streaming tutorial



Just add those components to your layout
1 – Button for play with the id:  buttonPlay
2 – Button for stop with the id:  buttonbuttonStopPlay
3-progressBar to show the buffing of the stream id: progressBar1
import android.app.Activity;
import android.os.Bundle;

import java.io.IOException;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class myMain extends Activity implements OnClickListener {

    private ProgressBar playSeekBar;

    private Button buttonPlay;

    private Button buttonStopPlay;

    private MediaPlayer player;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initializeUIElements();

        initializeMediaPlayer();
    }

    private void initializeUIElements() {

        playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
        playSeekBar.setMax(100);
        playSeekBar.setVisibility(View.INVISIBLE);

        buttonPlay = (Button) findViewById(R.id.buttonPlay);
        buttonPlay.setOnClickListener(this);

        buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
        buttonStopPlay.setEnabled(false);
        buttonStopPlay.setOnClickListener(this);

    }

    public void onClick(View v) {
        if (v == buttonPlay) {
            startPlaying();
        } else if (v == buttonStopPlay) {
            stopPlaying();
        }
    }

    private void startPlaying() {
        buttonStopPlay.setEnabled(true);
        buttonPlay.setEnabled(false);

        playSeekBar.setVisibility(View.VISIBLE);

        player.prepareAsync();

        player.setOnPreparedListener(new OnPreparedListener() {

            public void onPrepared(MediaPlayer mp) {
                player.start();
            }
        });

    }

    private void stopPlaying() {
        if (player.isPlaying()) {
            player.stop();
            player.release();
            initializeMediaPlayer();
        }

        buttonPlay.setEnabled(true);
        buttonStopPlay.setEnabled(false);
        playSeekBar.setVisibility(View.INVISIBLE);
    }

    private void initializeMediaPlayer() {
        player = new MediaPlayer();
        try {
            player.setDataSource("http://usa8-vn.mixstream.net:8138");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                playSeekBar.setSecondaryProgress(percent);
                Log.i("Buffering", "" + percent);
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player.isPlaying()) {
            player.stop();
        }
    }
}
android manifest file :






    
        
            
            
        
    


as you can see i have added a internet permission and without it the application will not work