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

The following examples show how to use android.media.MediaPlayer#setSurface() . 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: ChatInputView.java    From aurora-imui with MIT License 6 votes vote down vote up
private void playVideo() {
    try {
        mCameraSupport.release();
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setDataSource(mVideoFilePath);
        Surface surface = new Surface(mTextureView.getSurfaceTexture());
        mMediaPlayer.setSurface(surface);
        surface.release();
        mMediaPlayer.setLooping(true);
        mMediaPlayer.prepareAsync();
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mediaPlayer.start();
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 2
Source File: VideoLiveWallpaper.java    From LLApp with Apache License 2.0 6 votes vote down vote up
@Override
public void onSurfaceCreated(SurfaceHolder holder) {
    LogUtils.d(TAG,"VideoEngine#onSurfaceCreated ");
    super.onSurfaceCreated(holder);
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setSurface(holder.getSurface());
    try {
        AssetManager assetMg = getApplicationContext().getAssets();
        AssetFileDescriptor fileDescriptor = assetMg.openFd("test1.mp4");
        mMediaPlayer.setDataSource(fileDescriptor.getFileDescriptor(),
                fileDescriptor.getStartOffset(), fileDescriptor.getLength());
        mMediaPlayer.setLooping(true);
        mMediaPlayer.setVolume(0, 0);
        mMediaPlayer.prepare();
        mMediaPlayer.start();

    } catch (IOException e) {
        e.printStackTrace();
    }

}
 
Example 3
Source File: TutorialFragment.java    From shortrain with MIT License 6 votes vote down vote up
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    Surface s = new Surface(surface);

    int movieId = getArguments().getInt(MOVIE_RES_ID);
    try {
        mediaPlayer = new MediaPlayer();
        String path = "android.resource://" + getContext().getPackageName() + "/" + movieId;
        mediaPlayer.setDataSource(getContext(), Uri.parse(path));
        mediaPlayer.setSurface(s);
        mediaPlayer.prepare();
        mediaPlayer.setLooping(true);
        if (isVisible()) {
            videoIsVisibleToUser();
        }
    } catch (Exception ignore) {
        // This is an experiment...
    }
}
 
Example 4
Source File: MainActivity.java    From WeiXinRecordedDemo with MIT License 6 votes vote down vote up
private void playVideo(String videoPath){

        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(videoPath);
            mMediaPlayer.setSurface(new Surface(textureView.getSurfaceTexture()));
            mMediaPlayer.setLooping(true);
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mp.start();

                    float ratio = mp.getVideoWidth()*1f/mp.getVideoHeight();
                    int width = textureView.getWidth();
                    ViewGroup.LayoutParams layoutParams = textureView.getLayoutParams();
                    layoutParams.height = (int) (width/ratio);
                    textureView.setLayoutParams(layoutParams);
                }
            });
            mMediaPlayer.prepareAsync();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
Example 5
Source File: EditVideoActivity.java    From WeiXinRecordedDemo with MIT License 6 votes vote down vote up
private void initMediaPlay(SurfaceTexture surface){

        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.setSurface(new Surface(surface));
            mMediaPlayer.setLooping(true);
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mMediaPlayer.start();
                }
            });
            mMediaPlayer.prepareAsync();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
Example 6
Source File: CutTimeActivity.java    From WeiXinRecordedDemo with MIT License 6 votes vote down vote up
private void initMediaPlay(SurfaceTexture surface){

        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.setSurface(new Surface(surface));
            mMediaPlayer.setLooping(true);
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mMediaPlayer.start();
                }
            });
            mMediaPlayer.prepareAsync();
        }catch (Exception e){
            e.printStackTrace();
        }

        initVideoSize();
        initThumbs();
    }
 
Example 7
Source File: CutSizeActivity.java    From WeiXinRecordedDemo with MIT License 6 votes vote down vote up
private void initMediaPlay(SurfaceTexture surface){

        try {
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setDataSource(path);
            mMediaPlayer.setSurface(new Surface(surface));
            mMediaPlayer.setLooping(true);
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mMediaPlayer.start();
                }
            });
            mMediaPlayer.prepareAsync();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
 
Example 8
Source File: GuideFragment.java    From LefishGuide with Apache License 2.0 6 votes vote down vote up
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
    Surface surface = new Surface(surfaceTexture);
    try {
        int id = getArguments().getInt("Id");
        /**
         * mMediaPlayer = MediaPlayer.create(getContext(), R.raw.guide_one);
         * 创建实例是不行的,我也没有明白为什么
         */
        mMediaPlayer = new MediaPlayer();
        Uri video = Uri.parse("android.resource://" + getContext().getPackageName() + "/" + RES.get(id));
        mMediaPlayer.setDataSource(getContext(), video);

        mMediaPlayer.setSurface(surface);
        mMediaPlayer.prepare();
        mMediaPlayer.setLooping(false);
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setOnVideoSizeChangedListener(this);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.start();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example 9
Source File: CameraLiveWallpaper.java    From LiveWallPaper with Apache License 2.0 6 votes vote down vote up
/**
         * 播放
         */
        private void play(SurfaceHolder surfaceHolder,String videoPath){
            mediaPlayer = new MediaPlayer();
            // 设置多媒体流类型
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

            // 设置用于展示mediaPlayer的容器
            mediaPlayer.setSurface(surfaceHolder.getSurface());
//            mediaPlayer.setDisplay(surfaceHolder);//用这个方法会报 Wallpapers do not support keep screen on
            try {
                mediaPlayer.setDataSource(videoPath);
                mediaPlayer.prepare();
                mediaPlayer.start();
                mediaPlayer.setLooping(true);
            } catch (Exception e) {
                Log.i("通知", "播放过程中出现了错误哦");
            }
        }
 
Example 10
Source File: MovieView.java    From media-samples with Apache License 2.0 5 votes vote down vote up
void openVideo(Surface surface) {
    if (mVideoResourceId == 0) {
        return;
    }
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setSurface(surface);
    startVideo();
}
 
Example 11
Source File: VideoView.java    From Spectaculum with Apache License 2.0 5 votes vote down vote up
private void openVideo() {
    if (mUri == null || mInputSurfaceHolder == null) {
        // not ready for playback yet, will be called again later
        return;
    }

    release();

    try {
        mPlayer = new MediaPlayer();
        mPlayer.setSurface(getInputHolder().getSurface());
        mPlayer.setOnPreparedListener(mPreparedListener);
        mPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mPlayer.setOnSeekCompleteListener(mSeekCompleteListener);
        mPlayer.setOnCompletionListener(mCompletionListener);
        mPlayer.setOnErrorListener(mErrorListener);
        mPlayer.setOnInfoListener(mInfoListener);
        mPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        mPlayer.setDataSource(getContext(), mUri, mHeaders);
        mCurrentState = STATE_PREPARING;
        mPlayer.prepareAsync();
    } catch (Exception e) {
        Log.e(TAG, "video open failed", e);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
    }
}
 
Example 12
Source File: MovieView.java    From android-PictureInPicture with Apache License 2.0 5 votes vote down vote up
void openVideo(Surface surface) {
    if (mVideoResourceId == 0) {
        return;
    }
    mMediaPlayer = new MediaPlayer();
    mMediaPlayer.setSurface(surface);
    startVideo();
}
 
Example 13
Source File: OBVideoPlayer.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
public void prepareForPlaying(AssetFileDescriptor afd, long atTime, OBUtils.RunLambda seekCompletion)
{
    if(afd == null || activityPaused )
        return;

    playAfterPrepare = false;
    condition = playerLock.newCondition();
    fromTime = atTime;

    if(seekCompletion != null)
        this.seekCompletionBlock = seekCompletion;

    player = new MediaPlayer();
    player.setSurface(surface);
    //surface.release();
    player.setOnPreparedListener(this);
    player.setOnCompletionListener(this);
    player.setOnSeekCompleteListener(this);
    player.setOnErrorListener(this);
    player.setOnVideoSizeChangedListener(this);

    try
    {
        player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        player.prepareAsync();

    }
    catch (Exception e)
    {
        e.printStackTrace();
        return;
    }
}
 
Example 14
Source File: OBVideoPlayer.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
public void startPlayingAtTime(AssetFileDescriptor afd, long fr, MediaPlayer.OnPreparedListener preparedListener, MediaPlayer.OnCompletionListener completionListener)
{
    if(afd == null || activityPaused )
        return;

    condition = playerLock.newCondition();
    fromTime = fr;

    player = new MediaPlayer();
    player.setSurface(surface);
    //surface.release();
    player.setOnPreparedListener(preparedListener);
    player.setOnCompletionListener(completionListener);
    player.setOnSeekCompleteListener(this);
    player.setOnErrorListener(this);
    player.setOnVideoSizeChangedListener(this);
    try
    {
        player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        player.prepareAsync();

    }
    catch (Exception e)
    {
        e.printStackTrace();
        return;
    }
}
 
Example 15
Source File: PlaybackView.java    From justaline-android with Apache License 2.0 5 votes vote down vote up
private void prepareMediaPlayer(Uri uri) {
    Log.d(TAG, "prepareMediaPlayer: " + uri);
    try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnPreparedListener(this);
        mMediaPlayer.setDataSource(getContext(), uri);
        mMediaPlayer.setSurface(mSurface);
        mMediaPlayer.setAudioAttributes(mAudioAttributes);
        mMediaPlayer.setLooping(true);
        mMediaPlayer.prepare();
    } catch (IOException | IllegalStateException e) {
        Log.e(TAG, "prepareMediaPlayer: ", e);
    }
}
 
