首頁 收藏 QQ群
 網(wǎng)站導航

ZNDS智能電視網(wǎng) 推薦當貝市場

TV應用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計算器

綜合交流 / 評測 / 活動區(qū)

交流區(qū) | 測硬件 | 網(wǎng)站活動 | Z幣中心

新手入門 / 進階 / 社區(qū)互助

新手 | 你問我答 | 免費刷機救磚 | ROM固件

查看: 12118|回復: 0
上一主題 下一主題
[教程]

第二十八講:Android多媒體(Media)入門

[復制鏈接]
跳轉到指定樓層
樓主
發(fā)表于 2013-8-28 16:19 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
1792                      這幾張圖片到res/drawable目錄下,并建立3個xml文件,拷貝love.mp3到res/raw文件中。
play.xml
   
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <selector xmlns:android="http://schemas.android.com/apk/res/android">   
            <item android:drawable="@drawable/play_disable" android:state_enabled="false"> <!-- state_enabled=false -->   
            <item android:drawable="@drawable/play_50"> <!-- default -->   
    </item></item></selector>
復制代碼
pause.xml   
   
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <selector xmlns:android="http://schemas.android.com/apk/res/android">   
            <item android:drawable="@drawable/pause_disable" android:state_enabled="false"> <!-- state_enabled=false -->   
            <item android:drawable="@drawable/pause_50"> <!-- default -->   
    </item></item></selector>
復制代碼
stop.xml   
   
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <selector xmlns:android="http://schemas.android.com/apk/res/android">   
            <item android:drawable="@drawable/stop_disable" android:state_enabled="false"> <!-- state_enabled=false -->   
            <item android:drawable="@drawable/stop_50"> <!-- default -->   
    </item></item></selector>
復制代碼
3、res/layout/main.xml 的內(nèi)容如下:   
   
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">   
            <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:textsize="25sp" android:text="簡單音樂播放器">   
    </textview></linearlayout>   
    <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent">   
       
                    <imagebutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="4dp" android:adjustviewbounds="true" android:id="@+id/play" android:background="@drawable/play">   
                    </imagebutton>   
       
                    <imagebutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="4dp" android:adjustviewbounds="true" android:id="@+id/pause" android:background="@drawable/pause">   
                    </imagebutton>   
       
                    <imagebutton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="4dp" android:adjustviewbounds="true" android:id="@+id/stop" android:background="@drawable/stop">   
                    </imagebutton>   
            </linearlayout>
