user3167333
user3167333

Reputation: 257

How to play a streaming video from URL in android?

I want to play a streaming video in android from a website.

For example, I want to play the streaming video from this url: http://florotv.com/canal2.html

Using URL Helper, I have been able to capture the rtmp URL that it's

rtmp://198.144.153.139:443/kuyo<playpath>ver44?id=acf6f5271f8ce567ed6c8737ce85a044&pid=32342e3136362e37332e323139 <swfUrl>http://yukons.net/yplay2.swf <pageUrl>http://yukons.net/embed/37363635373233343334/eeff74c57593ca38defc902fa6d88005/600/400

Now that I have this URL, I wanna know if it's possible to play the video in android.

I have tried this but it doesn't work because I don't know how to set the swfUrl, pageUrl.....

private static final String MOVIE_URL="rtmp://198.144.153.139:443/kuyo";
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse(MOVIE_URL);
intent.setData(data);
startActivity(intent);

Thanks in advance....

Upvotes: 5

Views: 27252

Answers (4)

javaxian
javaxian

Reputation: 2394

To use the latest, streaming-hardened SDK from Google and avoid implementation differences between device versions and makers, go for AndroidX's Media 2/3 ExoPlayer library. Here's a simple example in Kotlin:

class FullScreenVideoActivity : AppCompatActivity(R.layout.activity_full_screen_video) {

    private var player: ExoPlayer? = null

    private var playWhenReady: Boolean = true
    private var playbackPosition: Long = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        PregnancyApp.component().inject(this)
        super.onCreate(savedInstanceState)

        playWhenReady = savedInstanceState?.getBoolean(::playWhenReady.name) ?: playWhenReady
        playbackPosition = savedInstanceState?.getLong(::playbackPosition.name) ?: playbackPosition

        closeIcon.setOnClickListener { finish() }
        playerView.setControllerVisibilityListener(
            StyledPlayerView.ControllerVisibilityListener { visibility -> closeIcon.visibility = visibility }
        )
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)

        outState.putBoolean(::playWhenReady.name, playWhenReady)
        outState.putLong(::playbackPosition.name, playbackPosition)
    }

    override fun onStart() {
        super.onStart()
        initializePlayer()
    }

    override fun onStop() {
        super.onStop()
        releasePlayer()
    }

    private fun initializePlayer() {
        player = ExoPlayer.Builder(this)
            .build()
            .also { exoPlayer ->
                playerView.player = exoPlayer
                val mediaItem = MediaItem.fromUri(Uri.parse("http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/TearsOfSteel.mp4"))
                exoPlayer.setMediaItem(mediaItem)
                exoPlayer.playWhenReady = playWhenReady
                exoPlayer.seekTo(0, playbackPosition)
                exoPlayer.prepare()
                exoPlayer.addAnalyticsListener(object : AnalyticsListener {

                    override fun onPlayerError(eventTime: AnalyticsListener.EventTime, error: PlaybackException) {
                        Timber.e(error)
                    }

                })
            }
    }

    private fun releasePlayer() {
        player?.let { exoPlayer ->
            playbackPosition = exoPlayer.currentPosition
            playWhenReady = exoPlayer.playWhenReady
            exoPlayer.release()
        }
        player = null
    }

}

and the layout file:

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black"
    >

    <com.google.android.exoplayer2.ui.StyledPlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <!-- This inner layout here is required to place the back icon below the top system bar (android:fitsSystemWindows="true")
         while keeping the entire fragment full-screen. -->
    <FrameLayout
        android:id="@+id/backContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:fitsSystemWindows="true"
        android:elevation="4dp"
        >

        <ImageView
            android:id="@+id/closeIcon"
            android:layout_width="64dp"
            android:layout_height="64dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:padding="16dp"
            android:src="@drawable/ic_close_blue_on_semitransparent"
            android:contentDescription="@string/close"
            android:background="?selectableItemBackgroundBorderless"
            />

    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Plo_Koon
Plo_Koon

Reputation: 3033

Add below code into your Activity.java file.

protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.videoPlayer);
try {        
    String link="http://videocloud/video.mp4";
    VideoView videoView = (VideoView) findViewById(R.id.myVideoView);
    final ProgressBar pbLoading = (ProgressBar) findViewById(R.id.pbVideoLoading);
    pbLoading.setVisibility(View.VISIBLE);
    MediaController mediaController = new MediaController(this);
    mediaController.setAnchorView(videoView);
    Uri video = Uri.parse(link);
    videoView.setMediaController(mediaController);
    videoView.setVideoURI(video);
    videoView.start();
    videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            pbLoading.setVisibility(View.GONE);
                        }
    });
} catch (Exception e) {
    // TODO: handle exception
    Toast.makeText(this, "Error connecting", Toast.LENGTH_SHORT).show();
}

}

If you don't need MediaController set it to null videoView.setMediaController(null)

Upvotes: 2

KnowIT
KnowIT

Reputation: 2502

You can use Vitamio library for android, where you can set the swfUrl and pageUrl.

Here is a tutorial for it.

Upvotes: 0

jlouzado
jlouzado

Reputation: 502

I don't think it's enough to just create an intent, you also have to create a context for the video to be played : Like a VideoView object.

The link below has a suggestion for this pattern : http://androidcodeexamples.blogspot.sg/2011/08/how-to-play-mp4-video-in-android-using.html

Essentially a MediaController object is created with the VideoView object. Then the VideoView is used to start the operation once the URI is set.

Edit : Probably the main problem though is that your URL contains POST parameters and isn't exactly a unique identifier of a resource (in this case a video file).

The 'swfUrl' and 'pageUrl' parameters are most likely unique to the server that's providing you the page.

Upvotes: 0

Related Questions