2012-02-10 125 views
0

我試圖在android平臺上實現http實時流式傳輸。但是對於.m3u8文件的這個(http://rajsimsan.site90.net/stream.m3u8)鏈接,我的模擬器沒有播放視頻。這裏是主要的java代碼。無法在android平臺上流式傳輸.m3u8文件

/* 
    * 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(); 
} 

}

回答

0

看看Vitamio。他們有一個庫,你可以使用 - 即使在Android 2.1設備上 - 可以讓你玩m3u8 feed(等等)。

不幸的是,模擬器不支持vitamio庫,因此您必須在物理設備上進行測試(或訂閱第三方真實設備測試服務)。

+0

shabbirh還有一件事....即時嘗試播放www.markmyfest.com/raj/stream.m3u8在模擬器(3.0)沒有反應....但是當我在蘋果標籤瀏覽器中播放相同的鏈接,它播放順利.....你可以告訴wat可能是問題嗎? – 2012-03-27 12:47:54

+0

模擬器通常不支持需要硬件加速的東西。如果你正在專業開發 - 最好的辦法是有一個開發設備,並將其用作你的測試場所 - 不要依賴模擬器 - 適用於所有移動平臺 - Android,iOS,WP7,BB等 - - 仿真器很適合整理你的基本UI和類似的東西 - 用於測試真正的功能和性能 - 沒有什麼比真正的設備。 希望有所幫助。 – shabbirh 2012-04-02 10:02:56

+0

@shabbirh如何將Vitamio SDK嵌入到我們的Andriod應用程序中? – String 2013-07-25 12:38:53

0

沒有vitamio,我們不能沒有m3u8流?