復制代碼
4、MainMusic.java的內(nèi)容如下:   
   
   
  1. package android.basic.lesson28;   
       
    import java.io.IOException;   
       
    import android.app.Activity;   
    import android.media.MediaPlayer;   
    import android.media.MediaPlayer.OnCompletionListener;   
    import android.media.MediaPlayer.OnPreparedListener;   
    import android.os.Bundle;   
    import android.view.View;   
    import android.view.View.OnClickListener;   
    import android.widget.ImageButton;   
    import android.widget.Toast;   
       
    public class MainMusic extends Activity {   
       
            // 聲明變量   
            private ImageButton play, pause, stop;   
            private MediaPlayer mPlayer;   
       
            /** Called when the activity is first created. */   
            @Override   
            public void onCreate(Bundle savedInstanceState) {   
                    super.onCreate(savedInstanceState);   
                    setContentView(R.layout.main);   
       
                    // 定義UI組件   
                    play = (ImageButton) findViewById(R.id.play);   
                    pause = (ImageButton) findViewById(R.id.pause);   
                    stop = (ImageButton) findViewById(R.id.stop);   
       
                    // 按鈕先全部失效   
                    play.setEnabled(false);   
                    pause.setEnabled(false);   
                    stop.setEnabled(false);   
       
                    // 定義單擊監(jiān)聽器   
                    OnClickListener ocl = new View.OnClickListener() {   
       
                            @Override   
                            public void onClick(View v) {   
                                    switch (v.getId()) {   
                                    case R.id.play:   
                                            // 播放   
                                            Toast.makeText(MainMusic.this, "點擊播放", Toast.LENGTH_SHORT)   
                                                            .show();   
                                            play();   
                                            break;   
                                    case R.id.pause:   
                                            // 暫停   
                                            Toast.makeText(MainMusic.this, "暫停播放", Toast.LENGTH_SHORT)   
                                                            .show();   
                                            pause();   
                                            break;   
                                    case R.id.stop:   
                                            // 停止   
                                            Toast.makeText(MainMusic.this, "停止播放", Toast.LENGTH_SHORT)   
                                                            .show();   
                                            stop();   
                                            break;   
                                    }   
                            }   
                    };   
       
                    // 綁定單擊監(jiān)聽   
                    play.setOnClickListener(ocl);   
                    pause.setOnClickListener(ocl);   
                    stop.setOnClickListener(ocl);   
       
                    // 初始化   
                    initMediaPlayer();   
            }   
       
            // 初始化播放器   
            private void initMediaPlayer() {   
       
                    // 定義播放器   
                    mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.love);   
       
                    // 定義資源準備好的監(jiān)聽器   
                    mPlayer.setOnPreparedListener(new OnPreparedListener() {   
                            @Override   
                            public void onPrepared(MediaPlayer mp) {   
                                    // 資源準備好了再讓播放器按鈕有效   
                                    Toast.makeText(MainMusic.this, "onPrepared", Toast.LENGTH_SHORT)   
                                                    .show();   
                                    play.setEnabled(true);   
                            }   
                    });   
       
                    // 定義播放完成監(jiān)聽器   
                    mPlayer.setOnCompletionListener(new OnCompletionListener() {   
       
                            @Override   
                            public void onCompletion(MediaPlayer mp) {   
                                    Toast.makeText(MainMusic.this, "onCompletion",   
                                                    Toast.LENGTH_SHORT).show();   
                                    stop();   
                            }   
                    });   
            }   
       
            // 停止播放   
            private void stop() {   
                    mPlayer.stop();   
                    pause.setEnabled(false);   
                    stop.setEnabled(false);   
                    try {   
                            mPlayer.prepare();   
                            mPlayer.seekTo(0);   
                            play.setEnabled(true);   
                    } catch (IllegalStateException e) {   
                            e.printStackTrace();   
                    } catch (IOException e) {   
                            e.printStackTrace();   
                    }   
       
            }   
       
            // 播放   
            private void play() {   
       
                    mPlayer.start();   
                    play.setEnabled(false);   
                    pause.setEnabled(true);   
                    stop.setEnabled(true);   
            }   
       
            // 暫停   
            private void pause() {   
                    mPlayer.pause();   
                    play.setEnabled(true);   
                    pause.setEnabled(false);   
                    stop.setEnabled(true);   
            }   
       
            // Activity銷毀前停止播放   
            @Override   
            protected void onDestroy() {   
                    super.onDestroy();   
                    if (stop.isEnabled()) {   
                            stop();   
                    }   
       
            }   
       
    }
復制代碼
  5、運行程序,查看效果       
二、簡單視頻播放器Android為視頻播放提供了VideoView 和 MediaController 兩個現(xiàn)成的組件,讓我們可以方便的實現(xiàn)MP4、3GP等視頻的播放。下面我們通過一個例子來看一下:1、新建一個項目 Lesson28_Video2、使用 Format Factory 這個軟件壓縮一個視頻備用,我這里壓縮的參數(shù)如下:   
     
