2012-12-18 88 views
0

我想在我的应用程序中播放.ts格式的文件。我有一个.m3u8扩展名文件的网址http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8如何在Android中阅读和播放.m3u8文件?

当我从url播放视频时,播放效果很好。 这里是从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(); 
    } 

我已签,当通过URL运行prog_index.m3u8文件有.ts文件&这个文件运行完全名单。 但我想要的是从那里读取自己的.m3u8文件&我可以提取.ts文件&播放这些.ts文件。

可以吗?

如果无法读取.m3u8文件,我可以播放.ts文件。如果我将其存储在本地文件夹中或从流中读取。

+0

看看这个问题的答案:http://stackoverflow.com/问题/ 3505930/make-an-http-request-with-android – MByD

+0

@Binyamin Sharet:我想在应用程序中播放.ts文件,您的链接如何解决我的问题我不明白。 – AB1209

回答

0

好的我在我的应用程序中使用FFmpeg解决了我的问题。我使用appunite给出的示例。

现在我可以播放.ts文件。

0

MainActivity.java

包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> 

和确保互联网的权限在AndroidManifest.xml