论坛首页 移动开发技术论坛

我的第一个Android 多媒体的Demo

浏览 12171 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-06-01  

作者:wuhua
;I Jj*SB(A'Tt3 空间:htt://wuhua.3geye.net Android J2ME $_ c+q"l5AH
转载请保留上面的信息(请尊重知识产品)谢谢Android J2ME 8l9Hb^ ]~u
Android J2ME "omG3`!GLtb

3^3k/a}SD3]3 package com.ucweb.player;Android J2ME d4~ i(k�j:Oz |

*Fu`%reo"m3 import java .io.IOException;
c%`~'WW&vM3 Android J2ME 5?-P1Jkc#V{
import android .app.Activity;Android J2ME #H)bYg2WgJ
import android.content.Context;
0lS nX4rU[3 import android.graphics.PixelFormat;
O(LaiSa3 import android.media.MediaPlayer;Android J2ME I5AWdt'u'oI
import android.media.MediaPlayer.OnBufferingUpdateListener;Android J2ME ,ED(kT)l'J"]
import android.media.MediaPlayer.OnCompletionListener;Android J2ME s*Zh;PKn)Fh}
import android.media.MediaPlayer.OnErrorListener;Android J2ME +g`&U9eCU
import android.os.Bundle;
Y6L7K2Z2a3 import android.util.Log;Android J2ME b(n+Fd0?2j)?E;[
import android.view.Menu;Android J2ME Z_G;t!W
import android.view.Surface;Android J2ME 3g*U4y8aSm(o
import android.view.SurfaceHolder;Android J2ME U p5sB|+zW;o
import android.view.SurfaceView;Android J2ME ,b;f/?3H5eP] W
import android.view.Window;
(?ac+qu-RP6j6\~3 import android.view.Menu.Item;
?._2]&{ i)G3
Kkn(x#C$DN DA3 public class VideoPlayer extends Activity implements OnErrorListener,
|Pc(L/n3                 OnBufferingUpdateListener, OnCompletionListenerAndroid J2ME m W}3D,t4[r
        {
l9mYK(GLc3         private static final String TAG = "PlayVideo";Android J2ME oV8z#@?fb)f�l9m
Android J2ME pl^ kR5_
        private static final int PLAY_VIDEO = Menu.FIRST + 1;Android J2ME *c(tKYvY#p g
        private static final int PLAY_MP3 = Menu.FIRST + 2;
oah1}8OPi3         private static final int STOP_PLAY_ID = Menu.FIRST + 4;
:vZ7y{mE3
]#[6H:e?3         private MediaPlayer mp;
M?qu-|g8s1hgf3
ut7~L,? q/r3         private Preview mPreview;
1{R ^{omnX3 Android J2ME T3PG7`1\l
        /** Called when the activity is first created. */
D)rFBr t1V!T f3         @Override
5|Md2dj(iJ^3         public void onCreate(Bundle icicle)Android J2ME yV ^;s9['V
        {
BnO(CC3                 super.onCreate(icicle);Android J2ME !G.v%t!xI0F
                Log.v(TAG, "onCreate: ===> ");Android J2ME "G6u d6v�s
                requestWindowFeature(Window.FEATURE_NO_TITLE);Android J2ME qC ~/d.fR
                getWindow().setFormat(PixelFormat.TRANSPARENT);
wXO3dZhF3
s6\j.F(TL8z3                 mPreview = new Preview(this);
;[ j?m(My3                 setContentView(mPreview);Android J2ME K*`sF]^
        }
T0X3licaj3
4W/HMF,g(y3         private void playVideo()
9@l�?~zFB.P6F3         {
KN s3C[FR3                 try
~R+b1Am y3                 {
9hP,E!q7\:c,LTy3                         SurfaceView view = new SurfaceView(this);
N(f6` x5T?1dW3                     Surface surface = view.getHolder().getSurface();
;iK W&B ej;h&mSp3                         setContentView(view);
}]N2I?&Hhc3
$rqB%S6g"g w)p5h!Z%Ne-R3                         Log.v(TAG, "height: " + mPreview.getHeight());
6gHPe'`w5pYU|3                         Log.v(TAG, "width: " + mPreview.getWidth());
fBR h T6M3                         mp = MediaPlayer.create(this, R.raw.mp3);Android J2ME 7UD#d2y9bzG
Android J2ME R3X8N,}0hn1H�PM W
                        mp.setOnErrorListener(this);
$`]HbT-k@(i%@5Oa3                         mp.setDisplay(mPreview.getHolder().getSurface());Android J2ME HO\8Fw-A
                        mp.prepare();
g |` OP7F8E3                         Log.v(TAG, "Duration:  ===>" + mp.getDuration());Android J2ME :v4gu`W d f&c
                        mp.start();Android J2ME |2kV2h w
                }
'p4|7q+}{V{3                 catch (Exception e)
4aF-`|,XsW np3                 {
4k9@Foxeb@�\3                         //e.printStackTrace();
~}_1e {&n!k3                         Log.e(TAG, "error: " + e.getMessage());
iRK-a9y3                         stopPlay();Android J2ME 5oj;SXCr
                }Android J2ME H$z.~i4]+f$\.n
        }Android J2ME M$[^#s+U:u

dPuh�O{3         private void playMp3() throws IOExceptionAndroid J2ME �L*Yv hc*N7Q
        {Android J2ME L@P7q&O
                mp = MediaPlayer.create(this, R.raw.mp3);
'Mn5aqP m"`3                 mp.prepare();
%M#~]m'BfO3                 Log.v(TAG, "Duration: ===>" + mp.getDuration());
o\YU ]NUH3                 mp.start();Android J2ME G"T.kRo�m5q/u7C+R,X
        }
*I[%zFL7u3 Android J2ME 4d J~5_8CyP(JT+Rp;k
        private void stopPlay() {Android J2ME -a9]9mc f|
                if (mp != null) {Android J2ME rw|$d IG"]!i1Ap(@
                        mp.stop();Android J2ME :~-q�lT(Gs A-Y
                        mp.release();Android J2ME a{;r:G@
                }Android J2ME F%H@'D9Zb
        }Android J2ME �g;{)\O B5C4`u
Android J2ME $?"z,Ww\E
        @Override
9zm�A,A�f:f3         public boolean onCreateOptionsMenu(Menu menu)Android J2ME :F*OM#N(yj
        {
E n1Zj1x"b-g9N T3                 boolean result = super.onCreateOptionsMenu(menu);Android J2ME ^ i6tOO8c
                menu.add(0, PLAY_MP3,"MP3");
${O.]L F$U a3                 menu.add(1, PLAY_VIDEO, "MP3");
c0aM!mv�u+`3~3                 menu.add(1, STOP_PLAY_ID, "MP3");Android J2ME %{ef)]FRMi
                return result;
7I:xU7X(R5Q(M3         }Android J2ME 9[d S9H`(K

9c%E Ta5R3         @OverrideAndroid J2ME `R4m}7S1un)V6\
        public boolean onOptionsItemSelected(Item item)Android J2ME QX@ Z T;lR
        {Android J2ME w{F|W e
                Log.v(TAG, "option: " + item.getId());
%NM z?7x#l$R"O/J3                 switch (item.getId()) {Android J2ME C q&?-\` F
                case PLAY_MP3:
,U:\T)?P2gB3                     try{
`-QH^S G3H2Z%k3                         playMp3();
2c!N'jJ | @d3                 }catch(Exception e){
a$YQ)M3\3                 }
|IwoW tB.r3                         break;Android J2ME z,e#@%k@
                case PLAY_VIDEO:
U)wq jP�i Y%R3                         playVideo();Android J2ME )jg"]w9\ M
                        break;
JDO`_\Q ??"E"e*}.N)V3                 case STOP_PLAY_ID:Android J2ME X+~,H,w;B }F3dG4}V
                        stopPlay();Android J2ME jM)T'PObG
                        break;
N#Bp,X7o�j9T8] T3                 }Android J2ME .a8Et1e-g H
                return super.onOptionsItemSelected(item);Android J2ME G I9r W@Z
        }Android J2ME v |n G6tN+t#lG

W \2CR e!K8X3         @Override
*O'n?u�@ Wjn3     protected boolean isFullscreenOpaque()Android J2ME `!jC8pT'X
        {Android J2ME -W3G0pXu&@;E1J
            // Our main window is set to translucent, but we know that weAndroid J2ME -C1KI*XA5s!Z@-q;}z
//will
O�` yc-\3             // fill it with opaque data. Tell the system that so it canAndroid J2ME d EN^?
//performAndroid J2ME Q*bYr2bw
            // some important optimizations.Android J2ME %u_#PBN-?bF TI
            return false;
l8V)W^GbM-~3         }Android J2ME [wm DC1l-u"U,N
Android J2ME rYV.I V xbO6ONd
        @Override
~n Q9k P3         protected void onResume() {
&^ R |fs;ZJ8{+[wjr3                 // Because the CameraDevice object is not a shared resource,
l5sfi }.sJ*u3                 // it's very important to release it when the activity is paused.Android J2ME #OLT~G-C+k
                super.onResume();
^d#@X}3                 //    mPreview.resume();Android J2ME 6e-_D M/m:rq
        }
1`e@YW3
!iHR.Y JVbK3         @Override
9{ p?hQ:\3b"uL3         protected void onPause() {Android J2ME lK(H-L$cc3V'Z4} f
                // Start Preview again when we resume.Android J2ME loe"C0G/B'? I(E)`
                super.onPause();
[RLq.s4} a3                 //    mPreview.pause();Android J2ME ZO N3i F }9],D
        }Android J2ME 3K T?a1wt7h
Android J2ME ]E5i�ga/`6Qn
        public void onError(MediaPlayer mediaPlayer, int what, int extra) {
Q6}n`&|3                 Log.e(TAG, "onError--->   what:" + what + "    extra:" + extra);Android J2ME g8}m#X*Pf
                if (mediaPlayer != null) {
Nui ^x2L s'x3                         mediaPlayer.stop();Android J2ME ? W�_ BON%N~
                        mediaPlayer.release();Android J2ME {l5B eAIh
                }
R!C'KE8m,M4`:U }3         }Android J2ME s3d"A1Bo%gWV
Android J2ME %C~8@#K1`HF0e7c
        public void onBufferingUpdate(MediaPlayer arg0, int percent) {Android J2ME 1}8vy#}1Bx P;t
                Log.d(TAG, "onBufferingUpdate--->   percent:" + percent);Android J2ME IlHvR:Id
        }Android J2ME ({!ba)O;bC�j
Android J2ME ?}.w2y�}'[l$^8Z
        public void onCompletion(MediaPlayer arg0) {Android J2ME L#F"W YPj\O$X
                Log.d(TAG, "onCompletion--->   finished");Android J2ME +f1CO9j*gg
        }Android J2ME 2a1n)Vr\(|O4sN N

5g$Ek1? ] a sr3 }Android J2ME L"\,X~Q