注意,如果播放時完全無法播放或者只有聲音沒有圖像,你就需要換壓縮軟件和調(diào)整壓縮參數(shù)重新壓縮視頻了,暫時只能這樣,我也是折騰了2-3小時都是黑屏,郁悶中(似乎得出一個答案,是否黑屏和機器設備的性能有關,我降低壓縮分辨率和每秒幀數(shù),出圖像音畫同步,如果提高每秒幀數(shù),聲音出來后十幾秒圖像才會出來,但是出來后音畫還是同步的,有興趣的朋友可以多測試測試給出一個結論)。用命令行的方式拷貝此視頻到存儲卡(sdcard)中,為什么不用eclipse中的可視化工具拷貝呢?因為那個方式靠大文件的時候經(jīng)常失敗,而命令行方式我沒拷貝失敗一次過。命令就是 adb push ,具體截個圖給你看:   
   
     
3、reslayoutmain.xml的內(nèi)容如下:   
  1. <?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" android:layout_gravity="top">   
    <videoview android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/VideoView01">   
    </videoview>   
    </linearlayout>
復制代碼
  4、MainVideo.java的內(nèi)容如下:   
  1. package android.basic.lesson28;   
       
    import android.app.Activity;   
    import android.net.Uri;   
    import android.os.Bundle;   
    import android.view.Window;   
    import android.view.WindowManager;   
    import android.widget.MediaController;   
    import android.widget.VideoView;   
       
    public class MainVideo extends Activity {   
            /** Called when the activity is first created. */   
            @Override   
            public void onCreate(Bundle savedInstanceState) {   
                    super.onCreate(savedInstanceState);   
                    //全屏   
                    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);   
                    //標題去掉   
                    this.requestWindowFeature(Window.FEATURE_NO_TITLE);   
                    //要在全屏等設置完畢后再加載布局   
                    setContentView(R.layout.main);   
       
                    //定義UI組件   
                    VideoView videoView = (VideoView) findViewById(R.id.VideoView01);   
                    //定義MediaController對象   
                    MediaController mediaController = new MediaController(this);   
                    //把MediaController對象綁定到VideoView上   
                    mediaController.setAnchorView(videoView);   
                    //設置VideoView的控制器是mediaController   
                    videoView.setMediaController(mediaController);   
       
                    //這兩種方法都可以 videoView.setVideoPath("file:///sdcard/love_480320.mp4");   
                    videoView.setVideoURI(Uri.parse("/sdcard/love_480320.mp4"));   
                    //啟動后就播放   
                    videoView.start();   
            }   
    }
復制代碼
  5、運行效果如下:   
       
   
三、簡單錄音程序1、新建一個一個項目 Tip_Recorder,主activity名字是  MainActivity2、其布局文件main.xml的代碼是:   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center">   
       
            <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="30sp" android:text="錄音" android:id="@+id/Button01"></button>   
            <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textsize="30sp" android:text="停止" android:id="@+id/Button02" android:layout_margintop="20dp"></button>   
    </linearlayout>
復制代碼
3、主程序文件 MainActivity.java的代碼如下:   
   
   
  1. package android.tip.yaoyao;   
       
    import java.io.File;   
    import java.io.IOException;   
    import java.util.Calendar;   
    import java.util.Locale;   
       
    import android.app.Activity;   
    import android.media.MediaRecorder;   
    import android.os.Bundle;   
    import android.text.format.DateFormat;   
    import android.view.View;   
    import android.widget.Button;   
    import android.widget.Toast;   
       
    public class MainActivity extends Activity {   
       
            private Button recordButton;   
            private Button stopButton;   
       
            private MediaRecorder mr;   
       
            @Override   
            public void onCreate(Bundle savedInstanceState) {   
                    super.onCreate(savedInstanceState);   
                    setContentView(R.layout.main);   
       
                    recordButton = (Button) this.findViewById(R.id.Button01);   
                    stopButton = (Button) this.findViewById(R.id.Button02);   
       
                    // 錄音按鈕點擊事件   
                    recordButton.setOnClickListener(new View.OnClickListener() {   
       
                            @Override   
                            public void onClick(View v) {   
       
                                    File file = new File("/sdcard/"   
                                                    + "YY"   
                                                    + new DateFormat().format("yyyyMMdd_hhmmss",   
                                                                    Calendar.getInstance(Locale.CHINA)) + ".amr");   
       
                                    Toast.makeText(getApplicationContext(), "正在錄音,錄音文件在"+file.getAbsolutePath(), Toast.LENGTH_LONG)   
                                                    .show();   
       
                                    // 創(chuàng)建錄音對象   
                                    mr = new MediaRecorder();   
       
                                    // 從麥克風源進行錄音   
                                    mr.setAudioSource(MediaRecorder.AudioSource.DEFAULT);   
       
                                    // 設置輸出格式   
                                    mr.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);   
       
                                    // 設置編碼格式   
                                    mr.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);   
       
                                    // 設置輸出文件   
                                    mr.setOutputFile(file.getAbsolutePath());   
       
                                    try {   
                                            // 創(chuàng)建文件   
                                            file.createNewFile();   
                                            // 準備錄制   
                                            mr.prepare();   
                                    } catch (IllegalStateException e) {   
                                            e.printStackTrace();   
                                    } catch (IOException e) {   
                                            e.printStackTrace();   
                                    }   
                                    // 開始錄制   
                                    mr.start();   
                                    recordButton.setText("錄音中……");   
                            }   
                    });   
       
                    // 停止按鈕點擊事件   
                    stopButton.setOnClickListener(new View.OnClickListener() {   
       
                            @Override   
                            public void onClick(View v) {   
       
                                    if (mr != null) {   
                                            mr.stop();   
                                            mr.release();   
                                            mr = null;   
                                            recordButton.setText("錄音");   
                                            Toast.makeText(getApplicationContext(), "錄音完畢", Toast.LENGTH_LONG).show();   
                                    }   
                            }   
                    });   
       
            }   
       
    }
