Android 播放在线视频及通知

利用SurfaceView和MediaPlay进行播放在线视频,不做其他解释放代码:

界面:

<?xml version="1.0" encoding="utf-8"?>



<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

              android:orientation="vertical"

              android:layout_width="match_parent"

              android:layout_height="match_parent">

    <EditText android:id="@+id/ev"

              android:layout_width="fill_parent"

              android:layout_height="wrap_content"

              android:inputType=""

              android:text="http://192.168.0.102/test.3gp"

            />

    <Button android:id="@+id/playvedio"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="播放"/>







    <SurfaceView android:id="@+id/sv"

                 android:layout_height="fill_parent"

                 android:layout_width="fill_parent"/>

</LinearLayout>
Activity:
/**

 * User: Tom

 * Date: 13-5-14

 * Time: 下午9:00

 */

public class PlayOnlineActivity extends Activity {

    private SurfaceView sv;

    private EditText et;

    private Button btnPaly;

    MediaPlayer mediaPlayer = null;

    private SurfaceHolder holder;



    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.playonline);

        sv = (SurfaceView) findViewById(R.id.sv);

        et = (EditText) findViewById(R.id.ev);

        btnPaly = (Button) findViewById(R.id.playvedio);



        holder = sv.getHolder();

        //surface不自己进行维护自己缓存,而是等待屏幕渲染

        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);



        btnPaly.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                try {

                    if (mediaPlayer == null) {

                        String vedioPath = et.getText().toString().trim();



                        mediaPlayer = new MediaPlayer();

                        //设置循环播放

                        mediaPlayer.setLooping(true);

                        mediaPlayer.setDataSource(vedioPath);

                        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                        mediaPlayer.setDisplay(holder);



                        mediaPlayer.prepareAsync();

                        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                            @Override

                            public void onPrepared(MediaPlayer mp) {

                                btnPaly.setText("暂停");

                                mediaPlayer.start();

                            }

                        });

                    } else {

                        if (mediaPlayer.isPlaying()) {

                            btnPaly.setText("播放");

                            mediaPlayer.pause();



                        } else {

                            btnPaly.setText("暂停");

                            mediaPlayer.start();

                        }

                    }

                } catch (Exception e) {

                    e.printStackTrace();

                }



            }

        });



    }

}

通知的使用方法:

/**

 * User: Tom

 * Date: 13-5-14

 * Time: 下午8:19

 */

public class NotificationActivity extends Activity {



    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.notifcation_demo);





        Button btnNotification = (Button) findViewById(R.id.btn_notification);

        btnNotification.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                //1、得到Notification服务

                NotificationManager manager = (NotificationManager) NotificationActivity.this.getSystemService(NOTIFICATION_SERVICE);

                //2、进行实例化一个Notification

                Notification notification = new Notification(R.drawable.ic_launcher, "tickettext", System.currentTimeMillis());



                PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, new Intent(NotificationActivity.this, ListdemoActivity.class), 0);



                //3、定义Notification处理事件

                notification.setLatestEventInfo(NotificationActivity.this, "notification标题", "notification内容",

                        pendingIntent);



                //4、管理Notification

                manager.notify(0,notification);



            }

        });

    }

}

你可能感兴趣的:(android)