真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Android編程視頻播放API之MediaPlayer用法示例

本文實(shí)例講述了Android編程視頻播放API之MediaPlayer用法。分享給大家供大家參考,具體如下:

在南海等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì) 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站制作,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都營銷網(wǎng)站建設(shè),外貿(mào)網(wǎng)站建設(shè),南海網(wǎng)站建設(shè)費(fèi)用合理。

現(xiàn)在項(xiàng)目要做音視頻這塊功能,這正是我相對比較薄弱的地方,項(xiàng)目中要用到,趕緊抓緊時間學(xué)習(xí)。前面有一編介紹不錯的文章https://www.jb51.net/article/121881.htm,值得學(xué)習(xí)。

這里給出一個較為簡單的示例:

/**
 * Android API MediaPlayer示例
 *
 * @description:
 * @author ldm
 * @date 2016-4-20 上午10:42:06
 */
public class MediaPlayerActivity extends Activity implements OnClickListener {
  private Button mlocalvideo;
  private Button mstreamvideo;
  private Button mlocalaudio;
  private Button mresourcesaudio;
  private static final String MEDIA = "media";
  private static final int LOCAL_AUDIO = 1;
  private static final int RESOURCES_AUDIO = 3;
  private static final int STREAM_VIDEO = 5;
  @Override
  protected void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_mediaplayer);
    initViewsAndEvents();
  }
  private void initViewsAndEvents() {
    mlocalaudio = (Button) findViewById(R.id.localaudio);
    mresourcesaudio = (Button) findViewById(R.id.resourcesaudio);
    mlocalvideo = (Button) findViewById(R.id.localvideo);
    mstreamvideo = (Button) findViewById(R.id.streamvideo);
    mlocalaudio.setOnClickListener(this);
    mresourcesaudio.setOnClickListener(this);
    mlocalvideo.setOnClickListener(this);
    mstreamvideo.setOnClickListener(this);
  }
  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.localaudio:
    case R.id.localvideo:
      Intent intent = new Intent(MediaPlayerActivity.this.getApplication(),
          MediaPlayerDemo_Audio.class);
      intent.putExtra(MEDIA, LOCAL_AUDIO);
      startActivity(intent);
      break;
    case R.id.resourcesaudio:
      Intent resIn = new Intent(MediaPlayerActivity.this.getApplication(),
          MediaPlayerDemo_Audio.class);
      resIn.putExtra(MEDIA, RESOURCES_AUDIO);
      startActivity(resIn);
      break;
    case R.id.streamvideo:
      Intent streamIn = new Intent(MediaPlayerActivity.this,
          MediaPlayerDemo_Video.class);
      streamIn.putExtra(MEDIA, STREAM_VIDEO);
      startActivity(streamIn);
      break;
    }
  }
}

/**
 * OnBufferingUpdateListener可以獲取視頻音頻流的緩沖狀態(tài);
 * OnCompletionListener播放完畢監(jiān)聽器
 * OnPreparedListener準(zhǔn)備播放相關(guān)接口
 * OnVideoSizeChangedListener注冊視頻大小改變監(jiān)聽器
 * SurfaceHolder.Callback:SurfaceView監(jiān)聽callback
 * @description:
 * @author ldm
 * @date 2016-4-20 上午9:46:16
 */
public class MediaPlayer_Video extends Activity implements
    OnBufferingUpdateListener, OnCompletionListener,
    OnPreparedListener, OnVideoSizeChangedListener, SurfaceHolder.Callback {
  private static final String TAG = "MediaPlayerDemo";
  //Video寬度
  private int mVideoWidth;
  //Video高度
  private int mVideoHeight;
  //播放器MediaPlayer
  private MediaPlayer mMediaPlayer;
  //播放界面SurfaceView
  private SurfaceView mPreview;
  //媒體播放器的載體 SurfaceHolder
  private SurfaceHolder holder;
  //文件路徑
  private String path;
  //Bundle數(shù)據(jù)對象
  private Bundle extras;
  private static final String MEDIA = "media";
  private static final int LOCAL_VIDEO = 1;
  private static final int RESOURCES_AUDIO = 3;
  private static final int STREAM_VIDEO = 5;
  private boolean mIsVideoSizeKnown = false;
  private boolean mIsVideoReadyToBePlayed = false;
  @SuppressWarnings("deprecation")
  @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);
    //高版本上已經(jīng)不推薦使用了 ,如果要兼容低版本(如Android 2.3或以下版本)要加上這段代碼,不然播放時 只會有聲音 沒有圖像。
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    extras = getIntent().getExtras();
  }
  private void playVideo(Integer Media) {
    doCleanUp();
    try {
      switch (Media) {
        case LOCAL_VIDEO://本地文件(如SD卡)
          /*
           * TODO:將路徑變量設(shè)置為本地媒體文件路徑。
           */
          path = "";//這里要填寫你所播放音視頻的文件路徑
          if (path == "") {
            Toast
                .makeText(
                    MediaPlayer_Video.this,
                    "Please edit MediaPlayerDemo_Video Activity, "
                        + "and set the path variable to your media file path."
                        + " Your media file must be stored on sdcard.",
                    Toast.LENGTH_LONG).show();
          }
          break;
        case STREAM_VIDEO://流文件
          /**
           * 路徑變量設(shè)置為mp4或3gpp格式的URL。應(yīng)該使用Http協(xié)議。
           * 媒體播放器只能通過流的形式獲取數(shù)據(jù),所以需要:
           * 1。必須是媒體流數(shù)據(jù)。
           * 2。必須合理剪輯。
           */
          path = "";
          if (path == "") {
            // Tell the user to provide a media file URL.
            Toast
                .makeText(
                    MediaPlayer_Video.this,
                    "Please edit MediaPlayerDemo_Video Activity,"
                        + " and set the path variable to your media file URL.",
                    Toast.LENGTH_LONG).show();
          }
          break;
        case RESOURCES_AUDIO:
          //播放放在諸如res/raw文件夾下的資源文件
          //直接通過mMediaPlayer = MediaPlayer.create(this, R.raw.test_cbr);
          //可以參考API Demos中AudioFxDemo類文件
          break;
      }
      // 創(chuàng)建MediaPlayer對象,傳入文件數(shù)據(jù)并設(shè)備監(jiān)聽
      mMediaPlayer = new MediaPlayer();
      mMediaPlayer.setDataSource(path);
      mMediaPlayer.setDisplay(holder);
      //使用 create() 方法創(chuàng)建的 MediaPlayer, 直接指定了 媒體資源, 不需要再進(jìn)行 調(diào)用 prepare() 方法;
      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();
  }
}

布局文件activity_mediaplayer

<?xml version="1.0" encoding="utf-8"?>

  

布局文件activity_mediaplayer_video

<?xml version="1.0" encoding="utf-8"?>

  
  


更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android開發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》

希望本文所述對大家Android程序設(shè)計(jì)有所幫助。


本文題目:Android編程視頻播放API之MediaPlayer用法示例
新聞來源:http://weahome.cn/article/gesisp.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部