308-Video 播放器(行動裝置進階應用)

1. strings.xml

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

    <string name="hello">播放3gp影片</string>
    <string name="app_name">播放3gp影片</string>
    <string name="str_button1">播放</string>
    <string name="str_err_nosd">SD卡不存在!</string>

</resources>


2. color.xml

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

    <drawable name="darkgray">#808080</drawable>
    <drawable name="white">#FFFFFF</drawable>
    <drawable name="blue">#0000FF</drawable>

</resources> 


3. main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/white"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/myTextView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textColor="@drawable/blue" />

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="320px"
        android:layout_height="240px" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/startButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/str_button1" />
    </LinearLayout>

</LinearLayout>


4. GAD03.java

import android.app.Activity;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;
import android.os.Environment;

public class GAD03 extends Activity {

    private boolean sdCardExist;
    private VideoView videoView;
    private Button startButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        setContentView(R.layout.main);

        videoView = (VideoView) this.findViewById(R.id.videoView);
        startButton = (Button) this.findViewById(R.id.startButton);

        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            sdCardExist = true;
        } else {
            sdCardExist = false;
            Toast.makeText(this,
                    this.getResources().getString(R.string.str_err_nosd),
                    Toast.LENGTH_SHORT).show();
        }

        startButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                playVideo(Environment.getExternalStorageDirectory().toString()
                        + "/hello.3gp");
            }
        });

    }

    private void playVideo(String strPath) {

        try {
            /* 呼叫VideoURI方法,指定解析路徑 */
            videoView.setVideoURI(Uri.parse(strPath));
            /* 設定控制Bar顯示於此Context中 */
            videoView.setMediaController(new MediaController(this));
            videoView.requestFocus();
            /* 呼叫VideoView.start()自動播放 */
            videoView.start();
        } catch (Exception ex) {
            Toast.makeText(GAD03.this, ex.toString(), Toast.LENGTH_LONG).show();

        }
    }
}

沒有留言: