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.
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.
Insert this Code to AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
来源:https://stackoverflow.com/questions/12131408/android-videoview-live-tv-stream-hls