Example 16
Source File: FragmentShowImage.java    From iGap-Android with GNU Affero General Public License v3.0 4 votes vote down vote up
private void setMediaPlayer(MediaPlayer mMediaPlayer, final TextureView mTextureView, final ImageView imgPlay, final TouchImageView touchImageView) {

            if (mTextureView == null) {
                return;
            }
            Surface surfaceTexture = new Surface(mTextureView.getSurfaceTexture());

            if (surfaceTexture == null) {
                return;
            }

            try {
                if (mMediaPlayer != null) {
                    mMediaPlayer.setSurface(surfaceTexture);
                    mMediaPlayer.setLooping(false);
                    mMediaPlayer.prepareAsync();
                }
            } catch (IllegalStateException e) {
                e.getMessage();
            }


            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {

                    getRealSize(mp, mTextureView);
                    imgPlay.setVisibility(View.GONE);
                    mp.start();
                    MusicPlayer.pauseSound();
                    mTextureView.setVisibility(View.VISIBLE);
                    touchImageView.animate().setDuration(700).alpha(0F).start();

                    G.handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            touchImageView.setVisibility(View.GONE);
                            touchImageView.clearAnimation();
                        }
                    }, 700);

                    videoController.setEnabled(true);
                    videoController.show();
                }
            });

        }
 
Example 17
Source File: LocalPlayer.java    From V.FlyoutTest with MIT License 4 votes vote down vote up
public static final void setSurface(MediaPlayer player, Surface surface) {
    player.setSurface(surface);
}
 
Example 18
Source File: LocalPlayer.java    From media-samples with Apache License 2.0 4 votes vote down vote up
public static final void setSurface(MediaPlayer player, Surface surface) {
    player.setSurface(surface);
}
 
Example 19
Source File: TextureViewMediaRender.java    From android-jungle-mediaplayer with Apache License 2.0 4 votes vote down vote up
@Override
public void prepareMediaRender(MediaPlayer mediaPlayer) {
    mediaPlayer.setSurface(new Surface(mTextureView.getSurfaceTexture()));
}
 
Example 20
Source File: TextureViewMediaRender.java    From android-jungle-mediaplayer with Apache License 2.0 4 votes vote down vote up
@Override
public void mediaRenderChanged(MediaPlayer mediaPlayer) {
    mediaPlayer.setSurface(new Surface(mTextureView.getSurfaceTexture()));
}