English 中文(简体)
简讯:媒体 视频比例
原标题:Android: MediaPlayer video aspect ratio issue

我有一个无法解决的问题。 我正在利用媒体宣传员的班子播放录像;然而,我总是在传播原来的比例。 这里提到了媒体参与者守则和xml守则。 我曾尝试过这么多事情,但仍然无法解决这个问题。 我需要保持原来的方面比率。 我已宣布,这项活动将在我安妮斯·马尼费斯的档案中充分放映。 在媒体参与者守则之后,我也在此附上了xml代码。 请通知我。

private class ErrorListener implements OnErrorListener {
    // FIXME: @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        String message = "Unknown error";
        switch (what) {
        case MediaPlayer.MEDIA_ERROR_UNKNOWN:
            message = "Unable to play media";
            showError(message);
            break;
        case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
            message = "Server failed";
            showError(message);
            break;
        case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
            message = "Invalid media";
            showError(message);
            break;
        }
        return false;
    }
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.videoview);
    Intent intent = getIntent();
    uri = intent.getData();
    new loadMovie().execute();
    mPreview = (SurfaceView) findViewById(R.id.videoview);
    mPreview.bringToFront();
    holder = mPreview.getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    mediaPlayer = new MediaPlayer();

}// onCreate Method Ends

private void playVideo() {
    doCleanUp();
    try {
        mediaPlayer.setDataSource(this, uri);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.setDisplay(holder);
    try {
        mediaPlayer.prepare();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    mediaPlayer.setOnErrorListener(new ErrorListener());
    mediaPlayer.setOnBufferingUpdateListener(this);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.setOnPreparedListener(this);
    mediaPlayer.setOnVideoSizeChangedListener(this);
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mediaController = new MediaController(this);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    mediaController.show();
    return false;
}

// --MediaPlayerControl
// methods----------------------------------------------------
public void start() {
    mediaPlayer.start();
}

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

public int getDuration() {
    return mediaPlayer.getDuration();
}

public int getCurrentPosition() {
    return mediaPlayer.getCurrentPosition();
}

public void seekTo(int i) {
    mediaPlayer.seekTo(i);
}

public boolean isPlaying() {
    return mediaPlayer.isPlaying();
}

public int getBufferPercentage() {
    return 0;
}

public boolean canPause() {
    return true;
}

public boolean canSeekBackward() {
    return true;
}

public boolean canSeekForward() {
    return true;
}

// End of MediaPlayer
// Controls--------------------------------------------------------
// ------------------------

public void onPrepared(MediaPlayer mediaPlayer) {
    mediaController.setMediaPlayer(this);
    mediaController.setAnchorView(findViewById(R.id.videoview));
    mIsVideoReadyToBePlayed = true;

    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }

    handler.post(new Runnable() {
        public void run() {
            mediaController.setEnabled(true);
            mediaController.show();
        }
    });
}

public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

public void surfaceCreated(SurfaceHolder holder) {
    // TODO Auto-generated method stub
    playVideo();
}

public void surfaceDestroyed(SurfaceHolder arg0) {

}

private void startVideoPlayback() {
    holder.setFixedSize(mVideoWidth, mVideoHeight);
    mediaPlayer.start();
}

public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
    if (width == 0 || height == 0) {
        return;
    }
    mIsVideoSizeKnown = true;
    mVideoWidth = width;
    mVideoHeight = height;
    if (mIsVideoReadyToBePlayed && mIsVideoSizeKnown) {
        startVideoPlayback();
    }

}

public void onCompletion(MediaPlayer arg0) {

}

public void onBufferingUpdate(MediaPlayer mp, int percent) {

}

private void releaseMediaPlayer() {
    mediaPlayer.release();
    mediaPlayer = null;
}

private void doCleanUp() {
    mVideoWidth = 0;
    mVideoHeight = 0;
    mIsVideoReadyToBePlayed = false;
    mIsVideoSizeKnown = false;
}

这里是《刑法》:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<SurfaceView android:id="@+id/videoview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
 </LinearLayout>

请允许我告诉我,我如何能够保持视频的那部分比例?

问题回答

第一,与大饥荒形成相对联系。 CENTER, 并附上您的表面观点。 这将确保录像集中在屏幕上。

另外,如果你利用一项活动,利用黑专题避免梯度

1. 在您的海陆观测中,设立了一个名单:

mMediaPlayer = new MediaPlayer();

............

MediaPlayer.OnVideoSizeChangedListener mOnVideoSizeChangedListener = new MediaPlayer.OnVideoSizeChangedListener() {

    @Override
    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

         setFitToFillAspectRatio(mp, width, height);

    }
};
mMediaPlayer.setOnVideoSizeChangedListener(mOnVideoSizeChangedListener);

............

最后,在座标上填充方法。 这将在地貌景观和海峡开展工作。

private void setFitToFillAspectRatio(MediaPlayer mp, int videoWidth, int videoHeight)
{
    if(mp != null)
    {       
        Integer screenWidth = ((Activity) mContext).getWindowManager().getDefaultDisplay().getWidth();
        Integer screenHeight = ((Activity) mContext).getWindowManager().getDefaultDisplay().getHeight();
        android.view.ViewGroup.LayoutParams videoParams = getLayoutParams();


        if (videoWidth > videoHeight)
        {
            videoParams.width = screenWidth;
            videoParams.height = screenWidth * videoHeight / videoWidth;
        }
        else
        {
            videoParams.width = screenHeight * videoWidth / videoHeight;
            videoParams.height = screenHeight;
        }


        setLayoutParams(videoParams);
    }
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签