復制代碼
4、因為錄音和寫存儲卡都需要權限聲明,所以這里也把AndroidManifest.xml代碼提供出來:   
   
   
  1. <?xml version="1.0" encoding="utf-8"?>   
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionname="1.0" android:versioncode="1" package="android.tip.yaoyao">   
        <application android:debuggable="true" android:label="@string/app_name" android:icon="@drawable/icon">   
            <activity android:label="@string/app_name" android:configchanges="orientation|keyboardHidden|keyboard" android:screenorientation="portrait" android:name=".MainActivity">   
                <intent -filter="">   
                    <action android:name="android.intent.action.MAIN">   
                    <category android:name="android.intent.category.LAUNCHER">   
                </category></action></intent>   
            </activity>   
       
        </application>   
        <uses -sdk="" android:minsdkversion="4">   
       
    <uses -permission="" android:name="android.permission.RECORD_AUDIO"></uses>   
    <uses -permission="" android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses>   
    </uses></manifest>
復制代碼
5、編譯并運行程序,查看結果。   
     
點擊錄音:   
     
錄音文件在存儲卡的根目錄幾個以YY開頭的amr文件   
     
   
6、這個例子要用到錄音設備,而模擬器并不能把電腦聲卡模擬出來使用,因此這個例子必須在真機上進行測試。 真機上測試方法也很簡單。在真機上把USB調(diào)試模式打開,把真機用USB線與電腦連接設置電腦和手機的連接方式為 ”僅充電“(此時手機可以操作存儲卡)打開Eclipse,在不選擇模擬器的情況下運行程序,此時,Eclipse會自動找到真機,并使用它運行程序,最完美的是他可以把真機運行程序的輸出信息,照樣輸出在Eclipse中的Logcat日志中。   
   
上面的真機截圖也是通過Eclipse的DDMS窗口直接抓取的,下圖中右上角顏色最深的圖標就是抓取真機截圖的按鈕:   
   
     
   
好了本講內(nèi)容就到這里,下次再見。   
   
   
   
   
   
   
   

上一篇:《Android學習指南》目錄
下一篇:第二十七講:Handler使用入門
您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

Archiver|新帖|標簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號 )

網(wǎng)絡信息服務信用承諾書 | 增值電信業(yè)務經(jīng)營許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號

GMT+8, 2024-10-21 07:40 , Processed in 0.067153 second(s), 14 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報:report#znds.com (請將#替換為@)

© 2007-2024 ZNDS.Com

快速回復 返回頂部 返回列表