第一行代码学习笔记第八章——运用手机多媒体
知識點目錄
- 8.1 將程序運行到手機上
- 8.2 使用通知
* 8.2.1 通知的基本使用
* 8.2.2 通知的進階技巧
* 8.2.3 通知的高級功能
- 8.3 調用攝像頭和相冊
* 8.3.1 調用攝像頭拍照
* 8.3.2 從相冊中選擇照片
- 8.4 播放多媒體文件
* 8.4.1 播放音頻
* 8.4.2 播放視頻
- 8.5 小結與點評
知識點回顧
8.1 將程序運行到手機上
前面的章節我們都是使用模擬器來運行程序的,但這章的Demo都需要在真正的Android手機上運行。
打開開發者選項:
從Android4.2系統開始,開發者選項是默認隱藏的,需要先進入到“關于手機”界面,然后對連續點擊版本號,直到讓開發者選項顯示出來。
手機連接電腦
打開開發者選項后,我們還需要通過數據線把手機連接到電腦上。然后進入到設置—>開發者選項界面,并在這個界面中勾選中USB調試選項。
如果是Windows操作系統,還需要在電腦上安裝手機驅動。一般借助360手機助手或豌豆莢等工具進行快速安裝,安裝完成后就可以看到手機已經連接到電腦上了:
8.2 使用通知
通知的主要的作用是:當應用程序不是在前臺運行時,向用戶發出一些提示信息。
發出一條通知后,手機最上方的狀態欄會顯示一個通知的圖標,下拉狀態欄后可以看到通知的詳細內容。
8.2.1 通知的基本使用
通知可以在活動、廣播接收器或服務里創建。但無論在哪創建使用步驟都是一樣的。
獲取NotificationManager來對通知進行管理
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
創建Notification對象
Notification notification = new NotificationCompat.Builder(this).setContentTitle("This is content title").setContentText("This is content text").setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)).build();幾乎每個Android版本都會對通知這部分進行修改,為了更好的兼容性,我們使用support庫中提供的兼容API。support-v4庫中提供了一個NotificationCompat類,使用這個類的構造器來創建Notification對象,就可以保證我們的程序在所有Android系統版本上都能正常工作。
-
setContentTitle()方法指定通知的標題內容
-
setContentText()方法指定通知的正文內容
-
setWhen()方法指定通知被創建的時間,以毫秒為單位,當下拉系統狀態欄時,這里指定的時間會顯示在相應的通知上
-
setSmallIcon()方法設置通知的小圖標,只能使用純alpha圖層的圖片進行設置,小圖標會顯示在系統狀態欄上
-
setLargeIcon()方法設置通知的大圖標,下拉系統狀態欄時,就可以看到設置的大圖標
調用NotificationManager的notify()方法
manager.notify(1,notification);參數一:id,要保證為每個通知所指定的id都是不同的
參數二:創建的Notification對象
示例代碼:
定義一個notification觸發點
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/send_notice"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAllCaps="false"android:text="Send notice"/></LinearLayout>創建通知
public class MainActivity extends AppCompatActivity implements View.OnClickListener {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button sendNotice = (Button) findViewById(R.id.send_notice);sendNotice.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.send_notice:String id = "my_channel_01";String name = "我是渠道名字";NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);Notification notification = null;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {//Android 8.0以上Toast.makeText(this, "Build.VERSION.SDK_INT = " + Build.VERSION.SDK_INT, Toast.LENGTH_SHORT).show();NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_DEFAULT);manager.createNotificationChannel(channel);notification = new NotificationCompat.Builder(this, id)//這里要傳一個id進去.setContentTitle("This is content title").setContentText("heheda").setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)).build();} else {//Android8.0以下Toast.makeText(this, "Build.VERSION.SDK_INT = " + Build.VERSION.SDK_INT, Toast.LENGTH_SHORT).show();notification = new NotificationCompat.Builder(this).setContentTitle("This is content title").setContentText("hahaha").setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)).build();}manager.notify(1, notification);break;default:break;}}}備注:在Android8.0以上發送通知的稍有不同,需要NotificationChannel。
PendingIntent
PendingIntent跟Intent有許多相似之處,都可以指明一個"意圖",都可以啟動活動、啟動服務以及發送廣播。不同的是Intent更加傾向于去立即執行某個動作,而PendingIntent更加傾向于在某個合適的時機去執行某個動作。所以,可以把PendingIntent理解為延遲執行的Intent
PendingIntent的基本用法:
根據具體的需求來選擇使用getActivity()方法、getBroadcast()方法或getService()方法。
Intent intent = new Intent(this, NotificationActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);getActivity()方法、getBroadcast()方法或getService()方法接收的參數都是相同的:
參數一:上下文
參數二:請求碼,一般傳入0即可
參數三:Intent對象
參數四:用于確定PendingIntent的行為。有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT這4個值可選,通常傳入0即可。
2.連綴在setContentIntent()方法中
notification = new NotificationCompat.Builder(this, id)//這里要傳一個id進去.setContentTitle("This is content title").setContentText("heheda").setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)).setContentIntent(pendingIntent) //連綴PendingIntent.build();這樣下拉系統狀態欄,點擊通知后,就會進入到NotificationActivity界面。
讓狀態欄上通知欄消失:
上面進入到NotificationActivity界面后,發現系統狀態欄上的通知欄圖標還沒有消失。
讓狀態欄上通知欄消失的方法有兩種:
-
在NotificationCompat.Builder中再連綴一個
notification = new NotificationCompat.Builder(this, id)//這里要傳一個id進去.setContentTitle("This is content title").setContentText("heheda").setWhen(System.currentTimeMillis()).setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)).setContentIntent(pendingIntent).setAutoCancel(true) //點擊這個通知時,通知會自動取消掉.build(); -
顯示地調用NotificationManager的cancel()方法
public class NotificationActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_notification);NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);manager.cancel(1);//這里的1,就是創建通知的時候給每條通知指定的id}}
8.2.2 通知的進階技巧
NotificationCompat.Builder中提供了非常豐富的API來讓我們創建出更加多樣的通知效果。下面我們學習一些常用的API。
setSound()
在通知發出的時候播放一段音頻。setSound()方法接收一個Uri參數,所以在指定音頻文件的時候還需要先獲取到音頻文件對應的URI。例如:每個手機的/system/media/audio/ringtones目錄下很多音頻文件,我們可以從中隨便選一個音頻文件,那么就可以在代碼中這樣指定;
Notification notification = new NotificationCompat.Builder(this, id)//這里要傳一個id進去.....setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg"))) //指定音頻.build();setVibrate()
用于設置手機靜止和振動的時長,以毫秒為單位。下標為0的值表示手機靜止的時長,下標為1的值表示手機振動的時長,下標為2的值又表示手機靜止的時長,以此類推。
Notification notification = new NotificationCompat.Builder(this, id)//這里要傳一個id進去.....setVibrate(new long[]{0,1000,1000,1000}) //指定振動時長.build();備注:控制手機振動需要聲明權限:
<uses-permission android:name="android.permission.VIBRATE"/>setLights()
用于控制在通知到來時,控制手機LED燈的顯示。
Notification notification = new NotificationCompat.Builder(this, id)//這里要傳一個id進去.....setLights(Color.GREEN,1000,1000).build();參數一:指定LED燈的顏色
參數二:指定LED燈亮起的時長,以毫秒為單位
參數三:指定LED燈暗去的時長,以毫秒為單位
setDefaults()
如果不想繁雜的設置,可以直接使用通知的默認效果,它會根據當前手機的環境來決定播放什么鈴聲,以及如何振動。
Notification notification = new NotificationCompat.Builder(this, id)//這里要傳一個id進去.....setDefaults(NotificationCompat.DEFAULT_ALL) //使用默認效果.build();8.2.3 通知的高級功能
NotificationCompat.Builder中還有更強大的API,可以構建出更加豐富的通知效果。
setStyle()
當通知欄中的內容足夠長的時候,正常情況下是無法完全顯示的,如下圖所示:
如果我們想讓通知欄中的內容完全顯示,可以使用setStyle()方法:
Notification notification = new NotificationCompat.Builder(this, id)//這里要傳一個id進去.....setStyle(new NotificationCompat.BigTextStyle().bigText("Learn how to build notification ,send and sync data,and use voice actions. Get the official" +"Andoird IDE and developer tools to build apps for Android.")).build();運行后的效果如下:
除了顯示長文字之外,通知里還可以顯示一張大圖片,具體用法如下:
Notification notification = new NotificationCompat.Builder(this, id)//這里要傳一個id進去.....setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.big_image))).build();運行后效果如下:
setPriority()
用于設置通知的重要程度。該方法接收一個整型參數用于設置這條通知的重要程度,共有5個指可選:
-
PRIORITY_DEFAULT 表示默認的重要程度
-
PRIORITY_MIN 表示最低的重要程度
-
PRIORITY_LOW 表示較低的重要程度
-
PRIORITY_HIGH 表示較高的重要程度
-
PRIORITY_MAX 表示最高的重要程度
具體用法如下:
Notification notification = new NotificationCompat.Builder(this, id)//這里要傳一個id進去.....setPriority(NotificationCompat.PRIORITY_MAX).build();8.3 調用攝像頭和相冊
調用系統的攝像頭進行拍照或者調用系統中的相冊都是很常見的功能需求。
8.3.1 調用攝像頭拍照
調用系統攝像頭拍照的步驟一般如下:
第一步:定義一個觸發點
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/take_photo"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Take Photo"android:textAllCaps="false"/><ImageViewandroid:id="@+id/picture"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"/></LinearLayout>布局中一個Button和一個ImageView。Button用于打開攝像頭進行拍照,ImageView用于將拍到的圖片顯示出來。
第二步:調用攝像頭的具體邏輯
public class MainActivity extends AppCompatActivity {private static final int TAKE_PHOTO = 1;private Button mTakePhoto;private Button mMTakePhoto;private ImageView mPicture;private Uri mImageUri;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mMTakePhoto = (Button) findViewById(R.id.take_photo);mPicture = (ImageView) findViewById(R.id.picture);mMTakePhoto.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//創建File對象,用于存儲拍照后的照片File outputImage = new File(getExternalCacheDir(), "output_image.jpg");try {if (outputImage.exists()) {outputImage.delete();}outputImage.createNewFile();} catch (IOException e) {e.printStackTrace();}//將創建的File對象轉換為Uri對象if (Build.VERSION.SDK_INT >= 24) {mImageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.cameraalbumtest.fileprovider", outputImage);} else {mImageUri = Uri.fromFile(outputImage);}//啟動相機程序Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);startActivityForResult(intent,TAKE_PHOTO);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {case TAKE_PHOTO:if (resultCode == RESULT_OK) {try {//將拍攝的照片顯示出來Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(mImageUri));mPicture.setImageBitmap(bitmap);} catch (FileNotFoundException e) {e.printStackTrace();}}break;}} }對上面的代碼分四大塊進行解釋:
1. 創建存放拍下圖片的路徑
創建一個File對象,用于存放攝像頭拍下的圖片,并命名為output_image.jpg,并將它存放在手機SD卡的應用關聯緩存目錄下。
應用關聯緩存目錄是指SD卡中專門用于存放當前應用緩存數據的位置,調用getExternalCacheDir()方法就可以得到這個目錄,具體路徑是/sdcard/Android/data/com.example.cameraalbumtest/cache ,如下圖所示:
為什么要是使用應用關聯緩存目錄呢?因為從Android6.0開始,讀寫SD卡被列為危險權限,如果將圖片存放在SD卡的任何其他目錄,都需要進行運行是權限處理才行,而使用應用關聯目錄則可以逃過這一步。
2. 將創建的File對象轉換為Uri對象
在低于Android7.0時,就調用Uri的fromFile()方法將File對象轉換為Uri對象。
在高于Android7.0時,就調用FileProvider的getUriForFile()方法將File對象轉換成一個封裝過的Uri對象。該方法共接收三個參數:
參數一:Context對象
參數二:authority字符串。需要跟后面再AndroidManifest.xml注冊時保持一致
參數三:創建的File對象
之所以要進行這樣一層轉換,因為從Android7.0開始,直接使用本地真實路徑的Uri被認為是不安全的,會拋出一個FileUriExposedException異常,而FileProvider則是一種特殊的內容提供器,它使用了和內容提供器類似的機制來對數據進行了保護,可以選擇性地將封裝過的Uri共享給外部,從而提高了應用的安全性。
這個Uri對象標識著output_image.jpg這張圖片的本地真實路徑。
3. 啟動相機程序
構建一個Intent對象,并將Intent的action指定為android.media.action.IMAGE_CAPTURE,再調用putExtra()方法指定圖片的輸出地址,最后調用startActivityForResult()來啟動活動。
這樣我是使用的隱式Intent,系統會找到能夠響應這個Intent的活動去啟動,這樣照相機程序就會被打開,拍下的照片將會輸出到output_image.jpg中
4. 將照片顯示出來
因為上面用的是startActivityForResult()方法,當照相機程序拍照完成后,就會返回到onActivityResult()方法中,如果發現拍照成功,就可以調用BitmapFactory的decodeStream()方法將output_image.jpg這張照片解析成Bitmap對象,然后把它設置到ImageView中顯示出來。
第三步:注冊內容提供器
上面用到了FileProvider,所以就需要到AndroidManifest.xml中去注冊:
<applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><providerandroid:authorities="com.example.cameraalbumtest.fileprovider"android:name="android.support.v4.content.FileProvider"android:exported="false"android:grantUriPermissions="true"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_paths"/></provider> </application>屬性解釋:
-
android:authorities 指定authorities,需要與FileProvider.getUriForFile()方法中的第二個參數一致
-
android:name 固定值。必須是android.support.v4.content.FileProvider
-
android:exported 表示是否允許外部程序訪問我們的內容提供器
-
android:grantUriPermissions 用于給內容提供器的數據子集授權。true表示權限能夠被授予內容提供器范圍內的任何數據;false表示權限只能授予這個元素所指定的數據子集。
-
指定Uri的共享路徑,并應用一個@xml/file_paths資源
第四步:創建file_paths資源
右擊res目錄—>New—>Directory,創建一個xml目錄,接著右擊xml目錄—>New—>File,創建一個file_paths.xml文件,然后修改file_paths.xml文件中的內容,如下所示:
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"><external-path name="my_images" path="."/> </paths>其中:external-path就是用來指定Uri共享的,name屬性的值可以隨便填寫,path屬性的值表示共享的具體路徑。這里的.就表示將整個SD卡進行共享,當然也可以僅共享我們存放output_image.jpg這張圖片的路徑。
第五步:添加權限
在Android4.4之前,訪問SD卡的應用關聯目錄也是要權限的,Android4.4之后的系統則不再需要權限聲明了,為了能夠兼容老版本,在AndroidManifest.xml中加入相應權限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>效果圖:
將程序運行到手機上,點擊Take Photo按鈕進行拍照,拍照完成后,點擊√,回到我們程序界面,拍照的照片就會顯示出來。如下圖所示:
8.3.2 從相冊中選擇照片
從相冊中選擇已經拍攝好的照片也是非常常見的功能。
實現從相冊中選擇照片的功能一般步驟如下:
第一步:新增一個觸發點
在前面CameraAlbumTest的基礎上,添加一個Button:
<Buttonandroid:id="@+id/choose_from_album"android:layout_width="match_parent"android:layout_height="wrap_content"android:textAllCaps="false"android:text="Choose From Album"/>Button用于打開系統相冊,然后再在ImageView上將選中的圖片顯示出來。
第二步:打開相冊的具體邏輯
public class MainActivity extends AppCompatActivity {private static final int TAKE_PHOTO = 1;private static final int CHOOSE_PHOTO = 2;private Button mMTakePhoto;private ImageView mPicture;private Uri mImageUri;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mMTakePhoto = (Button) findViewById(R.id.take_photo);mPicture = (ImageView) findViewById(R.id.picture);......//從相冊中選擇照片Button chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);chooseFromAlbum.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//權限申請if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);} else {openAlbum();}}});}private void openAlbum() {//打開相冊Intent intent = new Intent("android.intent.action.GET_CONTENT");intent.setType("image/*");startActivityForResult(intent, CHOOSE_PHOTO);}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {switch (requestCode) {case 1:if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {openAlbum();} else {Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();}break;}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {......case CHOOSE_PHOTO:if (resultCode == RESULT_OK) {//判斷手機系統的版本號if (Build.VERSION.SDK_INT >= 19) {handleImageOnKitKat(data);} else {handleImageBeforeKitKat(data);}}break;default:break;}}@TargetApi(19)private void handleImageOnKitKat(Intent data) {String imagePath = null;Uri uri = data.getData();if (DocumentsContract.isDocumentUri(this,uri)) {//如果是document類型的Uri,則通過document id處理String docId = DocumentsContract.getDocumentId(uri);if ("com.android.providers.media.documents".equals(uri.getAuthority())) {String id = docId.split(":")[1];//解析出數字格式的idString selection = MediaStore.Images.Media._ID + "=" + id;imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {Uri cententUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));imagePath = getImagePath(cententUri, null);}} else if ("content".equalsIgnoreCase(uri.getScheme())) {//如果是content類型的Uri,則使用普通方式處理imagePath = getImagePath(uri, null);} else if ("file".equalsIgnoreCase(uri.getScheme())) {//如果是file類型的Uri,直接獲取圖片路徑即可imagePath = uri.getPath();}displayImage(imagePath);}private void handleImageBeforeKitKat(Intent data) {Uri uri = data.getData();String imagePath = getImagePath(uri, null);displayImage(imagePath);}private String getImagePath(Uri uri, String selection) {String path = null;Cursor cursor = null;//通過Uri和selection來獲取真實的圖片路徑try {cursor = getContentResolver().query(uri, null, selection, null, null);if (cursor != null) {if (cursor.moveToFirst()) {path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));}}} catch (Exception e) {e.printStackTrace();} finally {if (cursor != null) {cursor.close();}}return path;}//將照片顯示出來private void displayImage(String imagePath) {if (imagePath != null) {Bitmap bitmap = BitmapFactory.decodeFile(imagePath);mPicture.setImageBitmap(bitmap);} else {Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show();}} }解釋說明:
-
需要動態申請WRITE_EXTERNAL_STORAGE這個危險權限
-
從Android4.4開始,選取相冊中的圖片不再返回圖片真實的Uri了,而是一個封裝過的Uri,所以在4.4版本以上的手機需要對這個Uri進行解析。返回的Uri分為document、content和file三種類型,需要從這三種情況去分別判斷解析。
效果圖:
重新運行程序,點擊Choose From Album按鈕,首先會彈出權限申請框,點擊允許后就會打開手機相冊,然后隨意選擇一張照片,回到我們程序的界面,選中的照片就會顯示出來,如下圖所示:
調用攝像頭拍照和從相冊中選擇照片的代碼我已上傳到我GitHub,有需要的朋友可以進入查看:
調用攝像頭拍照和從相冊中選擇照片功能實現
8.4 播放多媒體文件
Android在播放音頻和視頻方面提供了一套較為完整的API,使得開發者可以很輕松地編寫出一個簡易的音頻或視頻播放器。
8.4.1 播放音頻
Android中播放音頻文件一般都是使用MediaPlayer類來實現。
MediaPlayer中常用的控制方法如下:
下面我們在項目中實踐下:
第一步:編寫布局控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/play"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Play"android:textAllCaps="false"/><Buttonandroid:id="@+id/pause"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Pause"android:textAllCaps="false"/><Buttonandroid:id="@+id/stop"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Stop"android:textAllCaps="false"/></LinearLayout>布局中放置3個按鈕,分別用于對音頻文件的播放、暫停和停止操作。
第二步:音頻功能的具體實現
public class MainActivity extends AppCompatActivity implements View.OnClickListener {private static final String TAG = "MainActivity";private MediaPlayer mMediaPlayer = new MediaPlayer();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button play = (Button) findViewById(R.id.play);Button pause = (Button) findViewById(R.id.pause);Button stop = (Button) findViewById(R.id.stop);play.setOnClickListener(this);pause.setOnClickListener(this);stop.setOnClickListener(this);//申請權限if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);} else {initMediaPlayer();//初始化MediaPlayer}}private void initMediaPlayer() {try {File file = new File(Environment.getExternalStorageDirectory(),"audio.wma");mMediaPlayer.setDataSource(file.getPath());//指定音頻文件的路徑mMediaPlayer.prepare();//讓MediaPlayer進入到準備狀態} catch (IOException e) {e.printStackTrace();}}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {switch (requestCode) {case 1:if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {initMediaPlayer();} else {Toast.makeText(this, "拒絕權限將無法使用程序", Toast.LENGTH_SHORT).show();finish();}break;default:break;}}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.play:if (!mMediaPlayer.isPlaying()) {mMediaPlayer.start();//開始播放}break;case R.id.pause:if (mMediaPlayer.isPlaying()) {mMediaPlayer.pause();//暫停播放}break;case R.id.stop:if (mMediaPlayer.isPlaying()) {mMediaPlayer.stop();//停止播放}break;default:break;}}@Overrideprotected void onDestroy() {super.onDestroy();if (mMediaPlayer != null) {mMediaPlayer.stop();mMediaPlayer.release();}} }第三步:加入權限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>效果圖:
運行程序后,會先申請權限,同意授權后,就會出現如下界面:
點擊Play按鈕就會播放音頻。
點擊Pause按鈕,停止播放音頻,再次點擊Play按鈕,會接著暫停之前的位置繼續播放。
點擊Stop按鈕,停止播放音頻,但再次點擊Play按鈕時,音樂會從從頭開始播放。
8.4.2 播放視頻
Android中播放視頻文件一般都是使用VideoView類來實現。
VideoView中常用的控制方法如下:
下面我們在項目中實踐下:
第一步:編寫布局控件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><Buttonandroid:id="@+id/play"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:textAllCaps="false"android:text="Play"/><Buttonandroid:id="@+id/pause"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:textAllCaps="false"android:text="Pause"/><Buttonandroid:id="@+id/replay"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:textAllCaps="false"android:text="Replay"/></LinearLayout><VideoViewandroid:id="@+id/video_view"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>放置3個按鈕,分別用于控制視頻的播放、暫停和重新播放。下面放置一個VideoView,用于顯示要播放的視頻。
第二步:視頻功能的具體實現
public class MainActivity extends AppCompatActivity implements View.OnClickListener {private static final String TAG = "MainActivity";private VideoView mVideoView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mVideoView = (VideoView) findViewById(R.id.video_view);Button play = (Button) findViewById(R.id.play);Button pause = (Button) findViewById(R.id.pause);Button replay = (Button) findViewById(R.id.replay);play.setOnClickListener(this);pause.setOnClickListener(this);replay.setOnClickListener(this);if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);} else {initVideoPath();//初始化VideoView}}private void initVideoPath() {File file = new File(Environment.getExternalStorageDirectory(), "ad.mp4");mVideoView.setVideoPath(file.getPath());}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.play:if (!mVideoView.isPlaying()) {mVideoView.start(); //開始播放}break;case R.id.pause:if (mVideoView.isPlaying()) {mVideoView.pause(); //暫停播放}break;case R.id.replay:if (mVideoView.isPlaying()) {mVideoView.resume(); //重新播放}break;default:break;}}@Overridepublic void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {switch (requestCode) {case 1:if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {initVideoPath();} else {Toast.makeText(this, "拒絕權限將無法使用程序", Toast.LENGTH_SHORT).show();finish();}break;}}@Overrideprotected void onDestroy() {super.onDestroy();if (mVideoView != null) {mVideoView.suspend(); //將VideoView所占的資源釋放掉}} }第三步:加入權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>效果圖:
運行程序后,會先申請權限,同意授權后,點擊Play按鈕,就可以看到視頻已經開始播放了,如下圖所示:
點擊Pause按鈕可以暫停視頻的播放,點擊Replay按鈕可以從頭開始播放視頻。
VideoView只是幫我們做了一個很好的封裝,它的背后仍然使用MediaPlayer來對視頻文件進行控制。
VideoView并不是一個萬能的視頻播放工具類,它在視頻格式的支持以及播放效率方面都存在較大的不足。所以,如果想僅僅用VideoView編寫出一個功能非常強大的視頻播放器是不太現實的。比較適合于用于播放一些游戲類的片頭動畫或某個應用的宣傳視頻。
8.5 小結與點評
本章主要對Android系統的各種多媒體技術進行了學習,主要有:
-
通知的使用技巧
-
調用攝像頭拍照
-
從相冊中選取照片
-
播放音頻文件
-
播放視頻文件
非常感謝您的耐心閱讀,希望我的文章對您有幫助。歡迎點評、轉發或分享給您的朋友或技術群。
總結
以上是生活随笔為你收集整理的第一行代码学习笔记第八章——运用手机多媒体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一行代码学习笔记第七章——探究内容提供
- 下一篇: 第一行代码学习笔记第九章——使用网络技术