简体   繁体   English

如何在Android应用程序中播放实时流媒体?

[英]How to play live streaming in android application?

I want to make application for cricket live streaming. 我想申请板球直播。 I want to know following things : 我想知道以下事情:

  1. From where I can found the links to play cricket streaming ? 从哪里可以找到播放板球流的链接?
  2. Which type of links are these ? 这些类型的链接是什么?
  3. 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;
    }

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 ? 

No idea, but this is not a SO standard question anyway. 不知道,但无论如何这不是SO标准问题。

 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或RTSP。 HLS links are simple HTTP links that often end with a ".m3u8" postfix. HLS链接是简单的HTTP链接,通常以“.m3u8”后缀结尾。 (eg " http://somewebsite.com/streams/hls_stream_video.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 ? 

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. 我不确定“玩家”是指你的意思是Android API播放器还是第三方播放器应用程序。 So I'll cover both cases for you and future passengers. 因此,我将为您和未来的乘客报道两种情况。

I) Android API: You can do so with the help of a MediaController , a MediaPlayer and a SurafceView . I)Android API:您可以借助MediaControllerMediaPlayerSurafceView The latter two are also available in a unit entity known as VideoView . 后两者也可以在称为VideoView的单元实体中使用。 There is a code in the answer below, you can use that. 下面的答案中有一个代码,您可以使用它。 But Be aware of two key points: 但请注意两个要点:

Ia) Using MediaPlayer is harder to implement but gives you more detailed control compared to VideoView . Ia)使用MediaPlayer更难实现,但与VideoView相比,它提供了更详细的控制。

Ib) If you use some code similar to the below answer Never call prepare() for network streams. Ib)如果您使用类似于以下答案的代码,请不要为网络流调用prepare()。 Always prepareAsync() . 总是prepareAsync() And Always call setAudioStreamType() before prepareAsync. 并且总是在prepareAsync之前调用setAudioStreamType() Otherwise you will face transient sync issues between Audio and Video when seeking on the progressbar. 否则,在进度条上搜索时,您将面临音频和视频之间的瞬态同步问题。

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. 假设您的目标是Android,我建议您将选择范围缩小到HLS和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. 在较低带宽上运行时,HLS是首选。

There are many other topics like whether to choose UDP/TCP , IP-Multicast/Broadcast and so on... 还有很多其他主题,比如是否选择UDP / TCPIP多播/广播等......

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在视频流方面缺乏全面的帖子,也许我会根据需要扩展我的答案。

Follow this link : 点击此链接:

Android Video Streaming Android视频流

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) {

}
});

}

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);
      }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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