国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
第二十八講:Android多媒體(Media)入門 ? { Android學(xué)習(xí)指南 }

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

18Aug

本講內(nèi)容:Android中的音頻和視頻使用入門指南

Android 提供了 MediaPlayer 和 MediaRecorder 兩個(gè)工具類,來幫助開發(fā)者操作音頻和視頻。我們通過兩個(gè)小例子來學(xué)習(xí)一下多媒體資源的使用。

一、 簡(jiǎn)單音樂播放器

1、新建一個(gè)項(xiàng)目Lesson28_Music , 主Activity的名字是 MainMusic.java

2、拷貝

這幾張圖片到res/drawable目錄下,并建立3個(gè)xml文件,拷貝love.mp3到res/raw文件中。

play.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android=">
3     <item android:state_enabled="false" android:drawable="@drawable/play_disable"> <!-- state_enabled=false -->
4     <item android:drawable="@drawable/play_50"> <!-- default -->
5 </item></item></selector>

 

pause.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android=">
3     <item android:state_enabled="false" android:drawable="@drawable/pause_disable"> <!-- state_enabled=false -->
4     <item android:drawable="@drawable/pause_50"> <!-- default -->
5 </item></item></selector>

 

stop.xml

1 <?xml version="1.0" encoding="utf-8"?>
2 <selector xmlns:android=">
3     <item android:state_enabled="false" android:drawable="@drawable/stop_disable"> <!-- state_enabled=false -->
4     <item android:drawable="@drawable/stop_50"> <!-- default -->
5 </item></item></selector>

 

3、res/layout/main.xml 的內(nèi)容如下:

01 <?xml version="1.0" encoding="utf-8"?>
03     <textview android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="簡(jiǎn)單音樂播放器" android:textsize="25sp">
04 </textview>
06  
07         <imagebutton android:background="@drawable/play" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/play" android:adjustviewbounds="true" android:layout_margin="4dp">
08         </imagebutton>
09  
10         <imagebutton android:background="@drawable/pause" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/pause" android:adjustviewbounds="true" android:layout_margin="4dp">
11         </imagebutton>
12  
13         <imagebutton android:background="@drawable/stop" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/stop" android:adjustviewbounds="true" android:layout_margin="4dp">
14         </imagebutton>
15     </linearlayout>
16 </linearlayout>

 

4、MainMusic.java的內(nèi)容如下:

001 package android.basic.lesson28;
002  
003 import java.io.IOException;
004  
005 import android.app.Activity;
006 import android.media.MediaPlayer;
007 import android.media.MediaPlayer.OnCompletionListener;
008 import android.media.MediaPlayer.OnPreparedListener;
009 import android.os.Bundle;
010 import android.view.View;
011 import android.view.View.OnClickListener;
012 import android.widget.ImageButton;
013 import android.widget.Toast;
014  
015 public class MainMusic extends Activity {
016  
017     // 聲明變量
018     private ImageButton play, pause, stop;
019     private MediaPlayer mPlayer;
020  
021     /** Called when the activity is first created. */
022     @Override
023     public void onCreate(Bundle savedInstanceState) {
024         super.onCreate(savedInstanceState);
025         setContentView(R.layout.main);
026  
027         // 定義UI組件
028         play = (ImageButton) findViewById(R.id.play);
029         pause = (ImageButton) findViewById(R.id.pause);
030         stop = (ImageButton) findViewById(R.id.stop);
031  
032         // 按鈕先全部失效
033         play.setEnabled(false);
034         pause.setEnabled(false);
035         stop.setEnabled(false);
036  
037         // 定義單擊監(jiān)聽器
038         OnClickListener ocl = new View.OnClickListener() {
039  
040             @Override
041             public void onClick(View v) {
042                 switch (v.getId()) {
043                 case R.id.play:
044                     // 播放
045                     Toast.makeText(MainMusic.this, "點(diǎn)擊播放", Toast.LENGTH_SHORT)
046                             .show();
047                     play();
048                     break;
049                 case R.id.pause:
050                     // 暫停
051                     Toast.makeText(MainMusic.this, "暫停播放", Toast.LENGTH_SHORT)
052                             .show();
053                     pause();
054                     break;
055                 case R.id.stop:
056                     // 停止
057                     Toast.makeText(MainMusic.this, "停止播放", Toast.LENGTH_SHORT)
058                             .show();
059                     stop();
060                     break;
061                 }
062             }
063         };
064  
065         // 綁定單擊監(jiān)聽
066         play.setOnClickListener(ocl);
067         pause.setOnClickListener(ocl);
068         stop.setOnClickListener(ocl);
069  
070         // 初始化
071         initMediaPlayer();
072     }
073  
074     // 初始化播放器
075     private void initMediaPlayer() {
076  
077         // 定義播放器
078         mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.love);
079  
080         // 定義資源準(zhǔn)備好的監(jiān)聽器
081         mPlayer.setOnPreparedListener(new OnPreparedListener() {
082             @Override
083             public void onPrepared(MediaPlayer mp) {
084                 // 資源準(zhǔn)備好了再讓播放器按鈕有效
085                 Toast.makeText(MainMusic.this, "onPrepared", Toast.LENGTH_SHORT)
086                         .show();
087                 play.setEnabled(true);
088             }
089         });
090  
091         // 定義播放完成監(jiān)聽器
092         mPlayer.setOnCompletionListener(new OnCompletionListener() {
093  
094             @Override
095             public void onCompletion(MediaPlayer mp) {
096                 Toast.makeText(MainMusic.this, "onCompletion",
097                         Toast.LENGTH_SHORT).show();
098                 stop();
099             }
100         });
101     }
102  
103     // 停止播放
104     private void stop() {
105         mPlayer.stop();
106         pause.setEnabled(false);
107         stop.setEnabled(false);
108         try {
109             mPlayer.prepare();
110             mPlayer.seekTo(0);
111             play.setEnabled(true);
112         } catch (IllegalStateException e) {
113             e.printStackTrace();
114         } catch (IOException e) {
115             e.printStackTrace();
116         }
117  
118     }
119  
120     // 播放
121     private void play() {
122  
123         mPlayer.start();
124         play.setEnabled(false);
125         pause.setEnabled(true);
126         stop.setEnabled(true);
127     }
128  
129     // 暫停
130     private void pause() {
131         mPlayer.pause();
132         play.setEnabled(true);
133         pause.setEnabled(false);
134         stop.setEnabled(true);
135     }
136  
137     // Activity銷毀前停止播放
138     @Override
139     protected void onDestroy() {
140         super.onDestroy();
141         if (stop.isEnabled()) {
142             stop();
143         }
144  
145     }
146  
147 }

 

