android videoview 不要获取焦点,Disable Android's VideoView requestAudioFocus when playing a video?...

I got around this with a stupid solution by copying whole source code of android.widget.VideoView of Lollipop and removing line you mentioned.

Make your own VideoView class. don't use extends VideoView since you can't override openVideo() method.

I don't recommend this as I thinking it's a temporary solution. VideoView Changed a lot between 4.1-5.0 so this can make RuntimeException on Android version other than Lollipop

Edit

I made approach MediaPlayer + SurfaceView as pinxue told us;

It respects aspect ratio within viewWidth and viewHeight.

final String finalFilePath = filePath;

final SurfaceHolder surfaceHolder = sv.getHolder();

final MediaPlayer mediaPlayer = new MediaPlayer();

final LinearLayout.LayoutParams svLayoutParams = new LinearLayout.LayoutParams(viewWidth,viewHeight);

surfaceHolder.addCallback(new SurfaceHolder.Callback(){

@Override

public void surfaceCreated(SurfaceHolder holder) {

try {

if(isDebug) {

System.out.println("setting VideoPath to VideoView: "+finalFilePath);

}

mediaPlayer.setDataSource(finalFilePath);

}catch (IOException ioe){

if(isDebug){

ioe.printStackTrace();

}

//mediaPlayer = null;

}

mediaPlayer.setDisplay(surfaceHolder);

mediaPlayer.prepareAsync();

mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

@Override

public void onPrepared(MediaPlayer mp) {

if(isDebug){

System.out.println("Video is starting...");

}

// for compatibility, we adjust size based on aspect ratio

if ( mp.getVideoWidth() * svLayoutParams.height < svLayoutParams.width * mp.getVideoHeight() ) {

//Log.i("@@@", "image too wide, correcting");

svLayoutParams.width = svLayoutParams.height * mp.getVideoWidth() / mp.getVideoHeight();

} else if ( mp.getVideoWidth() * svLayoutParams.height > svLayoutParams.width * mp.getVideoHeight() ) {

//Log.i("@@@", "image too tall, correcting");

svLayoutParams.height = svLayoutParams.width * mp.getVideoHeight() / mp.getVideoWidth();

}

sv.post(new Runnable(){

@Override

public void run() {

sv.setLayoutParams(svLayoutParams);

}

});

mp.start();

}

});

}

@Override

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

if(isDebug){

System.out.println("surfaceChanged(holder, "+format+", "+width+", "+height+")");

}

}

@Override

public void surfaceDestroyed(SurfaceHolder holder) {

try {

mediaPlayer.setDataSource("");

}catch (IOException ioe){

if(isDebug){

ioe.printStackTrace();

}

}

}

});

if(sv.post(new Runnable() {

@Override

public void run() {

sv.setLayoutParams(svLayoutParams);///

sv.setVisibility(View.VISIBLE);

}})){

if(isDebug) {

System.out.println("post Succeded");

}

}else{

if(isDebug) {

System.out.println("post Failed");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值