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

Android多媒体开发笔记[1]-- 利用Su易做图ceView + MediaPlayer开发最简单的视频播放器

利用android SDK 提供的接口 开发最简单的视频播放demo。
接下来贴出main.xml的源码:
[html]
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
 
    <Su易做图ceView 
        android:id="@+id/su易做图ceView1" 
        android:layout_width="wrap_content" 
        android:layout_height="360dp" 
        android:layout_alignParentLeft="true" 
        android:layout_alignParentRight="true" 
        android:layout_alignParentTop="true" /> 
 
</RelativeLayout> 

接下来贴出程序源码:
[java]
package com.example.su易做图cemediaplayer; 
 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.os.Environment; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.Su易做图ceHolder; 
import android.view.Su易做图ceView; 
import android.view.Su易做图ceHolder.Callback; 
 
public class Su易做图ceMediaPlayer extends Activity { 
     
    Su易做图ceView su易做图ceView; 
    MediaPlayer mediaPlayer; 
 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_su易做图ce_media_player); 
         
        mediaPlayer=new MediaPlayer(); 
        su易做图ceView=(Su易做图ceView) this.findViewById(R.id.su易做图ceView1); 
         
        su易做图ceView.getHolder().addCallback(new Callback() {                 
            @Override 
            public void su易做图ceDestroyed(Su易做图ceHolder holder) { 
      
            } 
            @Override 
            public void su易做图ceCreated(Su易做图ceHolder holder) { 
                //开始播放 
                play(); 
            }                         
            @Override 
            public void su易做图ceChanged(Su易做图ceHolder holder, int format, int width, 
                            int height) { 
 
            } 
    });    

 
    private void play() { 
    try { 
            String path = Environment.getExternalStorageDirectory().getPath()+"/Movies/run_to_you.3gp"; 
            mediaPlayer.setDataSource(path); 
            //把视频画面输出到Su易做图ceView 
            mediaPlayer.setDisplay(su易做图ceView.getHolder()); 
            mediaPlayer.prepare(); 
            //开始播放 
            mediaPlayer.start();                 
    } catch (Exception e) { 
            // TODO: handle exception 
    } 
}    
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        getMenuInflater().inflate(R.menu.activity_su易做图ce_media_player, menu); 
        return true; 
    } 

程序运行截图:

 

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