将视频和图片从本地存储加载到Google ChromeCast android

时间:2016-04-20 08:34:08

标签: android chromecast

如何将本地视频URI /路径/文件设置为chromecast的MediaInfo.Builder()?另外,任何人都可以帮我一个关于如何从Android存储本地加载图像和视频并将其转换为谷歌Chrome演员的代码片段? 阅读其他问题,但在那里已经提到在应用程序上创建本地服务器。如果真的需要这样做,那么如何实现呢?我正在使用https://github.com/googlecast/CastVideos-android上提供的代码,但在那里可以从网址获取和播放视频。我想从本地存储加载视频,我也想显示图像。

更新: 现在我使用“NanoHttpD”创建了一个本地服务器,但无法获取所选本地文件的URL。以下是我的Acitivities代码段:

public class VideoBrowserFragment extends Fragment implements VideoListAdapter.ItemClickListener,
        LoaderManager.LoaderCallbacks<List<MediaInfo>> {

    private static final String TAG = "zz VideoBrowserFragment";
    private static final int REQUEST_TAKE_GALLERY_VIDEO = 123;
    private static final String CATALOG_URL =
            "http://commondatastorage.googleapis.com/gtv-videos-bucket/CastVideos/f.json";
    private RecyclerView mRecyclerView;
    private VideoListAdapter mAdapter;
    private View mEmptyView;
    private View mLoadingView;
    private VideoCastManager mCastManager;
    private VideoCastConsumerImpl mCastConsumer;
    private View btnLocalVideo;
    LocalServer server;
    String chosenExt = "";
    String localFileName = "";
    String localFilePath = "";
    String formatedIpAddress = "";
    Uri selectedImageUri;

    public VideoBrowserFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        Toast.makeText(getActivity(), "VideoBrowserFragment onCreateView", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "VideoBrowserFragment onCreateView");
        return inflater.inflate(R.layout.video_browser_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Toast.makeText(getActivity(), "VideoBrowserFragment onViewCreated", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "VideoBrowserFragment onViewCreated");

        mRecyclerView = (RecyclerView) getView().findViewById(R.id.list);
        mEmptyView = getView().findViewById(R.id.empty_view);
        mLoadingView = getView().findViewById(R.id.progress_indicator);
        btnLocalVideo = getView().findViewById(R.id.btnLocalVideo);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new VideoListAdapter(this);
        mRecyclerView.setAdapter(mAdapter);
        getLoaderManager().initLoader(0, null, this);
        mCastManager = VideoCastManager.getInstance();
        mCastConsumer = new VideoCastConsumerImpl() {
            @Override
            public void onConnected() {
                super.onConnected();
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onDisconnected() {
                super.onDisconnected();
                mAdapter.notifyDataSetChanged();
            }
        };
        mCastManager.addVideoCastConsumer(mCastConsumer);


//fetch local video
        btnLocalVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent();
                intent.setType("video/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_TAKE_GALLERY_VIDEO);


            }
        });
    }


    //get data of locally fetched videos....
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == getActivity().RESULT_OK) {
            if (requestCode == REQUEST_TAKE_GALLERY_VIDEO) {
                 selectedImageUri = data.getData();

                Toast.makeText(getActivity(), "Sending local video data", Toast.LENGTH_SHORT).show();
                MediaMetadata movieMetadata = new MediaMetadata(MediaMetadata.MEDIA_TYPE_MOVIE);

                movieMetadata.putString(MediaMetadata.KEY_SUBTITLE, "Local Subtitle");
                movieMetadata.putString(MediaMetadata.KEY_TITLE, "Local Video is playing");

                JSONObject jsonObj = null;
                try {
                    jsonObj = new JSONObject();
                    jsonObj.put("description", "Sample Descrioption over here");
                } catch (JSONException e) {
                    Log.e(TAG, "Failed to add description to the json object", e);
                }
                String filePath = getPath(selectedImageUri);
                chosenExt = filePath.substring(filePath.lastIndexOf(".") + 1);
                startLocalServer();
                String localUrl = formatedIpAddress + localFilePath;
                Log.d(TAG, "MediaInfo urlLocal: " + localUrl);
                MediaInfo item = new MediaInfo.Builder(localUrl)
                        .setStreamType(MediaInfo.STREAM_TYPE_BUFFERED)
                        .setContentType("video/mp4")
                        .setMetadata(movieMetadata)
                        .setStreamDuration(333 * 1000)
                        .build();
                Log.d(TAG, "VideoBrowserFragment itemClicked media :" + Utils.mediaInfoToBundle(item));

                Intent intent = new Intent(getActivity(), LocalPlayerActivity.class);
                intent.putExtra("media", Utils.mediaInfoToBundle(item));
                intent.putExtra("shouldStart", false);
                intent.putExtra("isLocal", true);
                intent.putExtra("videoUri", selectedImageUri.toString());
               /* Pair<View, String> imagePair = Pair
                        .create((View) viewHolder.getImageView(), transitionName);*/
                ActivityOptionsCompat options = ActivityOptionsCompat
                        .makeSceneTransitionAnimation(getActivity());
                ActivityCompat.startActivity(getActivity(), intent, options.toBundle());

            }
        }
    }


    //get path of video fetched...
    public String getPath(Uri uri) {
        Log.d(TAG, "uri is " + uri);
        String[] proj = {MediaStore.Images.Media.DATA};
        CursorLoader loader = new CursorLoader(getActivity(), uri, proj, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();

        if (cursor.moveToFirst()) {
            //int column_index1 = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);//Instead of "MediaStore.Images.Media.DATA" can be used "_data"
            Uri filePathUri = Uri.parse(cursor.getString(column_index));
            String file_name = filePathUri.getLastPathSegment().toString();
            String file_path = filePathUri.getPath();
            localFileName = file_name;
            localFilePath = file_path;
            Toast.makeText(getActivity(), "File Name & PATH are:" + file_name + "\n" + file_path, Toast.LENGTH_LONG).show();
            Log.d("zz fileData: ", "fp: " + file_path + " :and: fn: " + file_name);
        }
        String result = cursor.getString(column_index);
        cursor.close();
        return result;
    }

    @Override
    public void onDetach() {
        Toast.makeText(getActivity(), "VideoBrowserFragment onDetach", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "VideoBrowserFragment onDetach");
        mCastManager.removeVideoCastConsumer(mCastConsumer);
        super.onDetach();
    }

    @Override
    public void itemClicked(View view, MediaInfo item, int position) {
        Toast.makeText(getActivity(), "VideoBrowserFragment itemClicked", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "VideoBrowserFragment itemClicked");

        if (view instanceof ImageButton) {
            Log.d(TAG, "menu was clicked");
            com.scriptlanes.cast2.utils.Utils.showQueuePopup(getActivity(), view, item);
        } else {
            String transitionName = getString(R.string.transition_image);
            VideoListAdapter.ViewHolder viewHolder =
                    (VideoListAdapter.ViewHolder) mRecyclerView.findViewHolderForPosition(position);
            Pair<View, String> imagePair = Pair
                    .create((View) viewHolder.getImageView(), transitionName);
            ActivityOptionsCompat options = ActivityOptionsCompat
                    .makeSceneTransitionAnimation(getActivity(), imagePair);

            Intent intent = new Intent(getActivity(), LocalPlayerActivity.class);
            Log.d(TAG, "VideoBrowserFragment itemClicked media :" + Utils.mediaInfoToBundle(item));

            intent.putExtra("media", Utils.mediaInfoToBundle(item));
            intent.putExtra("shouldStart", false);
            //isUrl=true, isLocal=false
            intent.putExtra("isLocal", false);
            ActivityCompat.startActivity(getActivity(), intent, options.toBundle());
        }
    }

    @Override
    public Loader<List<MediaInfo>> onCreateLoader(int id, Bundle args) {
        Toast.makeText(getActivity(), "VideoBrowserFragment onCreateLoader", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "VideoBrowserFragment onCreateLoader");

        return new VideoItemLoader(getActivity(), CATALOG_URL);
    }

    @Override
    public void onLoadFinished(Loader<List<MediaInfo>> loader, List<MediaInfo> data) {
        Toast.makeText(getActivity(), "VideoBrowserFragment onLoadFinished", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "VideoBrowserFragment onLoadFinished");
        mAdapter.setData(data);
        mLoadingView.setVisibility(View.GONE);
        mEmptyView.setVisibility(null == data || data.isEmpty() ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onLoaderReset(Loader<List<MediaInfo>> loader) {
        Toast.makeText(getActivity(), "VideoBrowserFragment onLoaderReset", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "VideoBrowserFragment onLoaderReset");
        mAdapter.setData(null);
    }



    /*temp code nanohttpd*/


    public class LocalServer extends NanoHTTPD {

        private  final Logger LOG = Logger.getLogger(LocalServer.class.getName());

        public  void main(String[] args) {
            ServerRunner.run(LocalServer.class);
        }
        public LocalServer() {
            super(3000);
            Log.d("zz nanaHttpd: ", "STARTED");

        }

       @Override
        public Response serve(String uri, Method method, Map<String, String> header, Map<String, String> parameters, Map<String, String> files) {
            //String mediasend = getExtension(chosenFile);

           Log.d("zz nanaHttpd:  ", "uri:  " + uri+" , method: "+method);
           FileInputStream fis = null;
           File file = new File(localFilePath);
           Log.e("Creando imputStream", "Size: ");
           try {
               fis = new FileInputStream(file);
           } catch (FileNotFoundException e) {
               e.printStackTrace();
           }

            return new NanoHTTPD.Response(Response.Status.OK, "video/mp4", fis);
        }

    }

    private void startLocalServer() {
        server = new LocalServer();
        try {
            server.start();
        } catch (IOException ioe) {
            Log.d("zz Httpd", "The server could not start.");
        }
        Log.d("zz Httpd", "Web server initialized.");
        WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
        int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
        Log.d("zz Httpd IP U: ", ipAddress + "");
        String formatedIpAddress1 = String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff),
                (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff));
        formatedIpAddress = "http://" + formatedIpAddress1 + ":" + server.getListeningPort();
        Log.d("zz Httpd IP: ", "Please access! " + formatedIpAddress);


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //server.stop();
    }
}

1 个答案:

答案 0 :(得分:0)

正如您在其他帖子中看到的那样,您需要向应用添加嵌入式Web服务器,并在该Web服务器中公开本地内容。然后,您需要将这些本地内容的网址发送到您的chromecast。有许多开源的小型嵌入式http服务器可以使用,如果你不想构建编写自己的小型http服务器,你可以谷歌搜索这样的嵌入式解决方案。