Android VideoView live tv stream (HLS)

只谈情不闲聊 提交于 2019-12-07 07:28:46

问题


I'am trying to develop app for tv streaming (HLS). Using code below I tested stream on 2.3.3, 3.0 and 4.0.1 version Android devices, but encountered several problems. On Android 2.3.3 stream plays for >1 minute and then just stops. On Android 3.0 it plays well and on Android 4.0.3 it displays message 'This file cannot be played' (if I remember correctly). So my question would be: How can I play stream on ether of these devices, without having stream playing problems? Or where can I read more about solutions to these problems (tried to search but found nothing useful)?

Code in Main_Activity:

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    VideoView player = (VideoView)findViewById(R.id.player);
    String httpLiveUrl = "http://aj.lsops.net/live/aljazeer_en_high.sdp/playlist.m3u8";
    //for Android 2.3.3 I used httplive:// prefix
    player.setVideoURI(Uri.parse(httpLiveUrl));
    player.setMediaController(new MediaController(this));
    player.requestFocus();
    player.start();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

Code in xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<VideoView
    android:id="@+id/player"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

Sorry if my english is poor. Thank You.


回答1:


This will not solve all the streaming issues. But one thing you should do is call player.start() when the MediaPlayer is ready. The selected answer to this SO post sets a listener on the MediaPlayer object so that it will run start() when onPrepared(MediaPlayer mp) is called.




回答2:


Insert this Code to AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />


来源:https://stackoverflow.com/questions/12131408/android-videoview-live-tv-stream-hls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!