当前位置:操作系统 > 安卓/Android >>

android mediaplayer 视频修改视频大小 (屏幕尺寸mediaPlayer =...

1 主要是通过更改view 的大小来改变视频的大小。

2 在视频初始化时获得视频的高宽都为0 ,这个必须要注意,必须播放后延时 修改view 大小

3 sufaceview 的布局类是 android.widget.LinearLayout.LayoutParams 这个必须注意

4 在 sufaceview su易做图ceChanged事件中加入 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; 易做图: 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
       

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,