J;q0a;_0H5g-F3 class Preview extends SurfaceView implements SurfaceHolder.Callback {Android J2ME q,p�M,NNB2d9i%_jeW
        SurfaceHolder mHolder;Android J2ME $Jo4v,[3a7G
        private boolean mHasSurface;
F"iw+h*|!Nu2UoR3 Android J2ME ZD&k,j'm-qs|"K+F
        Preview(Context context) {Android J2ME tnQ,z i
                super(context);
OG rE}ia3
0n-@7y E/f5q-?3                 mHolder = getHolder();
z%sH |([`+F,c5r"f3                 mHolder.addCallback(this);
7_T f3kY\ CK3                 mHasSurface = false;
X+m[lVA?g3
S0?? iz;M.d Q3                 mHolder.setFixedSize(320, 240);Android J2ME e$K1\!B'nA
                //mHolder.setFixedSize(192, 242);
Q1f/@\|u�b/r3         }
/k8nC'pr3 Android J2ME tk1b|%mI4?9ep
        public void surfaceCreated(SurfaceHolder holder) {
g*tu(Nr@'A C$q3                 // The Surface has been created, start our main acquisition thread.
~*x%@+`%_"ls3                 mHasSurface = true;
AC3^ e"^;l;f c3                // return true;
3_#c2Qx5?3         }
9i Bn-d,e1mb3 Android J2ME [kB�s,O |Jo
        public void surfaceDestroyed(SurfaceHolder holder) {Android J2ME /Y"H�^,t~#hb
                // Surface will be destroyed when we return. Stop the preview.
?k/O4d)yP3t3                 mHasSurface = false;Android J2ME (H4H+P!` yZ:[3Rv ^
        }
lR H C7Bfb3 Android J2ME Sh$?nE*XR!W2F&w
        public void surfaceChanged(SurfaceHolder holder, int format, int w,
| XoI6sV2j6s3 int h) {Android J2ME -|V[|6qx,U
    // Surface size or format has changed. This should not happen inAndroid J2ME Uo'Xq9W2_ mW~d
//thisAndroid J2ME .d.DD%Z1iu
    // example.
Z&S `Z+Y,]}p8tn3 }}
YsZ6Sr+z3 Android J2ME e;oomc�@SWq[
现在对这个东西还进行摸索阶段不知道他有没有提供对流媒体的支持。如果可以直接支持流媒体的话。那真是太好了。这样就可以用java来写流媒体。 继续研究Android J2ME T?:^�ZE6N"}

   发表时间:2008-06-01  
支持一下, 貌似目前没有对流媒体的支持, 最大media文件size是1MB左右.
大家多摸索摸索...
0 请登录后投票
   发表时间:2008-06-02  
lordhong 写道
支持一下, 貌似目前没有对流媒体的支持, 最大media文件size是1MB左右.
大家多摸索摸索...

小弟我在研究这块东西,不过发现一直不成功。老出错。发现网络对这块也没有比较深入的研究,看了MideaPlayer,那些APIs也是少的可怜,比较简单。
0 请登录后投票
   发表时间:2008-06-06  
在下试验过了,对支持大小没有限制。
0 请登录后投票
   发表时间:2009-05-20  
模拟器(sdk1.1)不支持流媒体,但是真机G1上是可以的,继续研究。。。
0 请登录后投票
   发表时间:2009-05-21  
现在的限制取消了, 以前是SDK R13的限制.
真机上也不是流媒体, HTTP Progressive download而已...
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics