视频流与媒体播放器

时间:2014-04-26 07:51:37

标签: android video streaming video-streaming android-mediaplayer

我是新开发者,我希望我的应用能够流式传输视频。我基本上希望它像youtube一样工作,能够从自己的数据库/服务器流式传输视频(不知道它是如何工作的)。我搜索了一下从媒体播放器流式传输YouTube视频的方法,并在几个地方找到了下面的代码。 我还在xml文件中添加了一个VideoView。但它似乎有某种问题,我真的不知道它是什么。代码与我发现的唯一不同之处在于我将其修改为从片段而不是从活动中进行操作,因为现在我的应用只包含一个活动和四个三个片段。

所以我有两个问题:1。有没有人知道我的实施问题是什么? 2.这是我与服务器交互的方式,如果我在该服务器上拥有所有视频 - 只需写入网址?感谢。

import android.content.Intent;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.widget.VideoView;


public class main_page extends Fragment {
    public main_page() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View rootView = inflater.inflate(R.layout.main_page, container, false);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        VideoView videoView=(VideoView)getActivity().findViewById(R.id.vvMainPage);


        MediaController mc=new MediaController(getActivity());
        videoView.setMediaController(mc);

        String str="https://www.youtube.com/watch?v=fvEZUbzqqyM";
        Uri uri=Uri.parse(str);

        videoView.setVideoURI(uri);

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


    }

}

logcat的:

