android-从sd卡播放视频

标签 android media-player android-video-player

我想使用 mediaPlayer 创建一个简单的视频播放器。所以我做了下面给出的代码。但我在这里遇到了问题,因为视频不可见,但正在播放音频。 在我的 XML 文件中,我使用了一个简单的 VideoView。

    package com.Android.Simple;

    import android.app.Activity;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.SurfaceHolder;
    import android.widget.VideoView;

    public class SimpleVideoPlayerActivity extends Activity {

        private VideoView _UIVideo;
        SurfaceHolder _videoHolder;

        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.simple_video_view);

            _UIVideo = (VideoView) findViewById(R.id.VVSimpleVideo);
            MediaPlayer _mediaPlayer;
            String _path = "/sdcard/abc.3gp";
            _videoHolder = _UIVideo.getHolder();

             try {
                _mediaPlayer = new MediaPlayer();
                _mediaPlayer.setDataSource(_path);
                _mediaPlayer.setDisplay(_videoHolder);
                _mediaPlayer.prepare();
                _mediaPlayer.start();

            } catch (Exception e) {
             }
    }

和 list 文件:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.Android.Simple"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />

    <application
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name" >
            <activity
                android:name=".SimpleVideoPlayerActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>

    </manifest>





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

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

      </LinearLayout>

最佳答案

Try this Code

        VideoView videoView = (VideoView)this.findViewById(R.id.videoView);
        MediaController mc = new MediaController(this);
        mc.setAnchorView(videoView);
        mc.setMediaPlayer(videoView);
        videoView.setMediaController(mc);

        videoView.setVideoPath(path);


        videoView.requestFocus();
        videoView.start();

关于android-从sd卡播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9614467/

相关文章:

Android VideoView 根据每个设备调整大小

android - 无法在 videoview 中播放来自 youtube url 的视频

android - 如何将uri转换为inputStream数据并将流数据上传到服务器?

windows-phone-7 - 如何使用 MediaHistory.NowPlaying 属性获取 Windows Phone 7 中的当前播放列表

android - setVideoSurfaceTexture 失败 : -22 in mediaplayer

android - 从文件夹获取音频

android - 视频在播放前闪烁黑色 (ExoPlayer)

java - 在 NativeScript 中使用 Android 原生代码 (JAVA)

android - 如何在 Android Instrumentation 测试中定义和使用系统属性?

java - 条件运行时程序停止