简体   繁体   中英

How to read & play .m3u8 file in Android?

I want to play an .ts format file in my application.I have a url of an .m3u8 extension file's url http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8 .

When I play video from url it plays fine. Here is the code for playing video from url...

    try
    {
        String path = "http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8";

        Uri uri = Uri.parse(path);
        videoView.setVideoURI(uri);
        videoView.start();
    } catch (Exception e)
    {
        // TODO: handle exception
        e.printStackTrace();
    }

I have checked that prog_index.m3u8 file has list of .ts files & this file runs perfectly when run through a url. But what I want is to read the .m3u8 file myself & from there I can extract .ts files & play those .ts files.

Is it possible to do?

if reading of .m3u8 file is not possible can I play an .ts file.If I stored it locally in my raw folder or read from stream.

Ok I solved my problem using FFmpeg in my application.I used the example given at appunite .

Now I can play .ts files fine.

MainActivity.java

package com.example.hlsdemo;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {
    Button startB; 
    VideoView mVideoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        startB=(Button) findViewById(R.id.button);
        startB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mVideoView = (VideoView) findViewById(R.id.surface_view);
                mVideoView.setVideoURI(Uri.parse("http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"));
                mVideoView.setMediaController(new MediaController(MainActivity.this));
                mVideoView.requestFocus();
                mVideoView.postInvalidateDelayed(100);
                new Thread(new Runnable() {
                    public void run() {
                        mVideoView.start();

                    }
                }).start();
            }
        });
    }

}

activity_main.xml

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <VideoView
        android:id="@+id/surface_view"
        android:layout_width="fill_parent"
        android:layout_height="300dp"
        android:layout_gravity="center" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/surface_view"
        android:layout_marginRight="62dp"
        android:layout_marginTop="16dp"
        android:text="Stop" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="34dp"
        android:text="Start" />

</RelativeLayout>

and Make sure Internet Permission in AndroidManifest.xml

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM