Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android mediaplayer 視頻修改視頻大小 (屏幕尺寸mediaPlayer =...

android mediaplayer 視頻修改視頻大小 (屏幕尺寸mediaPlayer =...

編輯:關於Android編程

1 主要是通過更改view 的大小來改變視頻的大小。

2 在視頻初始化時獲得視頻的高寬都為0 ,這個必須要注意,必須播放後延時 修改view 大小

3 sufaceview 的布局類是 android.widget.LinearLayout.LayoutParams 這個必須注意

4 在 sufaceview surfaceChanged事件中加入 mediaPlayer.setDisplay(arg0) ,  setDisplay 方法在任意時刻都是可以調用的

 

01
    try {
02
                Log.e("m3u8", mediapath);
03
                mediaPlayer = new MediaPlayer();
04
                // 開始准備播放
05
                uiHandler.sendMessage(uiHandler
06
                        .obtainMessage(MediaPlayerMessage.PREPARE));
07
 
08
                // 設置媒體的路徑
09
                mediaPlayer.setDataSource(mediapath);
10
                // 設置邊播放變緩沖
11
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
12
                mediaPlayer.prepareAsync();
13
            } catch (Exception e) {
14
                // TODO: handle exception
15
                Log.e("meidaplayer err ", e.toString());
16
            }
17
 
18
            /**
19
             * 播放器異常事件
20
             */
21
            mediaPlayer.setOnErrorListener(new OnErrorListener() {
22
 
23
                @Override
24
                public boolean onError(MediaPlayer mp, int what, int extra) {
25
                    // TODO Auto-generated method stub
26
                    mediaPlayer.release();
27
                    return false;
28
                }
29
            });
30
                        //視頻准備完成 可以播放
31
            mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
32
 
33
                @Override
34
                public void onPrepared(MediaPlayer mp) {
35
                    // TODO Auto-generated method stub
36
                    try {
37
                        mp.start();
38
                                                <span style="color:#e53333;">//給ui 界面發送消息 這裡有個延時是設置 如果不設置延時 會出現 獲得視頻的高寬為零的文件</span>                       
1
uiHandler.sendEmptyMessageDelayed(MediaPlayerMessage.SETSIZE, 1000);
2
 
3
                    } catch (Exception e) {
4
                        // TODO: handle exception
5
                        Log.e("start mediaplayer", e.toString());
6
                    }
7
 
8
                }
9
            });
view sourceprint?
01
<pre class="brush:java; toolbar: true; auto-links: false;">    case MediaPlayerMessage.SETSIZE:
02
        int mVideoWidth = mediaPlayerView.mediaPlayer.getVideoWidth();
03
        int mVideoHeight = mediaPlayerView.mediaPlayer.getVideoHeight();
04
        int width=_frameLayout_main.getWidth();
05
    int height=_frameLayout_main.getHeight();
06
    <span style="color:#e53333;">android.widget.LinearLayout.LayoutParams sufaceviewParams = (android.widget.LinearLayout.LayoutParams) mediapaly_sufaceview
07
                            .getLayoutParams();</span>  if ( mVideoWidth * height  > width * mVideoHeight ) {
08
        //Log.i("@@@", "image too tall, correcting");
09
        sufaceviewParams.height = width * mVideoHeight / mVideoWidth;
10
        } else if ( mVideoWidth * height  < width * mVideoHeight ) {
11
        //Log.i("@@@", "image too wide, correcting");
12
        sufaceviewParams.width = height * mVideoWidth / mVideoHeight;
13
        }else{
14
                sufaceviewParams.height=height;
15
                sufaceviewParams.width = width;
16
         }
17
    sufaceviewParams.gravity=Gravity.CENTER;
18
 
19
    mediapaly_sufaceview.setLayoutParams(sufaceviewParams);
20
    break;</pre><p> </p>
21
<span style="color:#e53333;">//更在 surface 大小後事件</span>  @Override
22
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
23
        // TODO Auto-generated method stub www.2cto.com
24
        Log.e("Surface h w", String.valueOf(arg2) + " " + String.valueOf(arg3));
25
        if (mediaPlayer != null) {
26
            <span style="color:#e53333;">mediaPlayer.setDisplay(arg0);</span>       }
27
 
28
    }<br>


作者:tfc
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved