app/src/main/java/fi/finwe/orion360/sdk/pro/examples/minimal/MinimalVideoDownloadPlayer.java
public void downloadAndPlay(String videoUrl) {

        // Create a filename for the local copy of the video file.
        String fileName = videoUrl.substring(videoUrl.lastIndexOf('/') + 1);

        // Skip download, if a file with that name already exists in our downloads directory.
        // Notice that we need to remove scheme from the URI (file://) for file existence check.
        // Notice that we should check if external media is mounted (omitted here for simplicity).
        String localUri = PRIVATE_EXTERNAL_FILES_PATH + Environment.DIRECTORY_DOWNLOADS +
                File.separator + fileName;
        if (new File(Uri.parse(localUri).getPath()).exists()) {
            setContentUri(localUri); // Play already downloaded video file.
            return;
        }

        // Create a progress bar to be shown while downloading the file.
        final ProgressDialog progress = new ProgressDialog(this);
        progress.setTitle(getString(R.string.player_file_download_title));
        progress.setMessage(String.format(getString(R.string.player_file_download_message), fileName));
        progress.setMax(100);
        progress.setIndeterminate(false);
        progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progress.show();

        // Create and register a receiver for handling a completed download.
        final BroadcastReceiver receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {

                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {

                    DownloadManager.Query query = new DownloadManager.Query();
                    query.setFilterById(mDownloadId);
                    Cursor c = mDownloadManager.query(query);
                    if (c.moveToFirst()) {

                        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {

                            mProgressTimer.cancel();
                            progress.cancel();
                            unregisterReceiver(this);

                            Toast.makeText(MinimalVideoDownloadPlayer.this,
                                    String.format(getString(
                                            R.string.player_file_download_completed), 1),
                                    Toast.LENGTH_LONG).show();

                            String uriString = c.getString(c.getColumnIndex(
                                    DownloadManager.COLUMN_LOCAL_URI));
                            setContentUri(uriString); // Play just downloaded video file.

                        }
                    }
                }
            }
        };
        registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

        // Use download manager to download the file.
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(videoUrl));
        request.setTitle(getResources().getString(R.string.app_name));
        request.setDescription(videoUrl);
        request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, fileName);
        mDownloadId = mDownloadManager.enqueue(request);

        // Create a background task for updating download progress.
        mProgressTimer.schedule(new TimerTask() {

            @Override
            public void run() {

                DownloadManager.Query q = new DownloadManager.Query();
                q.setFilterById(mDownloadId);
                Cursor cursor = mDownloadManager.query(q);
                cursor.moveToFirst();
                int downloaded = cursor.getInt(cursor.getColumnIndex(
                        DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                int total = cursor.getInt(cursor.getColumnIndex(
                        DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                cursor.close();
                final int percent = (int) (100.0 * downloaded / total);
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        progress.setProgress(percent);
                    }

                });

            }

        }, 500, 500);

        // Notice that we have omitted here handling of all situations where download gets canceled!

    }
/**
     * Downloads a video file over the network to the local file system, then plays it.
     *
     * @param videoUrl The URL to the video file to be downloaded and played.
     */
Downloads a video file over the network to the local file system, then plays it.
@param videoUrl The URL to the video file to be downloaded and played.
Downloads a video file over the network to the local file system then plays it . @param videoUrl The URL to the video file to be downloaded and played .
public void downloadAndPlay(String videoUrl) {
        String fileName = videoUrl.substring(videoUrl.lastIndexOf('/') + 1);
        String localUri = PRIVATE_EXTERNAL_FILES_PATH + Environment.DIRECTORY_DOWNLOADS +
                File.separator + fileName;
        if (new File(Uri.parse(localUri).getPath()).exists()) {
            setContentUri(localUri); 
            return;
        }
        final ProgressDialog progress = new ProgressDialog(this);
        progress.setTitle(getString(R.string.player_file_download_title));
        progress.setMessage(String.format(getString(R.string.player_file_download_message), fileName));
        progress.setMax(100);
        progress.setIndeterminate(false);
        progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progress.show();
        final BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
                    DownloadManager.Query query = new DownloadManager.Query();
                    query.setFilterById(mDownloadId);
                    Cursor c = mDownloadManager.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                            mProgressTimer.cancel();
                            progress.cancel();
                            unregisterReceiver(this);
                            Toast.makeText(MinimalVideoDownloadPlayer.this,
                                    String.format(getString(
                                            R.string.player_file_download_completed), 1),
                                    Toast.LENGTH_LONG).show();
                            String uriString = c.getString(c.getColumnIndex(
                                    DownloadManager.COLUMN_LOCAL_URI));
                            setContentUri(uriString); 
                        }
                    }
                }
            }
        };
        registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(videoUrl));
        request.setTitle(getResources().getString(R.string.app_name));
        request.setDescription(videoUrl);
        request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, fileName);
        mDownloadId = mDownloadManager.enqueue(request);
        mProgressTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                DownloadManager.Query q = new DownloadManager.Query();
                q.setFilterById(mDownloadId);
                Cursor cursor = mDownloadManager.query(q);
                cursor.moveToFirst();
                int downloaded = cursor.getInt(cursor.getColumnIndex(
                        DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                int total = cursor.getInt(cursor.getColumnIndex(
                        DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                cursor.close();
                final int percent = (int) (100.0 * downloaded / total);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        progress.setProgress(percent);
                    }
                });
            }
        }, 500, 500);
    }
