Java 如何在android应用程序中播放直播?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22344344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play live streaming in android application?
提问by user3106818
I want to make application for cricket live streaming. I want to know following things :
我想申请板球直播。我想知道以下事情:
- From where I can found the links to play cricket streaming ?
- Which type of links are these ?
- Is there any player to play this type of videos ?
- 从哪里可以找到播放板球直播的链接?
- 这些是哪种类型的链接?
- 有没有播放器可以播放这种类型的视频?
Currently, I have implemented web page but I am looking for other alternative.
目前,我已经实现了网页,但我正在寻找其他替代方案。
Below is my code :
下面是我的代码:
link1 = (RelativeLayout) findViewById(R.id.link1);
link2 = (RelativeLayout) findViewById(R.id.link2);
link3 = (RelativeLayout) findViewById(R.id.link3);
link4 = (RelativeLayout) findViewById(R.id.link4);
link5 = (RelativeLayout) findViewById(R.id.link5);
link6 = (RelativeLayout) findViewById(R.id.link6);
link7 = (RelativeLayout) findViewById(R.id.link7);
link1.setOnClickListener(this);
link2.setOnClickListener(this);
link3.setOnClickListener(this);
link4.setOnClickListener(this);
link5.setOnClickListener(this);
link6.setOnClickListener(this);
link7.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.link1:
linkFunction("http://changevssame.blogspot.com/2014/03/willow-cricket-hd-live-streaming.html");
break;
case R.id.link2:
linkFunction("http://changevssame.blogspot.com/2014/03/foxsports-live-streaming.html");
break;
case R.id.link3:
linkFunction("http://changevssame.blogspot.com/2014/03/sky-sports-live-streaming.html");
break;
case R.id.link4:
linkFunction("http://changevssame.blogspot.com/2014/03/ten-sports-live-streaming.html");
break;
case R.id.link5:
linkFunction("http://changevssame.blogspot.com/2014/03/star-cricket.html");
break;
case R.id.link6:
linkFunction("http://changevssame.blogspot.com/2014/03/icc-t20-world-cup-2014-live-streaming.html");
break;
case R.id.link7:
linkFunction("http://changevssame.blogspot.com/2014/03/ptv-sports.html");
break;
default:
break;
}
回答by Lucian Novac
Try this:
尝试这个:
private void playLive(String path){
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(path);
mMediaPlayer.setDisplay(holder);
mMediaPlayer.prepare();
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
}
}
回答by Behnam
I will try to answer your questions but there are many fundamentals you've got to learn in order to build up a successful Streaming Application.
我将尝试回答您的问题,但您必须学习许多基础知识才能构建成功的流媒体应用程序。
1. From where I can found the links to play cricket streaming ?
1. From where I can found the links to play cricket streaming ?
No idea, but this is not a SO standard question anyway.
不知道,但这无论如何都不是一个标准的问题。
2. Which type of links are these ?
2. Which type of links are these ?
IF you mean live streaming links, there are many types but mostly they are either HLS or RTSP. HLS links are simple HTTP links that often end with a ".m3u8" postfix. (e.g "http://somewebsite.com/streams/hls_stream_video.m3u8")
如果您指的是实时流媒体链接,则有很多类型,但大多数都是 HLS 或 RTSP。HLS 链接是简单的 HTTP 链接,通常以“.m3u8”后缀结尾。(例如“ http://somewebsite.com/streams/hls_stream_video.m3u8”)
RTSP links on the other hand, have a format like this: "rtsp://somewebsite.com/streams/an_rtsp_stream.mp4"
另一方面,RTSP 链接的格式如下:“rtsp://somewebsite.com/streams/an_rtsp_stream.mp4”
3. Is there any player to play this type of videos ?
3. Is there any player to play this type of videos ?
Absolutely. You can do so by any means. I'm not exactly sure by "a player" whether you mean Android API player or third-party player applications. So I'll cover both cases for you and future passengers.
绝对地。您可以通过任何方式这样做。我不确定“播放器”是指 Android API 播放器还是第三方播放器应用程序。因此,我将为您和未来的乘客介绍这两种情况。
I) Android API:You can do so with the help of a MediaController
, a MediaPlayer
and a SurafceView
. The latter two are also available in a unit entity known as VideoView
.
There is a code in the answer below, you can use that. But Be aware of two key points:
I) Android API:您可以借助 a MediaController
、 aMediaPlayer
和 a 来实现SurafceView
。后两者也可在称为 的单元实体中使用VideoView
。下面的答案中有一个代码,您可以使用它。但要注意两个关键点:
I-a) Using MediaPlayer
is harder to implement but gives you more detailed control compared to VideoView
.
Ia)MediaPlayer
与VideoView
.
I-b) If you use some code similar to the below answer Nevercall prepare() for network streams. Always prepareAsync(). And Alwayscall setAudioStreamType()before prepareAsync. Otherwise you will face transient syncissues between Audio and Video when seeking on the progressbar.
Ib) 如果您使用一些类似于以下答案的代码,请不要为网络流调用 prepare()。总是prepareAsync()。并且始终在 prepareAsync 之前调用setAudioStreamType()。否则,在进度条上查找时,您将面临音频和视频之间的瞬时同步问题。
II) Player Application:I have done streaming with MXPlayer and it works great.
II) 播放器应用程序:我已经使用 MXPlayer 完成了流式传输,并且效果很好。
There are some considerations to take before starting:
在开始之前有一些注意事项:
What protocol to choose?
选择什么协议?
Assuming you are targeting Android, I can advice you to narrow your choices down to HLS and RTSP. You need to study them well before making a decision. But to give you a hint. HLS is preferred when functioning on lower Bandwidths.
假设您的目标是 Android,我可以建议您将选择范围缩小到 HLS 和 RTSP。在做出决定之前,您需要仔细研究它们。但是给你一个提示。在较低带宽上运行时首选 HLS。
There are many other topics like whether to choose UDP/TCP, IP-Multicast/Broadcastand so on...
还有很多其他的话题,比如是否选择UDP/TCP,IP-Multicast/Broadcast等等……
Want to delve into coding and learn Video Streaming programmatically?
想要深入编码并以编程方式学习视频流吗?
Go and visit this tutorial. This is the most complete zero-to-hero guide in my opinion.
去访问本教程。这是我认为最完整的零英雄指南。
Since SO lacks a thorough post on Video Streaming, maybe I will extend my answer on demand.
由于 SO 缺乏关于Video Streaming的详尽帖子,也许我会按需扩展我的答案。
回答by Siddharth_Vyas
Follow this link :
按照这个链接:
Below code works for me :
下面的代码对我有用:
public static void getVideoFromHttp(String urlPath) {
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(mContext);
mediacontroller.setAnchorView(mVideoview);
// Get the URL from String VideoURL
Uri mVideo = Uri.parse(urlPath);
mVideoview.setMediaController(mediacontroller);
mVideoview.setVideoURI(mVideo);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
mVideoview.requestFocus();
mVideoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
mVideoview.start();
}
});
mVideoview.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
}
});
}