Example usage for android.widget MediaController MediaController

List of usage examples for android.widget MediaController MediaController

Introduction

In this page you can find the example usage for android.widget MediaController MediaController.

Prototype

public MediaController(Context context, boolean useFastForward) 

Source Link

Usage

From source file:se.eliga.aves.songs.BirdSpeciesXenoCantoPlayerFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.audio_player_layout, container, false);

    setHasOptionsMenu(true);/*from   www  .  jav  a 2  s  . c o m*/

    mediaPlayer = new MediaPlayer();
    mediaController = new MediaController(getActivity(), false);
    mediaController.setMediaPlayer(this);
    mediaController.setAnchorView(view);

    mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {

        @Override
        public boolean onError(MediaPlayer mp, int what, int extra) {
            if (what == -38) {
                return true; //not pretty.. http://stackoverflow.com/questions/15205855/error-19-0-mediaplayer/15206308#15206308
            } else {
                return false;
            }
        }
    });

    mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
        @Override
        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            progressBar.setProgress(percent);
        }
    });
    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            handler.post(new Runnable() {
                public void run() {
                    progressBar.setVisibility(ProgressBar.GONE);
                    if (mediaController != null) {
                        mediaController.show(0);
                    }
                    if (mediaPlayer != null) {
                        try {
                            mediaPlayer.start();
                        } catch (Exception e) {
                            Log.d(TAG, "Could not start mediaplayer", e);
                        }
                    }
                }
            });
        }
    });
    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            if (audioAutoNext.isChecked()) {
                int count = getListAdapter().getCount();
                XenoCantoAudio nextAudio;
                if (currentPosition < (count - 1)) {
                    nextAudio = (XenoCantoAudio) getListAdapter().getItem(++currentPosition);
                } else {
                    nextAudio = (XenoCantoAudio) getListAdapter().getItem(0);
                }
                initiatePlay(nextAudio);
            }
        }
    });
    loadBird(getCurrentBird());
    return view;
}

From source file:de.lebenshilfe_muenster.uk_gebaerden_muensterland.sign_video_view.AbstractSignVideoFragment.java

private void initializeMediaController() {
    final MediaController mediaController = new MediaController(getActivity(), false);
    mediaController.setAnchorView(this.videoView);
    this.videoView.setMediaController(mediaController);
}

From source file:net.sf.asap.Player.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Uri uri = getIntent().getData();/*from  www.  jav  a  2s. c om*/
    filename = uri.getLastPathSegment();
    ZipFile zip = null;
    final byte[] module = new byte[ASAPInfo.MAX_MODULE_LENGTH];
    int moduleLen;
    try {
        InputStream is;
        if (Util.isZip(filename)) {
            zip = new ZipFile(uri.getPath());
            filename = uri.getFragment();
            is = zip.getInputStream(zip.getEntry(filename));
        } else {
            try {
                is = getContentResolver().openInputStream(uri);
            } catch (FileNotFoundException ex) {
                if (uri.getScheme().equals("http"))
                    is = httpGet(uri);
                else
                    throw ex;
            }
        }
        moduleLen = readAndClose(is, module);
    } catch (IOException ex) {
        showError(R.string.error_reading_file);
        return;
    } finally {
        Util.close(zip);
    }
    try {
        asap.load(filename, module, moduleLen);
    } catch (Exception ex) {
        showError(R.string.invalid_file);
        return;
    }
    info = asap.getInfo();

    setTitle(R.string.playing_title);
    setContentView(R.layout.playing);
    setTag(R.id.name, info.getTitleOrFilename());
    setTag(R.id.author, info.getAuthor());
    setTag(R.id.date, info.getDate());
    findViewById(R.id.stop_button).setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            finish();
        }
    });
    mediaController = new MediaController(this, false);
    mediaController.setAnchorView(getContentView());
    mediaController.setMediaPlayer(new MediaController.MediaPlayerControl() {
        public boolean canPause() {
            return !isPaused();
        }

        public boolean canSeekBackward() {
            return false;
        }

        public boolean canSeekForward() {
            return false;
        }

        public int getBufferPercentage() {
            return 100;
        }

        public int getCurrentPosition() {
            return asap.getPosition();
        }

        public int getDuration() {
            return info.getDuration(song);
        }

        public boolean isPlaying() {
            return !isPaused();
        }

        public void pause() {
            audioTrack.pause();
        }

        public void seekTo(int pos) {
            seek(pos);
        }

        public void start() {
            resume();
        }
    });
    if (info.getSongs() > 1) {
        mediaController.setPrevNextListeners(new OnClickListener() {
            public void onClick(View v) {
                playNextSong();
            }
        }, new OnClickListener() {
            public void onClick(View v) {
                playPreviousSong();
            }
        });
    }
    new Handler().postDelayed(new Runnable() {
        public void run() {
            mediaController.show();
        }
    }, 500);

    stop = false;
    playSong(info.getDefaultSong());
    new Thread(this).start();
}

From source file:com.nagravision.mediaplayer.FullscreenActivity.java

/**
 *
 *///from  w  w w .  ja v a 2 s. c om
