Android视频自动播放

Android视频自动播放

2023年7月15日发(作者:)

Android视频自动播放

今天是内容是给游戏添加一个视频,用于开场公司logo播放。

要求:

1.不要出现播放器那种 "开始","暂停" "快进""快退"等按钮。

2.播放完成后需要事件监听移除掉视频。

首先在android中播放视频默认支持3GP,MP4格式,如果你需要支持其他格式必须软解码其他格式文件。

因为我做的不是一个播放器只需要在游戏开头播放一下视频就行了,所以这里选用MP4格式。

然后API的选择有 MediaPlayer和VideoView

用MediaPlayer要自己编写响应的代码,如果你熟悉MediaPlayer只是稍微复杂一点而已。

用VideoView 是android已经封装好的View 它继承自SurfaceView并实现了MediaPlayerControl接口。

稍微想了下,毫不犹豫的选择了VideoView 。

首先要重写VideoView因为他默认不是全屏的,我需要全屏

package ;

import t;

import uteSet;

import iew;

public class MyVideoView extends VideoView {

public static int WIDTH;

public static int HEIGHT;

public MyVideoView(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int width = getDefaultSize(WIDTH, widthMeasureSpec);

int height = getDefaultSize(HEIGHT, heightMeasureSpec);

setMeasuredDimension(width,height);

} }

XML代码:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:id="@+id/surface_view"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

/>

注意在

这里设置为fill_parent或者wrap_content都是无影响的,他不会因为你设置成fill_parent就让视频全屏播放,感兴趣的朋友可以查ure()源码,视频的尺寸由

这个方法控制,它是根据比例来显示的,所以我上面需要重写ure();

然后我们看下Activity的代码:

/*

* Copyright (C) 2009 The Android Open Source Project

*

* Licensed under the Apache License, Version 2.0 (the "License");

* you may not use this file except in compliance with the License.

* You may obtain a copy of the License at

*

* /licenses/LICENSE-2.0

*

* Unless required by applicable law or agreed to in writing, software

* distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

* See the License for the specific language governing permissions and

* limitations under the License.

*/

package ;

import ty;

import layer;

import letionListener;

import ;

import ;

import yMetrics;

import ;

import Manager;

import ;

public class VideoViewDemo extends Activity implements OnCompletionListener {

/**

* TODO: Set the path variable to a streaming video URL or a local media

* file path.

*/

private String path = "/sdcard/4";

private MyVideoView mVideoView;

@Override

public void onCreate(Bundle icicle) {

te(icicle);

requestWindowFeature(E_NO_TITLE);

getWindow().addFlags(_FULLSCREEN);

setContentView(iew);

mVideoView = (MyVideoView) findViewById(e_view);

ompletionListener(this);

DisplayMetrics dm = new DisplayMetrics();

dowManager().getDefaultDisplay().getMetrics(dm);

=ixels;

=Pixels;

if (path == "") {

// Tell the user to provide a media file URL/path.

xt(

, "Please edit VideoViewDemo Activity, and set path"

+ " variable to your media file URL/path",

_LONG).show();

} else {

/*

* Alternatively,for streaming media you can use

* eoURI((URLstring));

*/

//eoPath(path);

eoURI(("ce:///"+));

//如果你需要添加控制条就取消改注释

//iaController(new MediaController(this));

//1.6中测试自动播放的播放代码是不需要.start()

//2.1中测试自动播放的播放代码

// ();

//所以为了兼容性 我们选择();保证所有版本都在开始时自动播放

();

}

}

@Override

protected void onDestroy() {

roy();

(0);

}

@Override

public void onCompletion(MediaPlayer mp) {

n("播放完成");

}

}

这里大部分是API demo中的源码,稍微做了下修改。

1.如果需要播放工程资源中的视频则需要使用

eoURI(("ce:///"+)); 2.如果需要播放SD卡中的文件则请看上面代码中

private String path = "/sdcard/4";

//eoPath(path);

3.播放完成后的事件处理

@Override

public void onCompletion(MediaPlayer mp) {

n("播放完成");

}

实现implements OnCompletionListener并且设置

ompletionListener(this);

4.吧设备的分辨率传到VideoView中用于适用不同分辨率的设备

总结:

在播放视频的问题上 我遇到了以下这些问题

1.首先全屏播放时需要重写onMeasure的

2.不同版本下 有的会自动播放,有的不会自动播放。所以为了统一请采用()因为在1.6中 你不设置()他也会自动播放,但在2.1测试中必须明确

发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1689430400a246992.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信