zoukankan      html  css  js  c++  java
  • android 视频播放

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

    要求:

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

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

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

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

    然后API的选择有 MediaPlayer和VideoView

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

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

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

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

    package ss.ss;

    import android.content.Context;
    import android.util.AttributeSet;
    import android.widget.VideoView;

    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代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ss.ss.MyVideoView
    android:id="@+id/surface_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

    </LinearLayout>

    注意在 

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

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

    然后我们看下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
    *
    *
    http://www.apache.org/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 ss.ss;



    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.media.MediaPlayer.OnCompletionListener;
    import android.net.Uri;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.Toast;

    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/main.mp4";
    private MyVideoView mVideoView;

    @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.videoview);
    mVideoView = (MyVideoView) findViewById(R.id.surface_view);

    mVideoView.setOnCompletionListener(this);
    DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);
    MyVideoView.WIDTH=dm.widthPixels;
    MyVideoView.HEIGHT=dm.heightPixels;
    if (path == "") {
    // Tell the user to provide a media file URL/path.
    Toast.makeText(
    VideoViewDemo.this,
    "Please edit VideoViewDemo Activity, and set path"
    + " variable to your media file URL/path",
    Toast.LENGTH_LONG).show();

    } else {

    /*
    * Alternatively,for streaming media you can use
    * mVideoView.setVideoURI(Uri.parse(URLstring));
    */
    //mVideoView.setVideoPath(path);
    mVideoView.setVideoURI(Uri.parse("android.resource://ss.ss/"+R.raw.main));

    //如果你需要添加控制条就取消改注释
    //mVideoView.setMediaController(new MediaController(this));
    //1.6中测试自动播放的播放代码是不需要.start()
    //2.1中测试自动播放的播放代码
    // mVideoView.start();
    //所以为了兼容性 我们选择mVideoView.start();保证所有版本都在开始时自动播放
    mVideoView.start();
    }
    }
    @Override
    protected void onDestroy() {
    super.onDestroy();
    System.exit(0);
    }
    @Override
    public void onCompletion(MediaPlayer mp) {
    System.out.println("播放完成");
    }

    }

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

    1.如果需要播放工程资源中的视频则需要使用 mVideoView.setVideoURI(Uri.parse("android.resource://ss.ss/"+R.raw.main));

    2.如果需要播放SD卡中的文件则请看上面代码中 

    private String path = "/sdcard/main.mp4";

     //mVideoView.setVideoPath(path);

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

     @Override
     public void onCompletion(MediaPlayer mp) {
     System.out.println("播放完成");
     }

    实现implements OnCompletionListener并且设置  mVideoView.setOnCompletionListener(this);

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

    总结:

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

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

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

  • 相关阅读:
    C++ 引用做左值
    C++ 引用本质的详解
    C++ 引用基础
    C语言错误 指针的类型错误
    C++ c++与C语言的区别(三目运算符,const修饰符)
    C++ c++与C语言的区别(struct类型的加强,函数-变量类型加强,bool类型)
    C++ c++与C语言的区别(实用性增强,register关键字增强,全局变量检测增强)
    C++ c++初识
    C语言 Linux内核链表(企业级链表)
    C语言 结构体中属性的偏移量计算
  • 原文地址:https://www.cnblogs.com/tianjian/p/2342841.html
Copyright © 2011-2022 走看看