04-24 18:00:59.956: W/EGL_emulation(1905): eglSurfaceAttrib not implemented
04-24 18:01:00.128: D/OpenGLRenderer(1905): TextureCache::get: create texture(0xb989d8b8): name, size, mSize = 242, 1048576, 1100992
04-24 18:01:00.352: D/OpenGLRenderer(1905): TextureCache::get: create texture(0xb98a8d48): name, size, mSize = 244, 100, 1101092
04-24 18:01:00.476: D/OpenGLRenderer(1905): TextureCache::get: create texture(0xb98957e8): name, size, mSize = 246, 7488, 1108580
04-24 18:01:03.620: D/OpenGLRenderer(1905): TextureCache::get: create texture(0xb9899820): name, size, mSize = 255, 7488, 1116068
04-24 18:01:05.500: D/getUser (dev)(1905): First name: dev Last name: dev Email: dev@dev.com Username: dev Password: 123
04-24 18:01:05.616: W/EGL_emulation(1905): eglSurfaceAttrib not implemented
04-24 18:01:05.640: D/MediaPlayer(1905): Couldn't open file on client side, trying server side
04-24 18:01:05.652: E/MediaPlayer(1905): Unable to to create media player
04-24 18:01:05.652: W/VideoView(1905): Unable to open content: https://www.youtube.com/watch?v=fvEZUbzqqyM
04-24 18:01:05.652: W/VideoView(1905): java.io.IOException: setDataSource failed.: status=0x80000000
04-24 18:01:05.652: W/VideoView(1905):  at android.media.MediaPlayer._setDataSource(Native Method)
04-24 18:01:05.652: W/VideoView(1905):  at android.media.MediaPlayer.setDataSource(MediaPlayer.java:958)
04-24 18:01:05.652: W/VideoView(1905):  at android.media.MediaPlayer.setDataSource(MediaPlayer.java:946)
04-24 18:01:05.652: W/VideoView(1905):  at android.media.MediaPlayer.setDataSource(MediaPlayer.java:899)
04-24 18:01:05.652: W/VideoView(1905):  at android.widget.VideoView.openVideo(VideoView.java:235)
04-24 18:01:05.652: W/VideoView(1905):  at android.widget.VideoView.access$2000(VideoView.java:51)
04-24 18:01:05.652: W/VideoView(1905):  at android.widget.VideoView$6.surfaceCreated(VideoView.java:478)
04-24 18:01:05.652: W/VideoView(1905):  at android.view.SurfaceView.updateWindow(SurfaceView.java:543)
04-24 18:01:05.652: W/VideoView(1905):  at android.view.SurfaceView.access$000(SurfaceView.java:81)
04-24 18:01:05.652: W/VideoView(1905):  at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
04-24 18:01:05.652: W/VideoView(1905):  at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
04-24 18:01:05.652: W/VideoView(1905):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1818)
04-24 18:01:05.652: W/VideoView(1905):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
04-24 18:01:05.652: W/VideoView(1905):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
04-24 18:01:05.652: W/VideoView(1905):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
04-24 18:01:05.652: W/VideoView(1905):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
04-24 18:01:05.652: W/VideoView(1905):  at android.view.Choreographer.doFrame(Choreographer.java:525)
04-24 18:01:05.652: W/VideoView(1905):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
04-24 18:01:05.652: W/VideoView(1905):  at android.os.Handler.handleCallback(Handler.java:615)
04-24 18:01:05.652: W/VideoView(1905):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-24 18:01:05.652: W/VideoView(1905):  at android.os.Looper.loop(Looper.java:137)
04-24 18:01:05.652: W/VideoView(1905):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-24 18:01:05.652: W/VideoView(1905):  at java.lang.reflect.Method.invokeNative(Native Method)
04-24 18:01:05.652: W/VideoView(1905):  at java.lang.reflect.Method.invoke(Method.java:511)
04-24 18:01:05.652: W/VideoView(1905):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-24 18:01:05.652: W/VideoView(1905):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-24 18:01:05.652: W/VideoView(1905):  at dalvik.system.NativeStart.main(Native Method)
04-24 18:01:05.656: D/VideoView(1905): Error: 1,0
04-24 18:01:05.808: W/EGL_emulation(1905): eglSurfaceAttrib not implemented
04-24 18:01:05.820: D/OpenGLRenderer(1905): TextureCache::get: create texture(0xb98aea90): name, size, mSize = 263, 19008, 1135076
04-24 18:01:05.832: D/OpenGLRenderer(1905): TextureCache::get: create texture(0xb98aa748): name, size, mSize = 264, 19008, 1154084
04-24 18:01:05.844: D/OpenGLRenderer(1905): TextureCache::get: create texture(0xb989d1d0): name, size, mSize = 265, 4, 1154088
04-24 18:01:07.248: D/OpenGLRenderer(1905): TextureCache::get: create texture(0xb98acd60): name, size, mSize = 282, 324, 1154412
04-24 18:01:09.852: D/OpenGLRenderer(1905): TextureCache::flush: target size: 692647
04-24 18:01:09.856: D/OpenGLRenderer(1905): TextureCache::callback: name, removed size, mSize = 246, 7488, 1146924
04-24 18:01:09.856: D/OpenGLRenderer(1905): TextureCache::callback: name, removed size, mSize = 255, 7488, 1139436
04-24 18:01:09.856: D/OpenGLRenderer(1905): TextureCache::callback: name, removed size, mSize = 244, 100, 1139336
04-24 18:01:09.856: D/OpenGLRenderer(1905): TextureCache::callback: name, removed size, mSize = 282, 324, 1139012
04-24 18:01:09.856: D/OpenGLRenderer(1905): TextureCache::callback: name, removed size, mSize = 263, 19008, 1120004
04-24 18:01:09.856: D/OpenGLRenderer(1905): TextureCache::callback: name, removed size, mSize = 264, 19008, 1100996
04-24 18:01:09.856: D/OpenGLRenderer(1905): TextureCache::callback: name, removed size, mSize = 265, 4, 1100992
04-24 18:01:09.856: D/OpenGLRenderer(1905): TextureCache::callback: name, removed size, mSize = 242, 1048576, 52416

1 个答案:

答案 0 :(得分:0)

试试这个

Uri uri = Uri.parse("first_uri");
MediaController mediaController = new MediaController(this);
VideoView videoView = (VideoView) findViewById(R.id.video);

    mediaController.setAnchorView(videoView);
    videoView.setVideoURI(uri);
    videoView.setMediaController(mediaController);
    videoView.start();