5、運(yùn)行程序,查看效果

二、簡(jiǎn)單視頻播放器

Android為視頻播放提供了VideoView 和 MediaController 兩個(gè)現(xiàn)成的組件,讓我們可以方便的實(shí)現(xiàn)MP4、3GP等視頻的播放。下面我們通過一個(gè)例子來看一下:

1、新建一個(gè)項(xiàng)目 Lesson28_Video

2、使用 Format Factory 這個(gè)軟件壓縮一個(gè)視頻備用,我這里壓縮的參數(shù)如下:

注意,如果播放時(shí)完全無法播放或者只有聲音沒有圖像,你就需要換壓縮軟件和調(diào)整壓縮參數(shù)重新壓縮視頻了,暫時(shí)只能這樣,我也是折騰了2-3小時(shí)都是黑屏,郁悶中(似乎得出一個(gè)答案,是否黑屏和機(jī)器設(shè)備的性能有關(guān),我降低壓縮分辨率和每秒幀數(shù),出圖像音畫同步,如果提高每秒幀數(shù),聲音出來后十幾秒圖像才會(huì)出來,但是出來后音畫還是同步的,有興趣的朋友可以多測(cè)試測(cè)試給出一個(gè)結(jié)論)。

用命令行的方式拷貝此視頻到存儲(chǔ)卡(sdcard)中,為什么不用eclipse中的可視化工具拷貝呢?因?yàn)槟莻€(gè)方式靠大文件的時(shí)候經(jīng)常失敗,而命令行方式我沒拷貝失敗一次過。命令就是 adb push ,具體截個(gè)圖給你看:

3、res\layout\main.xml的內(nèi)容如下:

1 <?xml version="1.0" encoding="utf-8"?>
3 <videoview android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/VideoView01">
4 </videoview>
5 </linearlayout>

 

4、MainVideo.java的內(nèi)容如下:

01 package android.basic.lesson28;
02  
03 import android.app.Activity;
04 import android.net.Uri;
05 import android.os.Bundle;
06 import android.view.Window;
07 import android.view.WindowManager;
08 import android.widget.MediaController;
09 import android.widget.VideoView;
10  
11 public class MainVideo extends Activity {
12     /** Called when the activity is first created. */
13     @Override
14     public void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         //全屏
17         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
18         //標(biāo)題去掉
19         this.requestWindowFeature(Window.FEATURE_NO_TITLE);
20         //要在全屏等設(shè)置完畢后再加載布局
21         setContentView(R.layout.main);
22  
23         //定義UI組件
24         VideoView videoView = (VideoView) findViewById(R.id.VideoView01);
25         //定義MediaController對(duì)象
26         MediaController mediaController = new MediaController(this);
27         //把MediaController對(duì)象綁定到VideoView上
28         mediaController.setAnchorView(videoView);
29         //設(shè)置VideoView的控制器是mediaController
30         videoView.setMediaController(mediaController);
31  
32         //這兩種方法都可以 videoView.setVideoPath("file:///sdcard/love_480320.mp4");
33         videoView.setVideoURI(Uri.parse("/sdcard/love_480320.mp4"));
34         //啟動(dòng)后就播放
35         videoView.start();
36     }
37 }

 

5、運(yùn)行效果如下:

好了本講內(nèi)容就到這里,下次再見。

 
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
android如何創(chuàng)建一個(gè)透明的ImageButton
Android Layout XML屬性研究
android 中ImageButton按下改變背景圖片的效果
第二十四講:Android動(dòng)畫入門(一) ? { Android學(xué)習(xí)指南 }
向android 的狀態(tài)欄中加入快捷按鈕(home,back,menu等等)的方法(續(xù)) ...
android 根據(jù)button不同狀態(tài)顯示不同(背景)圖片
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服