Java Code Examples for android.media.MediaPlayer#setDisplay()

The following examples show how to use android.media.MediaPlayer#setDisplay() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomVideoView.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public void startPlay() {
        if (mSurfaceHolder == null) {
            return;
        }
        stopPlaying();
        // 关闭系统音乐声音
        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setOnPreparedListener(this);
            mIsPrepared = false;
            mMediaPlayer.setDisplay(mSurfaceHolder);
//            mMediaPlayer.setDataSource(mContext, Uri.parse("http://videojj-cdn.oss-cn-beijing.aliyuncs.com/flash/player/video/6.mp4"));
            mMediaPlayer.setDataSource(mContext, Uri.parse(mCurrentUrl));
//            mMediaPlayer.setDataSource(mContext, Uri.parse("https://static.videojj.com/online/video/Team%20Video%2B%2B_We_Are_Young.mp4"));
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.prepareAsync();
        } catch (Exception ex) {
            VenvyLog.e("media player error" + ex);
        }
    }
 
Example 2
Source File: CustomVideoView.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public void startPlay(String path) {
        if (mSurfaceHolder == null) {
            return;
        }
        if (TextUtils.isEmpty(path)) {
            return;
        }
        mCurrentUrl = path;
        stopPlaying();
        // 关闭系统音乐声音
        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setOnPreparedListener(this);
            mIsPrepared = false;
            mMediaPlayer.setDisplay(mSurfaceHolder);
            mMediaPlayer.setDataSource(mContext, Uri.parse(path));
//            mMediaPlayer.setDataSource(mContext, Uri.parse("https://static.videojj.com/online/video/Team%20Video%2B%2B_We_Are_Young.mp4"));
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.prepareAsync();
        } catch (Exception ex) {
            VenvyLog.e("media player error" + ex);
        }
    }
 
Example 3
Source File: DynamicWallPaper.java    From LiveWallPaper with Apache License 2.0 6 votes vote down vote up
private void initMediaPlayer(SurfaceHolder holder){
    mediaPlayer = new MediaPlayer();
    try {
        AssetManager assetMg = getApplicationContext().getAssets();
        AssetFileDescriptor fileDescriptor = assetMg.openFd(此处资源asset请从鸿洋大神那获取);
        mediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
                fileDescriptor.getStartOffset(), fileDescriptor.getLength());
        mediaPlayer.setDisplay(holder);
        mediaPlayer.prepare();
        mediaPlayer.setLooping(true);
        mediaPlayer.setVolume(0, 0);
        mediaPlayer.prepare();
    }catch (Exception e){
        e.printStackTrace();
    }

}
 
Example 4
Source File: MNViderPlayer.java    From MNVideoPlayer with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void surfaceCreated(SurfaceHolder holder) {
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaPlayer.setDisplay(holder); // 添加到容器中
    //播放完成的监听
    mediaPlayer.setOnCompletionListener(this);
    // 异步准备的一个监听函数,准备好了就调用里面的方法
    mediaPlayer.setOnPreparedListener(this);
    //播放错误的监听
    mediaPlayer.setOnErrorListener(this);
    mediaPlayer.setOnBufferingUpdateListener(this);
    //第一次初始化需不需要主动播放
    if (isFirstPlay) {
        playVideo(videoPath, videoTitle);
    }
    isFirstPlay = true;
}
 