public void downloadAndPlay ( String videoUrl ) { String fileName = videoUrl . substring ( videoUrl . lastIndexOf ( '/' ) + 1 ) ; String localUri = PRIVATE_EXTERNAL_FILES_PATH + Environment . DIRECTORY_DOWNLOADS + File . separator + fileName ; if ( new File ( Uri . parse ( localUri ) . getPath ( ) ) . exists ( ) ) { setContentUri ( localUri ) ; return ; } final ProgressDialog progress = new ProgressDialog ( this ) ; progress . setTitle ( getString ( R . string . player_file_download_title ) ) ; progress . setMessage ( String . format ( getString ( R . string . player_file_download_message ) , fileName ) ) ; progress . setMax ( 100 ) ; progress . setIndeterminate ( false ) ; progress . setProgressStyle ( ProgressDialog . STYLE_HORIZONTAL ) ; progress . show ( ) ; final BroadcastReceiver receiver = new BroadcastReceiver ( ) { @ Override public void onReceive ( Context context , Intent intent ) { if ( DownloadManager . ACTION_DOWNLOAD_COMPLETE . equals ( intent . getAction ( ) ) ) { DownloadManager . Query query = new DownloadManager . Query ( ) ; query . setFilterById ( mDownloadId ) ; Cursor c = mDownloadManager . query ( query ) ; if ( c . moveToFirst ( ) ) { int columnIndex = c . getColumnIndex ( DownloadManager . COLUMN_STATUS ) ; if ( DownloadManager . STATUS_SUCCESSFUL == c . getInt ( columnIndex ) ) { mProgressTimer . cancel ( ) ; progress . cancel ( ) ; unregisterReceiver ( this ) ; Toast . makeText ( MinimalVideoDownloadPlayer . this , String . format ( getString ( R . string . player_file_download_completed ) , 1 ) , Toast . LENGTH_LONG ) . show ( ) ; String uriString = c . getString ( c . getColumnIndex ( DownloadManager . COLUMN_LOCAL_URI ) ) ; setContentUri ( uriString ) ; } } } } } ; registerReceiver ( receiver , new IntentFilter ( DownloadManager . ACTION_DOWNLOAD_COMPLETE ) ) ; DownloadManager . Request request = new DownloadManager . Request ( Uri . parse ( videoUrl ) ) ; request . setTitle ( getResources ( ) . getString ( R . string . app_name ) ) ; request . setDescription ( videoUrl ) ; request . setDestinationInExternalFilesDir ( this , Environment . DIRECTORY_DOWNLOADS , fileName ) ; mDownloadId = mDownloadManager . enqueue ( request ) ; mProgressTimer . schedule ( new TimerTask ( ) { @ Override public void run ( ) { DownloadManager . Query q = new DownloadManager . Query ( ) ; q . setFilterById ( mDownloadId ) ; Cursor cursor = mDownloadManager . query ( q ) ; cursor . moveToFirst ( ) ; int downloaded = cursor . getInt ( cursor . getColumnIndex ( DownloadManager . COLUMN_BYTES_DOWNLOADED_SO_FAR ) ) ; int total = cursor . getInt ( cursor . getColumnIndex ( DownloadManager . COLUMN_TOTAL_SIZE_BYTES ) ) ; cursor . close ( ) ; final int percent = ( int ) ( 100.0 * downloaded / total ) ; runOnUiThread ( new Runnable ( ) { @ Override public void run ( ) { progress . setProgress ( percent ) ; } } ) ; } } , 500 , 500 ) ; }
Downloads a video file over the network to the local file system, then plays it.
Downloads a video file over the network to the local file system then plays it .
// Create a filename for the local copy of the video file. // Skip download, if a file with that name already exists in our downloads directory. // Notice that we need to remove scheme from the URI (file://) for file existence check. // Notice that we should check if external media is mounted (omitted here for simplicity). // Play already downloaded video file. // Create a progress bar to be shown while downloading the file. // Create and register a receiver for handling a completed download. // Play just downloaded video file. // Use download manager to download the file. // Create a background task for updating download progress. // Notice that we have omitted here handling of all situations where download gets canceled!