怎么写android上一个简单的视频播放器? [英] how to write a simple video player on android?

查看:173
本文介绍了怎么写android上一个简单的视频播放器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有code在Android简单的音频播放器。但它不工作的视频。请指导我写一个简单的视频播放器。 音频播放器code是

 包com.example.helloplayer;

公共类HelloPlayer延伸活动{
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        MediaPlayer的MP = MediaPlayer.create(这一点,R.raw.file);
        mp.start();
   }
}
 

解决方案

为了使视频播放器,你将不得不使用视频查看。样品如下所示

布局文件

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>

< VideoView机器人:ID =@ + ID / videoView
机器人:layout_height =FILL_PARENT
机器人:layout_width =FILL_PARENT/>
< / LinearLayout中>
 

我在这里播放存储在资源/原材料文件夹中的视频,一是视频文件的名称,你可以用你的视频文件的名称替换它。也请确保您要玩的机器人支持的视频格式

VideoplayerActivitvity

 进口android.app.Activity;
进口android.net.Uri;
进口android.os.Bundle;
进口android.widget.MediaController;
进口android.widget.Toast;
进口android.widget.VideoView;

公共类VideoplayerActivity延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    VideoView videoView =(VideoView)findViewById(R.id.videoView);
    的MediaController的MediaController =新的MediaController(本);
    mediaController.setAnchorView(videoView);
    乌里的uri = Uri.parse(android.resource://+ getPackageName()+/+ R.raw.one);
    videoView.setMediaController(的MediaController);
    videoView.setVideoURI(URI);
    videoView.requestFocus();

    videoView.start();


    }
    }
 

I have code for simple audio player on android. But it is not working for videos. Please guide me to write a simple video player. The code of audio player is

package com.example.helloplayer;

public class HelloPlayer extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MediaPlayer mp = MediaPlayer.create(this, R.raw.file); 
        mp.start();
   }
}

解决方案

For making a video player you will have to make use of video view. a sample is shown below

Layout file

<?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">

<VideoView android:id="@+id/videoView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>

Here i am playing a video stored in "resource/raw" folder , "one" is the name of video file,you can replace it with name of your video file .also make sure that you are going to play an android supported video format

VideoplayerActivitvity

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoplayerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    VideoView videoView =(VideoView)findViewById(R.id.videoView);
    MediaController mediaController= new MediaController(this);
    mediaController.setAnchorView(videoView);        
    Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.one);        
    videoView.setMediaController(mediaController);
    videoView.setVideoURI(uri);        
    videoView.requestFocus();

    videoView.start();


    }
    }   

这篇关于怎么写android上一个简单的视频播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