Android:通过http播放大型视频MP4文件

时间:2013-03-21 15:50:04

标签: java android video streaming

我的http服务器上有300 MB视频...我正在尝试在我的应用程序中远程播放(在3G模式下)这些视频的简单代码 - sd存储或本地存储不是一种选择。当然我从SDK android-17(MediaPlayerDemo_Video,VideoViewDemo ...)的演示源开始,在3G模式下我的视频没有播放(我在三星Galaxy Nexus上测试)。之后我在下一个代码中尝试了新的尝试:

/**
 * Listing 15-4: Initializing and assigning a Surface View to a Media Player
 */
import java.io.IOException;

import mobile.dariknews.R;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;

public class SurfaceViewVideoViewActivity extends Activity 
  implements SurfaceHolder.Callback {

  static final String TAG = "SurfaceViewVideoViewActivity";

  private MediaPlayer mediaPlayer;

  public void surfaceCreated(SurfaceHolder holder) { 
    try {
      // When the surface is created, assign it as the
      // display surface and assign and prepare a data 
      // source.
      mediaPlayer.setDisplay(holder);
      mediaPlayer.setDataSource("http://snimkitevi-bg.com/darik/1.MP4");
      //mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mediaPlayer.prepareAsync();
    } catch (IllegalArgumentException e) {
      Log.e(TAG, "Illegal Argument Exception", e);
    } catch (IllegalStateException e) {
      Log.e(TAG, "Illegal State Exception", e);
    } catch (SecurityException e) {
      Log.e(TAG, "Security Exception", e);
    } catch (Exception e) {      
      Log.e(TAG, "IO Exception", e);
    }
  }

  public void surfaceDestroyed(SurfaceHolder holder) {
    mediaPlayer.release();
  }  

  public void surfaceChanged(SurfaceHolder holder,
                             int format, int width, int height) { }


  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.surfaceviewvideoviewer);

    // Create a new Media Player.
    mediaPlayer = new MediaPlayer();

    // Get a reference to the Surface View.
    final SurfaceView surfaceView =
      (SurfaceView)findViewById(R.id.surfaceView);

    // Configure the Surface View.
    surfaceView.setKeepScreenOn(true);

    // Configure the Surface Holder and register the callback.
    SurfaceHolder holder = surfaceView.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    holder.setFixedSize(400, 300);

    // Connect a play button.
    Button playButton = (Button)findViewById(R.id.buttonPlay);
    playButton.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        mediaPlayer.start();        
      }
    });

    // Connect a pause button.
    Button pauseButton = (Button)findViewById(R.id.buttonPause);
    pauseButton.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        mediaPlayer.pause();        
      }
    });

    // Add a skip button.
    Button skipButton = (Button)findViewById(R.id.buttonSkip);    
    skipButton.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        mediaPlayer.seekTo(mediaPlayer.getDuration()/2);
      }
    });

    /**
     * Listing 15-5: Controlling playback using the Media Controller
     */
    MediaController mediaController = new MediaController(this);
    mediaController.setMediaPlayer(new MediaPlayerControl() {
      public boolean canPause() {
        return true;
      }

      public boolean canSeekBackward() {
        return true;
      }

      public boolean canSeekForward() {
        return true;
      }

      public int getBufferPercentage() {
        return 0;
      }

      public int getCurrentPosition() {
        return mediaPlayer.getCurrentPosition();
      }

      public int getDuration() {
        return mediaPlayer.getDuration();
      }

      public boolean isPlaying() {
        return mediaPlayer.isPlaying();
      }

      public void pause() {
        mediaPlayer.pause();
      }

      public void seekTo(int pos) {
        mediaPlayer.seekTo(pos);
      }

      public void start() {
        mediaPlayer.start();
      }
    });    
  }
}

之后的新尝试

import android.app.Activity;
import android.app.Application;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.widget.MediaController;
import android.widget.VideoView;

public class PlayerActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.videoplayer);
        new LoadOutbox().execute();

    }

    /**
     * Background Async Task to Load all OUTBOX messages by making HTTP Request
     * */
    class LoadOutbox extends AsyncTask<String, String, String> {
        int progressBarStatus = 0;
        private Handler progressBarHandler = new Handler();

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        /**
         * getting Outbox JSON
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            VideoView videoView = (VideoView) findViewById(R.id.videoView1);
            videoView.setKeepScreenOn(true);
            // Attach a Media Controller MediaController mediaController = new
            // MediaController( this); videoView.setMediaController(
            // mediaController);
            MediaController mediaController = new MediaController(
                    PlayerActivity.this);

            videoView.setMediaController(mediaController);

            Uri uri = Uri.parse("http://snimkitevi-bg.com/darik/1.MP4");
            videoView.setVideoURI(uri);
            videoView.start();

            return null;
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {

                }
            });

        }

    }

    public static Bitmap getLocalImageBitmap(String url) {
        return MainNewsActivities.getLocalImageBitmap(url);
    }

}

之后,我使用MediaPlayer和VIewVideo进行了50次其他测试......所有结果都相同 - 慢速和冻结的视频播放。我的需求非常简单 - 我的网站上有大视频文件(mp4),我想在我的应用上播放它们。所以我在这里 - 在Android应用程序中播放大型远程文件的正确公式是什么?

PS:当然我尝试使用GStreamer,但是这个框架与上一个Android环境不兼容 - 我在2天内玩过...我的视频没问题 - 在wifi模式下我玩http://snimkitevi-bg.com/darik/1.MP4

那么你能给我一些很好的建议,例子,教程,MediaPlayer如何在3G网络连接中正常播放,缓存和快速大视频文件?

0 个答案:

没有答案