【问题标题】:not able to stream .m3u8 file on an android platform无法在 android 平台上流式传输 .m3u8 文件
【发布时间】:2012-02-10 16:04:55
【问题描述】:

我正在尝试在 android 平台上实现 http 直播。但是对于 .m3u8 文件的这个(http://rajsimsan.site90.net/stream.m3u8)链接,我的模拟器没有播放视频。这是主要的java代码。

    /*
    * Copyright (C) 2009 The Android Open Source Project
    *
     * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
  */

package com.example.testhls;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
  import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class MediaPlayerDemo_Video extends Activity implements
    OnBufferingUpdateListener, OnCompletionListener,
    OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {

private static final String TAG = "MediaPlayerDemo";
private int mVideoWidth;
private int mVideoHeight;
private MediaPlayer mMediaPlayer;
private SurfaceView mPreview;
private SurfaceHolder holder;
private String path;
private Bundle extras;
private static final String MEDIA = "media";
private boolean mIsVideoSizeKnown = false;
private boolean mIsVideoReadyToBePlayed = false;

/**
 * 
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.mediaplayer_2);
    mPreview = (SurfaceView) findViewById(R.id.surface);
    holder = mPreview.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    extras = getIntent().getExtras();

}

private void playVideo(Integer Media) {
    doCleanUp();
    try {
        switch (Media) {
            case Globals.TEST_HTTP:
                path = "http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8";
                break;
            case Globals.TEST_HTTPLIVE:
                path="http://rajsimsan.site90.net/stream.m3u8";
                //path = "smoothApple.isml/manifest(format=m3u8-aapl).m3u8";
                //httplive://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8
                break;
        }
        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);
    }
}

public void onBufferingUpdate(MediaPlayer arg0, int percent) {
    Log.d(TAG, "onBufferingUpdate percent:" + percent);

}

public void onCompletion(MediaPlayer arg0) {
    Log.d(TAG, "onCompletion called");
}

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    Log.v(TAG, "onVideoSizeChanged called");
    if (width == 0 || height == 0) {
        Log.e(TAG, "invalid video width(" + width + ") or height(" + height + ")");
        return;
    }
    mIsVideoSizeKnown = true;
    mVideoWidth = width;
    mVideoHeight = height;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
}

public void onPrepared(MediaPlayer mediaplayer) {
    Log.d(TAG, "onPrepared called");
    mIsVideoReadyToBePlayed = true;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }
}

public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
    Log.d(TAG, "surfaceChanged called");

}

public void surfaceDestroyed(SurfaceHolder surfaceholder) {
    Log.d(TAG, "surfaceDestroyed called");
}


public void surfaceCreated(SurfaceHolder holder) {
    Log.d(TAG, "surfaceCreated called");
    playVideo(extras.getInt(MEDIA));


}

@Override
protected void onPause() {
    super.onPause();
    releaseMediaPlayer();
    doCleanUp();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    releaseMediaPlayer();
    doCleanUp();
}

private void releaseMediaPlayer() {
    if (mMediaPlayer != null) {
        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

private void doCleanUp() {
    mVideoWidth = 0;
    mVideoHeight = 0;
    mIsVideoReadyToBePlayed = false;
    mIsVideoSizeKnown = false;
}

private void startVideoPlayback() {
    Log.v(TAG, "startVideoPlayback");
    holder.setFixedSize(mVideoWidth, mVideoHeight);
    mMediaPlayer.start();
}

}

【问题讨论】:

    标签: android http-live-streaming


    【解决方案1】:

    如果没有 vitamio,我们不能在没有 m3u8 的情况下进行流式传输吗?

    【讨论】:

      【解决方案2】:

      看看Vitamio。他们有一个您可以使用的库 — 甚至在 Android 2.1 设备上 — 允许您播放 m3u8 提要(以及其他内容)。

      很遗憾,模拟器不支持 vitamio 库,因此您必须在物理设备上进行测试(或订阅第三方真机测试服务)。

      【讨论】:

      • shabbirh 还有一件事......我正在尝试在模拟器(3.0)中播放 www.markmyfest.com/raj/stream.m3u8 没有任何反应......但是当我在苹果标签浏览器,它播放流畅.....你能告诉我可能是问题吗?
      • 模拟器一般不支持需要硬件加速的东西。如果您是专业开发人员 - 最好的办法是拥有一个开发设备并将其用作您的测试平台 - 不要依赖模拟器 - 适用于所有移动平台 - Android、iOS、WP7、BB 等 - - 模拟器非常适合整理你的基本 UI 和类似的东西 - 用于测试真实的功能和性能 - 与真实设备完全不同。希望对您有所帮助。
      • @shabbirh 如何将 Vitamio SDK 嵌入到我们的 Andriod 应用程序中?
      • @String 你可以从 github 下载VitamioBundle,它包含示例,非常简单。
      • @Crossle Song 谢谢,成功了吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-25
      • 1970-01-01
      • 2014-06-14
      • 2019-10-03
      • 1970-01-01
      • 2014-12-08
      相关资源
      最近更新 更多