eclipse 中的 VideoView 无法在手机上播放

发布于 2024-11-28 18:48:31 字数 804 浏览 0 评论 0原文

我在 Eclipse 中为 Android 应用程序放置了一个视频视图,但该视频无法在我的手机上播放。

package test.test;

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


public class graphics extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    VideoView videoView = (VideoView) findViewById(R.id.test);
    MediaController mediaController = new MediaController (this);
    mediaController.setAnchorView(videoView);
    Uri video = Uri.parse("http://commonsware.com/misc/test2.3gp");
    videoView.setMediaController(mediaController);
    videoView.setVideoURI(video);
    videoView.start();

    }
} 

I put a videoview in eclipse for an android app but the video will not play on my phone.

package test.test;

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


public class graphics extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    VideoView videoView = (VideoView) findViewById(R.id.test);
    MediaController mediaController = new MediaController (this);
    mediaController.setAnchorView(videoView);
    Uri video = Uri.parse("http://commonsware.com/misc/test2.3gp");
    videoView.setMediaController(mediaController);
    videoView.setVideoURI(video);
    videoView.start();

    }
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷途知返 2024-12-05 18:48:31

我认为问题在于连接(http)或VideoView的使用。

要了解连接是否是问题,您可以尝试播放手机本地的媒体内容(即来自 SD 卡的媒体内容)。

问题还来自于 VideoView 的使用

VideoView 类使用 SurfaceView 和 MediaPlayer 来实现视频播放。 MediaPlayer 有 API 来设置 url、准备媒体管道、启动管道等。但在管道可以启动之前;管道必须准备好,即处于预卷状态。为了通知应用程序这一点,MediaPlayer 提供了侦听器。在本例中它是 onPrepareListener。
与 MediaPlayer 交互的 VideoView 也(必须?)提供这些侦听器。

看一下下面的 VideoPlayer 活动代码,它使用 VideoView 进行播放。 (仅针对本地内容进行验证)
此活动采用要根据意图播放的文件的绝对路径。 (从列表活动传递)

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;


public class VideoPlayer extends Activity implements OnCompletionListener, OnPreparedListener {
    private static VideoView vView;
    private String filePath;
    public static long clipDurationMS; 
    private View        mLoadingIndicator; 

    public void onCreate(Bundle savedInstanceState)     
    {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        vView = (VideoView)findViewById(R.id.VideoView01);
        mLoadingIndicator = findViewById(R.id.progress_indicator); 

        vView.setBackgroundColor(0x0000); 

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        filePath = (String)extras.get("URL");

        vView.setVideoPath(filePath);  

        MediaController mc;
        mc = new MediaController(this);
        vView.setMediaController(mc);

        vView.requestFocus();
        vView.setOnCompletionListener(this);
        vView.setOnPreparedListener(this);
    }

    public void onCompletion(MediaPlayer arg0) 
    {
        finish();   
    }

    public void onPrepared(MediaPlayer arg0) 
    {
        mLoadingIndicator.setVisibility(View.GONE);
        ViewGroup.LayoutParams params;

        params        = vView.getLayoutParams();

        params.height = arg0.getVideoHeight();
        params.width  = arg0.getVideoWidth();

        vView.setLayoutParams(params);

        vView.start(); 
    }

    public void onStop(){
        super.onStop();
        vView.stopPlayback();
        finish();
    }
}

Shash

I think problem lies either with connectivity (http) or VideoView usage.

To know if the connectivity is the issue, you can try playing media content local to the phone, i.e. from SD Card.

Issues also be coming from VideoView usage

VideoView class uses SurfaceView and MediaPlayer to achieve Video Playback. MediaPlayer has APIs to set url, prepare the media pipeline, start the pipeline etc. But before pipeline can be started; the pipeline has to be ready i.e. in preroll condition. To notify application about this, MediaPlayer provides listeners. In this case it is onPrepareListener.
VideoView which interacts with MediaPlayer also (has to ?) provides these listeners as well.

Have a look below code for VideoPlayer activity which uses VideoView for playback. (Verified for local content only)
This activity takes absolute path to file to be played from intent. (passed from list activity)

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;


public class VideoPlayer extends Activity implements OnCompletionListener, OnPreparedListener {
    private static VideoView vView;
    private String filePath;
    public static long clipDurationMS; 
    private View        mLoadingIndicator; 

    public void onCreate(Bundle savedInstanceState)     
    {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

        vView = (VideoView)findViewById(R.id.VideoView01);
        mLoadingIndicator = findViewById(R.id.progress_indicator); 

        vView.setBackgroundColor(0x0000); 

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        filePath = (String)extras.get("URL");

        vView.setVideoPath(filePath);  

        MediaController mc;
        mc = new MediaController(this);
        vView.setMediaController(mc);

        vView.requestFocus();
        vView.setOnCompletionListener(this);
        vView.setOnPreparedListener(this);
    }

    public void onCompletion(MediaPlayer arg0) 
    {
        finish();   
    }

    public void onPrepared(MediaPlayer arg0) 
    {
        mLoadingIndicator.setVisibility(View.GONE);
        ViewGroup.LayoutParams params;

        params        = vView.getLayoutParams();

        params.height = arg0.getVideoHeight();
        params.width  = arg0.getVideoWidth();

        vView.setLayoutParams(params);

        vView.start(); 
    }

    public void onStop(){
        super.onStop();
        vView.stopPlayback();
        finish();
    }
}

Shash

溺渁∝ 2024-12-05 18:48:31

检查您的手机是否已连接到互联网。在我看来,这可能是一个问题,因为您正在尝试播放来自 Uri 的视频。

Check whether your phone is connected to an Internet connection or not. In my view that might be a problem because you are trying to play a video from an Uri.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文