Android mediaplayer video size modification (screen size mediaPlayer =...

Source: Internet
Author: User

1. You can change the video size by changing the view size.

2. When initializing a video, the height and width of the video are all 0. Note that the view size must be modified after playback.

3. The layout class of sufaceview is android. widget. LinearLayout. LayoutParams. Note this

4. Add mediaPlayer. setDisplay (arg0) to the sufaceview surfaceChanged event. The setDisplay method can be called at any time.

 

01
Try {
02
Log. e ("m3u8", mediapath );
03
MediaPlayer = new MediaPlayer ();
04
// Start preparation
05
UiHandler. sendMessage (uiHandler
06
. ObtainMessage (MediaPlayerMessage. PREPARE ));
07
 
08
// Set the media path
09
MediaPlayer. setDataSource (mediapath );
10
// Sets the buffer for simultaneous playback.
11
MediaPlayer. setAudioStreamType (AudioManager. STREAM_MUSIC );
12
MediaPlayer. prepareAsync ();
13
} Catch (Exception e ){
14
// TODO: handle exception
15
Log. e ("meidaplayer err", e. toString ());
16
}
17
 
18
/**
19
* Player exception events
20
*/
21
MediaPlayer. setOnErrorListener (new OnErrorListener (){
22
 
23
@ Override
24
Public boolean onError (MediaPlayer mp, int what, int extra ){
25
// TODO Auto-generated method stub
26
MediaPlayer. release ();
27
Return false;
28
}
29
});
30
// The video can be played after preparation.
31
MediaPlayer. setOnPreparedListener (new OnPreparedListener (){
32
 
33
@ Override
34
Public void onPrepared (MediaPlayer mp ){
35
// TODO Auto-generated method stub
36
Try {
37
Mp. start ();
38
<Span style = "color: # e53333; "> // send a message to the ui interface. Here, the latency is set. If no latency is set, a file with a high width and Zero Width will appear. </span>
1
UiHandler. sendEmptyMessageDelayed (MediaPlayerMessage. SETSIZE, 1000 );
2
 
3
} Catch (Exception e ){
4
// TODO: handle exception
5
Log. e ("start mediaplayer", e. toString ());
6
}
7
 
8
}
9
});
View sourceprint?
01
<Pre class = "brush: java; toolbar: true; auto-links: false;"> case MediaPlayerMessage. SETSIZE:
02
Int mVideoWidth = mediaPlayerView. mediaPlayer. getVideoWidth ();
03
Int mVideoHeight = mediaPlayerView. mediaPlayer. getVideoHeight ();
04
Int width = _ frameLayout_main.getWidth ();
05
Int height = _ frameLayout_main.getHeight ();
06
<Span style = "color: # e53333;"> android. widget. LinearLayout. LayoutParams sufaceviewParams = (android. widget. LinearLayout. LayoutParams) mediapaly_sufaceview
07
. GetLayoutParams (); </span> if (mVideoWidth * height> width * mVideoHeight ){
08
// Log. I ("@", "image too tall, correcting ");
09
SufaceviewParams. height = width * mVideoHeight/mVideoWidth;
10
} Else if (mVideoWidth * height <width * mVideoHeight ){
11
// Log. I ("@", "image too wide, correcting ");
12
SufaceviewParams. width = height * mVideoWidth/mVideoHeight;
13
} Else {
14
SufaceviewParams. height = height;
15
SufaceviewParams. width = width;
16
}
17
SufaceviewParams. gravity = Gravity. CENTER;
18
 
19
Mediapaly_sufaceview.setLayoutParams (sufaceviewParams );
20
Break; </pre> <p> </p>
21
<Span style = "color: # e53333;"> // event after surface size </span> @ Override
22
Public void surfaceChanged (SurfaceHolder arg0, int arg1, int arg2, int arg3 ){
23
// TODO Auto-generated method stub www.2cto.com
24
Log. e ("Surface h w", String. valueOf (arg2) + "" + String. valueOf (arg3 ));
25
If (mediaPlayer! = Null ){
26
<Span style = "color: # e53333;"> mediaPlayer. setDisplay (arg0); </span>}
27
 
28
} <Br>


Author: tfc

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.