我的第一個Android 多媒體的Demo

來源:互聯網
上載者:User
作者:wuhua
空間:htt://wuhua.3geye.net
轉載請保留上面的資訊(請尊重知識產品)謝謝

package com.ucweb.player;

import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.Menu.Item;

public class VideoPlayer extends Activity implements OnErrorListener,
                OnBufferingUpdateListener, OnCompletionListener
        {
        private static final String TAG = "PlayVideo";

        private static final int PLAY_VIDEO = Menu.FIRST + 1;
        private static final int PLAY_MP3 = Menu.FIRST + 2;
        private static final int STOP_PLAY_ID = Menu.FIRST + 4;

        private MediaPlayer mp;

        private Preview mPreview;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle icicle)
        {
                super.onCreate(icicle);
                Log.v(TAG, "onCreate: ===> ");
                requestWindowFeature(Window.FEATURE_NO_TITLE);
                getWindow().setFormat(PixelFormat.TRANSPARENT);

                mPreview = new Preview(this);
                setContentView(mPreview);
        }

        private void playVideo()
        {
                try
                {
                        SurfaceView view = new SurfaceView(this);
                    Surface surface = view.getHolder().getSurface();
                        setContentView(view);

                        Log.v(TAG, "height: " + mPreview.getHeight());
                        Log.v(TAG, "width: " + mPreview.getWidth());
                        mp = MediaPlayer.create(this, R.raw.mp3);

                        mp.setOnErrorListener(this);
                        mp.setDisplay(mPreview.getHolder().getSurface());
                        mp.prepare();
                        Log.v(TAG, "Duration:  ===>" + mp.getDuration());
                        mp.start();
                }
                catch (Exception e)
                {
                        //e.printStackTrace();
                        Log.e(TAG, "error: " + e.getMessage());
                        stopPlay();
                }
        }

        private void playMp3() throws IOException
        {
                mp = MediaPlayer.create(this, R.raw.mp3);
                mp.prepare();
                Log.v(TAG, "Duration: ===>" + mp.getDuration());
                mp.start();
        }

        private void stopPlay() {
                if (mp != null) {
                        mp.stop();
                        mp.release();
                }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
                boolean result = super.onCreateOptionsMenu(menu);
                menu.add(0, PLAY_MP3,"MP3");
                menu.add(1, PLAY_VIDEO, "MP3");
                menu.add(1, STOP_PLAY_ID, "MP3");
                return result;
        }

        @Override
        public boolean onOptionsItemSelected(Item item)
        {
                Log.v(TAG, "option: " + item.getId());
                switch (item.getId()) {
                case PLAY_MP3:
                    try{
                        playMp3();
                }catch(Exception e){
                }
                        break;
                case PLAY_VIDEO:
                        playVideo();
                        break;
                case STOP_PLAY_ID:
                        stopPlay();
                        break;
                }
                return super.onOptionsItemSelected(item);
        }

        @Override
    protected boolean isFullscreenOpaque()
        {
            // Our main window is set to translucent, but we know that we
//will
            // fill it with opaque data. Tell the system that so it can
//perform
            // some important optimizations.
            return false;
        }

        @Override
        protected void onResume() {
                // Because the CameraDevice object is not a shared resource,
                // it's very important to release it when the activity is paused.
                super.onResume();
                //    mPreview.resume();
        }

        @Override
        protected void onPause() {
                // Start Preview again when we resume.
                super.onPause();
                //    mPreview.pause();
        }

        public void onError(MediaPlayer mediaPlayer, int what, int extra) {
                Log.e(TAG, "onError--->   what:" + what + "    extra:" + extra);
                if (mediaPlayer != null) {
                        mediaPlayer.stop();
                        mediaPlayer.release();
                }
        }

        public void onBufferingUpdate(MediaPlayer arg0, int percent) {
                Log.d(TAG, "onBufferingUpdate--->   percent:" + percent);
        }

        public void onCompletion(MediaPlayer arg0) {
                Log.d(TAG, "onCompletion--->   finished");
        }

}

class Preview extends SurfaceView implements SurfaceHolder.Callback {
        SurfaceHolder mHolder;
        private boolean mHasSurface;

        Preview(Context context) {
                super(context);

                mHolder = getHolder();
                mHolder.addCallback(this);
                mHasSurface = false;

                mHolder.setFixedSize(320, 240);
                //mHolder.setFixedSize(192, 242);
        }

        public void surfaceCreated(SurfaceHolder holder) {
                // The Surface has been created, start our main acquisition thread.
                mHasSurface = true;
               // return true;
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
                // Surface will be destroyed when we return. Stop the preview.
                mHasSurface = false;
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int w,
int h) {
    // Surface size or format has changed. This should not happen in
//this
    // example.
}}

現在對這個東西還進行摸索階段不知道他有沒有提供對流媒體的支援。如果可以直接支援流媒體的話。那真是太好了。這樣就可以用java來寫流媒體。 繼續研究

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.