Example 5
Source File: MediaPlayerActivity.java    From SendBird-Android with MIT License 6 votes vote down vote up
private void playVideo() {
    mProgressBar.setVisibility(View.VISIBLE);
    doCleanUp();
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(mUrl);
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.prepareAsync();
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: MediaPlayerActivity.java    From SendBird-Android with MIT License 6 votes vote down vote up
private void playVideo() {
    mProgressBar.setVisibility(View.VISIBLE);
    doCleanUp();
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(mUrl);
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.prepareAsync();
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: LiveStuffH4.java    From goprohero with MIT License 6 votes vote down vote up
@Override
 public void surfaceCreated(SurfaceHolder sh) {
     _mediaPlayer = new MediaPlayer();
     _mediaPlayer.setDisplay(_surfaceHolder);

     Context context = getApplicationContext();
//     Map<String, String> headers = getRtspHeaders();
     Uri source = Uri.parse(RTSP_URL);

     try {
         // Specify the IP camera's URL and auth headers.
      //   _mediaPlayer.setDataSource(context, source, headers);

         // Begin the process of setting up a video stream.
         _mediaPlayer.setOnPreparedListener(this);
         _mediaPlayer.prepareAsync();
     }
     catch (Exception e) {}
 }
 
Example 8
Source File: VideoViewRecycler.java    From MaterialDesignSupport with MIT License 6 votes vote down vote up
public void startCurrentPlayback() {
    if (mIsSurfaceCreated == false) {
        mIsPendingPlayback = true;
        return;
    }

    stopCurrentPlayback();

    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDisplay(getHolder());
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setDataSource(mContext, Uri.parse(mResourcePath));
        mMediaPlayer.prepareAsync();
    } catch (Exception e) {
        Log.i("VideoRow", "Issue trying to set the display!");
    }
}
 
Example 9
Source File: VideoViewerActivity.java    From CameraV with GNU General Public License v3.0 6 votes vote down vote up
private void playVideo(Uri uri) {
doCleanUp();
try {


  // Create a new media player and set the listeners
  mMediaPlayer = new MediaPlayer();
  mMediaPlayer.setDataSource(this, uri);
  mMediaPlayer.setDisplay(holder);
  mMediaPlayer.prepare();
  mMediaPlayer.setOnBufferingUpdateListener(this);
  mMediaPlayer.setOnCompletionListener(this);
  mMediaPlayer.setOnPreparedListener(this);
  mMediaPlayer.setOnVideoSizeChangedListener(this);
  mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

} catch (Exception e) {
  Log.e(TAG, "error: " + e.getMessage(), e);
}
}
 
Example 10
Source File: MainActivity.java    From MultiMediaSample with Apache License 2.0 5 votes vote down vote up
/**
 * 播放输入框的文件
 */
private void play(){
    String path=etPath.getText().toString().trim();
    mMediapPlayer=new MediaPlayer();
    try {
        //设置数据类型
        mMediapPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        //设置以下播放器显示的位置
        mMediapPlayer.setDisplay(holder);

        mMediapPlayer.setDataSource(path);
        mMediapPlayer.prepare();
        mMediapPlayer.start();

        mMediapPlayer .setOnCompletionListener(this);
        //把当前播放器的状诚置为:播放中
        currentstate=PLAYING;

        //把音乐文件的总长度取出来,设置给seekbar作为最大值
        int duration=mMediapPlayer.getDuration();//总时长
        mSeekbar.setMax(duration);
        //把总时间显示textView上
        int m=duration/1000/60;
        int s=duration/1000%60;
        tvTotalTime.setText("/"+m+":"+s);
        tvCurrentTime.setText("00:00");

        isStopUpdatingProgress=false;
        new Thread(new UpdateProgressRunnable()).start();


    }catch(Exception e) {
        e.printStackTrace();
    }
}
 
Example 11
Source File: VideoView.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;
    }
    // we shouldn't clear the target state, because somebody might have
    // called start() previously
    release(false);
    try {
        mMediaPlayer = new MediaPlayer();
        if (mAudioSession != 0) {
            mMediaPlayer.setAudioSessionId(mAudioSession);
        } else {
            mAudioSession = mMediaPlayer.getAudioSessionId();
        }
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
        mMediaPlayer.setOnInfoListener(mInfoListener);
        mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        mMediaPlayer.setLooping(mLooping);
        mMediaPlayer.setDataSource(getContext(), mUri);
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.prepareAsync();

        // we don't set the target state here either, but preserve the
        // target state that was there before.
        mCurrentState = STATE_PREPARING;
        attachMediaController();
    } catch (Exception ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
    }
}
 
Example 12
Source File: UniversalVideoView.java    From LLApp with Apache License 2.0 4 votes vote down vote up
private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;
    }
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    // we shouldn't clear the target state, because somebody might have
    // called start() previously
    release(false);
    try {
        mMediaPlayer = new MediaPlayer();

        if (mAudioSession != 0) {
            mMediaPlayer.setAudioSessionId(mAudioSession);
        } else {
            mAudioSession = mMediaPlayer.getAudioSessionId();
        }
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
        mMediaPlayer.setOnInfoListener(mInfoListener);
        mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        mMediaPlayer.setDataSource(mContext, mUri);
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.prepareAsync();


        // we don't set the target state here either, but preserve the
        // target state that was there before.
        mCurrentState = STATE_PREPARING;
        attachMediaController();
    } catch (IOException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
    }
}
 
Example 13
Source File: SurfaceViewMediaRender.java    From android-jungle-mediaplayer with Apache License 2.0 4 votes vote down vote up
@Override
public void prepareMediaRender(MediaPlayer mediaPlayer) {
    mediaPlayer.setDisplay(mSurfaceView.getHolder());
}
 
Example 14
Source File: SurfaceViewMediaRender.java    From android-jungle-mediaplayer with Apache License 2.0 4 votes vote down vote up
@Override
public void mediaRenderChanged(MediaPlayer mediaPlayer) {
    mediaPlayer.setDisplay(mSurfaceView.getHolder());
}
 
Example 15
Source File: AssetVideoView.java    From ResearchStack with Apache License 2.0 4 votes vote down vote up
private void openVideo() {
    if (mFileDescriptor == null || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;
    }
    // we shouldn't clear the target state, because somebody might have
    // called start() previously
    release(false);

    AudioManager am = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
    am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);

    try {
        mMediaPlayer = new MediaPlayer();

        if (mAudioSession != 0) {
            mMediaPlayer.setAudioSessionId(mAudioSession);
        } else {
            mAudioSession = mMediaPlayer.getAudioSessionId();
        }
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
        mMediaPlayer.setOnInfoListener(mInfoListener);
        mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        mMediaPlayer.setDataSource(mFileDescriptor, mStartOffset, mLength);
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.prepareAsync();


        // we don't set the target state here either, but preserve the
        // target state that was there before.
        mCurrentState = STATE_PREPARING;
        attachMediaController();
    } catch (IOException | IllegalArgumentException ex) {
        Log.w(TAG, "Unable to open content: " + mFileDescriptor, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    }
}
 
Example 16
Source File: MediaPlayerDemo_Video.java    From codeexamples-android with Eclipse Public License 1.0 4 votes vote down vote up
private void playVideo(Integer Media) {
    doCleanUp();
    try {

        switch (Media) {
            case LOCAL_VIDEO:
                /*
                 * TODO: Set the path variable to a local media file path.
                 */
                path = "";
                if (path == "") {
                    // Tell the user to provide a media file URL.
                    Toast
                            .makeText(
                                    MediaPlayerDemo_Video.this,
                                    "Please edit MediaPlayerDemo_Video Activity, "
                                            + "and set the path variable to your media file path."
                                            + " Your media file must be stored on sdcard.",
                                    Toast.LENGTH_LONG).show();

                }
                break;
            case STREAM_VIDEO:
                /*
                 * TODO: Set path variable to progressive streamable mp4 or
                 * 3gpp format URL. Http protocol should be used.
                 * Mediaplayer can only play "progressive streamable
                 * contents" which basically means: 1. the movie atom has to
                 * precede all the media data atoms. 2. The clip has to be
                 * reasonably interleaved.
                 * 
                 */
                path = "";
                if (path == "") {
                    // Tell the user to provide a media file URL.
                    Toast
                            .makeText(
                                    MediaPlayerDemo_Video.this,
                                    "Please edit MediaPlayerDemo_Video Activity,"
                                            + " and set the path variable to your media file URL.",
                                    Toast.LENGTH_LONG).show();

                }

                break;


        }

        // Create a new media player and set the listeners
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(path);
        mMediaPlayer.setDisplay(holder);
        mMediaPlayer.prepare();
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);


    } catch (Exception e) {
        Log.e(TAG, "error: " + e.getMessage(), e);
    }
}