@SuppressLint("InlinedApi")
@Override
protected void onCreate(Bundle inBundle) {
    Log.v(LOG_TAG, "FullscreenActivity::onCreate - Enter\n");
    super.onCreate(inBundle);

    getApplication().registerActivityLifecycleCallbacks(this);

    if (Build.VERSION.SDK_INT < 16) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.activity_fullscreen);
    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    mMoviesList = (ListView) findViewById(R.id.start_drawer);
    mUrlsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1, MOVIES_ARR);
    mMoviesList.setAdapter(mUrlsAdapter);
    mVideoHolder = (VideoView) findViewById(R.id.fullscreen_content);

    View decorView = getWindow().getDecorView();
    // Hide the status bar.
    int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_FULLSCREEN;
    if (Build.VERSION.SDK_INT >= 19) {
        uiOptions |= View.SYSTEM_UI_FLAG_IMMERSIVE;
    }
    decorView.setSystemUiVisibility(uiOptions);
    // Remember that you should never show the action bar if the
    // status bar is hidden, so hide that too if necessary.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
    actionBar.hide();

    mNotifBuilder = new Notification.Builder(this);
    mNotifMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mExplicitIntent = new Intent(Intent.ACTION_VIEW);
    mExplicitIntent.setClass(this, this.getClass());
    mDefaultIntent = PendingIntent.getActivity(this, REQUEST_DISPLAY, mExplicitIntent,
            Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_DEBUG_LOG_RESOLUTION);
    mInfosActionIntent = new Intent();
    mInfosActionIntent.setAction("com.nagravision.mediaplayer.VIDEO_INFOS");
    mInfosActionIntent.addCategory(Intent.CATEGORY_DEFAULT);
    mInfosActionIntent.addCategory(Intent.CATEGORY_INFO);
    mInfosIntent = PendingIntent.getActivity(this, REQUEST_INFOS, mInfosActionIntent,
            Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_DEBUG_LOG_RESOLUTION);

    if (Build.VERSION.SDK_INT >= 19) {
        mVideoHolder.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE);
    } else {
        mVideoHolder.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_FULLSCREEN);
    }
    OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
        public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position,
                long id) {
            Log.v(LOG_TAG, "FullscreenActivity.onCreate.OnItemClickListener::onItemClick - Enter\n");
            ClickItem(position, 0);
        }
    };

    mMoviesList.setOnItemClickListener(mMessageClickedHandler);

    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    mControlsView = new MediaController(this, true);
    mControlsView.setPrevNextListeners(new View.OnClickListener() {
        /*
         * Next listener
         */
        @Override
        public void onClick(View view) {
            Log.v(LOG_TAG, "FullscreenActivity.onCreate.OnClickListener::onClick(next) - Enter\n");
            mVideoHolder.stopPlayback();
            int position = 0;
            if (mLastPosition > 0)
                position = mLastPosition + 1;
            if (position >= MOVIES_URLS.length)
                position = 0;
            ClickItem(position, 0);

            String toaststr = getResources().getString(R.string.next_movie_toast_string) + MOVIES_ARR[position];
            Toast.makeText(FullscreenActivity.this, toaststr, Toast.LENGTH_LONG).show();
        }
    }, new View.OnClickListener() {
        /*
         * Prev listener
         */
        @Override
        public void onClick(View view) {
            Log.v(LOG_TAG, "FullscreenActivity.onCreate.OnClickListener::onClick(prev) - Enter\n");
            mVideoHolder.stopPlayback();
            int position = 0;
            if (mLastPosition > 0)
                position = mLastPosition - 1;
            if (position < 0)
                position = MOVIES_URLS.length - 1;
            ClickItem(position, 0);

            String toaststr = getResources().getString(R.string.prev_movie_toast_string) + MOVIES_ARR[position];
            Toast.makeText(FullscreenActivity.this, toaststr, Toast.LENGTH_LONG).show();
        }
    });
    mVideoHolder.setMediaController(mControlsView);

    // Set up an instance of SystemUiHider to control the system UI for
    // this activity.
    mSystemUiHider = SystemUiHider.getInstance(this, mVideoHolder, HIDER_FLAGS);
    mSystemUiHider.setup();
    mSystemUiHider.setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() {
        // Cached values.
        int mControlsHeight;
        int mShortAnimTime;

        @Override
        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
        public void onVisibilityChange(boolean visible) {
            Log.v(LOG_TAG, "FullscreenActivity.OnVisibilityChangeListener::onVisibilityChange - Enter\n");

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
                if (mControlsHeight == 0)
                    mControlsHeight = mControlsView.getHeight();
                if (mShortAnimTime == 0)
                    mShortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
                mControlsView.animate().translationY(visible ? 0 : mControlsHeight).setDuration(mShortAnimTime);
            } else
                mControlsView.setVisibility(visible ? View.VISIBLE : View.GONE);

            if (visible && AUTO_HIDE)
                delayedHide(AUTO_HIDE_DELAY_MILLIS);
        }
    });

    // Set up the user interaction to manually show or hide the system UI.
    mVideoHolder.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (TOGGLE_ON_CLICK) {
                mSystemUiHider.toggle();
            } else {
                mSystemUiHider.show();
            }
        }
    });

    mDrawer.openDrawer(mMoviesList);
}