Android第一行代碼讀書(shū)筆記 - 第八章

====================================

====== 第八章:豐富你的程序 — 運(yùn)用手機(jī)多媒體 ======

====================================

從Android4.2開(kāi)始,開(kāi)發(fā)者選型默認(rèn)是隱藏的,你需要先進(jìn)入“關(guān)于手機(jī)”界面,然后對(duì)著最下面的版本號(hào)那一欄連續(xù)點(diǎn)擊,才會(huì)讓開(kāi)發(fā)者選項(xiàng)顯示出來(lái)。

8.1 將程序運(yùn)行到真機(jī)上

1、將手機(jī)連接到電腦

2、進(jìn)入手機(jī)的設(shè)置 —》開(kāi)發(fā)者選項(xiàng),勾選USB調(diào)試選項(xiàng)(Android4.2開(kāi)始,開(kāi)發(fā)者選項(xiàng)默認(rèn)是隱藏的,需要先進(jìn)入到“關(guān)于手機(jī)”界面,然后對(duì)著最下面的版本號(hào)一欄連續(xù)點(diǎn)擊,才會(huì)讓開(kāi)發(fā)者選項(xiàng)顯示出來(lái))

3、觀察Android Monitor,發(fā)現(xiàn)當(dāng)前設(shè)備是有兩個(gè)設(shè)備在線的。

8.2 使用通知:

通知Notification是Android系統(tǒng)中比較有特色的功能。

8.2.1 通知的基本功能:

通知的使用方法比較靈活:既可以在活動(dòng)里面創(chuàng)建、也可以在廣播接收器里創(chuàng)建,當(dāng)然也可以在我們這一章的服務(wù)里創(chuàng)建。(在活動(dòng)里創(chuàng)建通知的場(chǎng)景比較少,因?yàn)橐话阍诔绦蜻M(jìn)入后臺(tái)的時(shí)候我們才需要使用通知)。

現(xiàn)在我們來(lái)學(xué)習(xí)一下創(chuàng)建通知的步驟:

1、需要一個(gè)NotificationManager對(duì)象:通過(guò)調(diào)用Context的getSystemService()方法獲取到。Context.NOTIFICATION_SERVICE作為參數(shù)。

如:

NotificationManager manager = (NotificationManger)getSystemService(Context.NOTIFICATION_SERVICE);

2、使用一個(gè)Builder構(gòu)造器來(lái)創(chuàng)建Notification對(duì)象。API不穩(wěn)定性在通知上突顯的特別明顯。解決辦法就是使用support庫(kù)中提供的兼容API,support-v4庫(kù)中提供了一個(gè)NotificationCompat類,使用這個(gè)類的構(gòu)造器來(lái)創(chuàng)建Notificaition對(duì)象,就可以保證所有系統(tǒng)都能用。

如:

Notificaition notification = new NotificationCompat.Builder(context).build();

上面的代碼只是創(chuàng)建了一個(gè)空的Notification對(duì)象。

3、一些基本的設(shè)置:

Notification notification = new NotificationCompat.Builder(context)

.setContentTitle(“This is content title”)

.setContextText(“This is content text”)

.setWhen(System.currentTimeMillis())

.setSmallIcon(R.drawale.small_icon);

.setLargeIcon(BitmapFactory.decodeResource(getResource().R.drawable.large_icon))

.build();

// 設(shè)置標(biāo)題、設(shè)置正文、指定通知被創(chuàng)建的時(shí)間、設(shè)置通知小圖標(biāo)、設(shè)置通知大圖標(biāo)

4、只需要調(diào)用NotificationManger的notify()方法,就可以讓通知顯示出來(lái)了。notify方法接收兩個(gè)參數(shù),第一個(gè)是id,要保證每個(gè)通知所指定的id都是不同的,第二個(gè)參數(shù)是Notification對(duì)象。

如:

manager.notify(1, notification);

新建一個(gè)NotificationTest項(xiàng)目:

1、修改activity_main.xml文件:

<LinearLayout xmlns:android=“http://schemaa.android.com/apk/res/andrdoid

android:orientation=“vertical”

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<Button

android:id=“@+id/send_notice”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“Send notice” />

</LinearLayout>

2、修改MainActivity代碼:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

@Override

protected void onCreate(Bundle savedInstnceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button sendNotice = (Button) findViewById(R.id.send_notice);

sendNotice.setOnClickListener(this);

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.send_notice:

NotificationManager manager = (NotificationManager) getSystemServdise(NOTIFICATION_SERVICE);

Notification notification = new NotificationCompat.Builder(this, “1”)

.setContentTitle(“This is content title”)

.setContentText(“This is content text”)

.setWhen(System.currentTimeMillis())

.setSmallIcon(R.mipmap.ic_lancher)

.setLargeIcon(BitmapFactory.decodeResource(getResources().R.mipmap.i c_launcher))

.build();

manager.notify(1, notification);

break;

default:

break;

}

}

}

但是這條通知是沒(méi)法點(diǎn)擊的。

需要新概念PendingIntent:Intent用于立即執(zhí)行某個(gè)動(dòng)作,PendingIntent更加傾向于在某個(gè)合適的時(shí)機(jī)去執(zhí)行某個(gè)動(dòng)作??梢岳斫鉃檠舆t執(zhí)行的Intent;

可以通過(guò)getActivity() getBroadcast() getService()方法之一來(lái)獲取PendingIntent。這三個(gè)方法都接受四個(gè)參數(shù),1、Context,2、傳0即可。3、是一個(gè)Intent對(duì)象。4、有FLAG_ONE_SHOT、FLAG_NO_CREATE、FLAG_CHANEL_CURRNT、FLAG_UPDATE_CURRENT四種值可選。

NotificationCompat.Builder可以再鏈接setContentIntent()方法。接收的參數(shù)正式PendingIntent對(duì)象。

優(yōu)化一下剛才的NotificationTest項(xiàng)目:

給剛才的通知加上點(diǎn)擊功能。

5、右鍵com.example.notification包,New —> Activity —> Empty Activity 新建NotificationActivity,名字叫做notification_layout

<RelativeLayout xmlns:andrdoi=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_centerInParent=“true”

android:textSize=“24sp”

android:text=“This is notification layout” />

</RelativeLayout>

6、再次修改MainActivity的代碼

public class MainActivity extends AppCompatActivity implements View.OnClickLinster {

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.send_notice:

Intent intent = new Intent(this, NotificationActivity.class);

PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

Notification notification = new NotificationCompat.build(this, “1”)

.setContentTitle(“This is content title”)

.setContentText(“This is content text”)

.setSmallIcon(…)

.setLargeIcon(…)

.setContentIntent(pi)

.build();

manager.notify(1, notification);

break;

default:

break;

}

}

}

6、在通知的時(shí)候再鏈接一個(gè)setAutoCancel()方法,或者再顯式的調(diào)用NotificationManager的cancel()方法,即可將它取消。

第一種方法:鏈接setAutoCancel():
Notification notification = new NotificationCompat.Builder(this).

.setContentTitle(“This is content title”)

.setAutoCancel(true)

.build();

可以看到,setAutoCancel()方法傳入true,就表示當(dāng)點(diǎn)擊了這個(gè)通知的時(shí)候,通知會(huì)自動(dòng)取消掉。

第二種方法寫法:

public class NotificationActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.notification_layout);

NotificationManger manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

manager.cancel(1);

}

}

cancel中傳入的1就是我們創(chuàng)建通知的時(shí)候指定的id。

8.2.2 通知的進(jìn)階技巧

實(shí)際上,NotificationCompat.Builder中提供了非常豐富的API來(lái)讓我們創(chuàng)建出更加多樣的通知效果。

1、先看看setSound()方法吧。它接收一個(gè)Uri參數(shù),如:

Notification notification = new NotificationCompat.Builder(this)

.setSound(Uri.fromFile(new File(“/system/media/audio/ringtones/Luna.ogg”);

.build();

2、setVibrate()方法可以設(shè)置手機(jī)振動(dòng)。它接收一個(gè)毫秒為單位的long類型的數(shù)組。

如new long[] {0, 1000, 1000, 1000}

// 數(shù)組0位表示手機(jī)靜止時(shí)長(zhǎng)

// 數(shù)組1位表示手機(jī)振動(dòng)時(shí)長(zhǎng)

// 數(shù)組2位表示手機(jī)靜止時(shí)長(zhǎng)

// 數(shù)組3位表示手機(jī)振動(dòng)時(shí)長(zhǎng)

控制手機(jī)振動(dòng),還需要聲明權(quán)限,因此需要編輯AndroidManifest.xml文件

<mainfest xmlns:android=“http://schemas.android.com/apk/res/android

package=“com.example.notificationtest”

android:versionCode=“1”

android:versionName=“1.0” >

<uses-permission android:name=“android.permission.VISIBRATE” />

</manifest>

3、setLights()方法可以設(shè)置LED燈閃爍。接收三個(gè)參數(shù),

參數(shù)1:LED燈顏色

參數(shù)2:LED燈亮起時(shí)長(zhǎng)(毫秒)

參數(shù)3:LED等暗去的時(shí)長(zhǎng)(毫秒)

.setLights(Color.GREEN, 1000, 1000)

如果不想做這么多負(fù)責(zé)設(shè)置,直接只用通知的默認(rèn)效果,讓手機(jī)根據(jù)環(huán)境來(lái)決定播放的鈴聲、振動(dòng):

.setDefaults(NotificationCompat.DEFAULT_ALL);

.build();

8.2.3 通知的高級(jí)功能

繼續(xù)觀察NotificationCompat.Builder這個(gè)類,會(huì)發(fā)現(xiàn)里面還有很多API是我們沒(méi)有使用過(guò)的。

1、setStyle()方法:構(gòu)建出富文本的通知內(nèi)容。接收一個(gè)NotificationCompat.Style參數(shù)。如:

Notification notification = new NotificationCompat.Builder(this)

.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResource(),R.drawable.big_image)))

,build();

2、setPriority()設(shè)置重要程度。5個(gè)可選值:PRIORITY_MIN、PRIORITY_LOW、PRIORITY_DEFAULT、PRIORITY_HIGH、PRIORITY_MAX

setPrority(NotificationCompat.PRIORITY_MAX);

8.3 調(diào)用攝像頭和相冊(cè)。

新建一個(gè)CameraAlbumTest項(xiàng)目

1、Button+ImageView

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

android:orientation=“vertical”

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<Button

android:id=“@+id/take_photo”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“Take Photo” />

<ImageView

android:id=“@+id/picture“

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“center_horizontal” />

</LinearLayout>

2、調(diào)用攝像頭邏輯:

public class MainActivity extends AppCompatActivity {

public static final int TAKE_PHOTO = 1;

private ImageView picture;

private Uri imageUri;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button takePhoto = (Button) findViewById(R.id.take_photo);

picture = (ImageView) findViewById(R.id.picture);

takePhoto.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// 創(chuàng)建File對(duì)象,用于存儲(chǔ)拍照后的圖片

File outputImage = new File(getExternalCacheDir(), “output_image.jpg”);

try {

if (outputImage.exists() {

outputImage.delete();

}

} catch (IOException e) {

e.printStackTrace();

}

if (Build.VERSION.SDK_INT >= 24) {

imageUri = FIleProvider.getUriForFile(MainActivity.this, “com.example.cameraalbumtest.fileprovider” outputImage);

} else {

imageUri = Uri.fromFile(outputImage);

}

// 啟動(dòng)相機(jī)程序

Intent intent = new Intent(“android.media.action.IMAGE_CAPTURE”);

Intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

startActivityForResult(intent, TAKE_PHOTO);

}

});

}

@Override

protected void onActivityResult(int requestCode, inte resultCode, Intent data) {

switch (requestCode) {

case TAKE_PHOTO:

if (resultCode == RESULT_OK) {

try {

// 將拍攝的照片顯示出來(lái)

Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri);

picture.setImageBitmap(bitmap);

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

break;

default:

break;

}

}

}

備注:

1、SD卡中專門用于存放當(dāng)前應(yīng)用緩存數(shù)據(jù)的位置,調(diào)用getExtranalCacheDir()方法可以得到這個(gè)目錄。

2、如果Android7.0以下,調(diào)用Uri的fromFile()方法將File對(duì)象轉(zhuǎn)換為Uri對(duì)象。如果是Android7.0以上,調(diào)用FileProvider的getUriForFile()方法將File對(duì)象轉(zhuǎn)換成一個(gè)封裝過(guò)的Uri對(duì)象。三個(gè)參數(shù),1Context 2任意字符串 3File對(duì)象。

3、構(gòu)建一個(gè)Intent,將action指定為android.media.action.IMAGE_CAPTURE,然后調(diào)用startActivityForResult()來(lái)啟動(dòng)活動(dòng)。

4、拍照完成后會(huì)返回onActivityResult()方法中

3、需要再AndroidManifest.xml中對(duì)內(nèi)容提供器進(jìn)行注冊(cè):

<manifest xmlns:android=“http://schemas.android.com/apk/res/android

package=“com.example.cameraalbumtest” >

<application

android:allowBackup=“true”

android:icon=“@mipmap/ic_launcher”

android:label=“@string/app_name”

android:supportsRtl=“true”

android:theme=“@style/AppTheme” >

<provider

android:name=“android.support.v4.content.FileProvider”

android:authorities=“com.example.cameraalbumtest.fileprovider”

android:exported=“false”

android:grantUriPermissions=“true” >

<meta-data

android:name=“android.support.FILE_PROVIDER_PATHS”

android:resource=“@xml/file_paths” />

</provider>

</aplication>

</manifest>

備注:

1、android:name的值是固定的。

2、android:authorities的值必須和剛才FileProvider.getUriForFIle()方法中的第二個(gè)參數(shù)字符串一致。

3、<meta-data>來(lái)指定Uri的共享路徑,并引用了一個(gè)@xml/file_paths的資源

新增這個(gè)file_path的xml文件:

右鍵res目錄,New —》 Dirctory,創(chuàng)建一個(gè)xml目錄,右鍵xml目錄創(chuàng)建一個(gè)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=“” />

</path>

其中,external-path就是用來(lái)指定Uri共享的。

4、還需要在AndroidMainifest.xml中聲明一下訪問(wèn)SD卡的權(quán)限:

<mainfest xmlns:android=“http://schmas.android.com/apk/res/android

package=“com.example.cameraalbumtest” >

<uses-permission android:name=“android.permission.WRITE_EXTERNAL_STORAGE” />

</manifest>

8.3.2 從相冊(cè)中選擇照片

從CameraAlbumTest項(xiàng)目基礎(chǔ)進(jìn)行修改。

1、修改activity_main.xml,增加一個(gè)按鈕

<Button

android:id=“@+id/choose_from_album”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“Choose From Album” />

2、修改MainAcitivity的代碼

public class MainActivity extends AppCompatActivity {

public static final int CHOOSE_PHOTO = 2;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button takePhoto = (Button) findViewById(R.id.choose_from_album);

Button chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);

chooseFromAlbum.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(VIew v) {

if (ContextCompat.checkSelfPermission(MainActivity.this, Mainifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRNTED) {

ActivityCompat.requestPermissions(MainActivity.this, new String[] {Mainifest.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); // 打開(kāi)相冊(cè)

}

@Override

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

switch (requestCode) {

case 1:

if (granteResult.length > 0 && grantResult[0] == PackageManager.PERMISSION.GRANTED) {

openAlbum();

} else {

Toast.makeText(this, “You denid the permission”, Toast.LENGTH_SHORT).show();

}

break;

default:

}

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch (requestCode) {

case CHOOSE_PHOTO:

if (requstCode == RESULT_OK) {

// 判斷手機(jī)系統(tǒng)版本號(hào)

if (Build.VERSION.SDK_INT >= 19) {

// 4.4及以上系統(tǒng)使用這個(gè)方法處理圖片

handleImageOnKitKat(data);

} else {

// 4.4以下系統(tǒng)使用這個(gè)方法處理圖片

handleImageBeforeKitKat(data);

}

}

break;

default:

break;

}

}

@TargetApi(19)

private void handleImageOnKitKat(Intent data) {

String imagePath = null;

Uri uri = data.getData();

if (DocumentContract.isDocumentUri(this, uri)) {

// 如果是document類型的uri,則通過(guò)document id處理

String docId = DocumentContract.getDocumentId(uri);

if (“com.android.providers.media.documents”.equals(uri.getAuthority())) {

String id = docId.split(“:”)[1];

String selection = MediaStore.Images.Media._ID + “=” + id;

imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);

} else if (“com.android.providers.downloads.document”.equals(uri.getAuthority())) {

Uri contentUri = ContentUri.withAppendedId(Uri.parse(“content://downloads/public_downloads”), Long.valueOf(docId));

} else if (“content”.equalsIgnoreCase(uri.getScheme())) {

// 如果是content類型的Uri,則使用普通方式處理

imagePath = getImagePath(uri, null);

} else if (“file”.eqaulsIgnoreCase(uri.getScheme())) {

// 如果是file類型的Uri,直接獲取圖片路徑即可

imagePath = uri.getPath();

}

displayImage(imagePath); // 根據(jù)圖片路徑顯示圖片

}

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;

// 通過(guò)Uri和selection來(lái)獲取真實(shí)的圖片路徑

Cursor cursor = getContentResolver().query(uri, null, selection, null, null);

if (cursor != null) {

if (cursor.moveToFirst()) {

path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)_;

}

cursor.close();

}

return path;

}

private void dispalyImage(String imagePath) {

if (imagePath != null ) {

Bitmap bitmap = BitmapFactory.decodeFile(imagePath);

picture.setImageBitmap(bitmap)

} else {

Toast.makeText(this, “failed to get image”, Toast.LENGTH_SHORT).show();

}

}

}

首先我們動(dòng)態(tài)申請(qǐng)WRITE_EXTERNAL_STORAGE這個(gè)危險(xiǎn)權(quán)限。因?yàn)橄鄡?cè)中的照片都是儲(chǔ)存在SD卡職工,我們要從SD卡中讀取照片就需要申請(qǐng)這個(gè)權(quán)限。WRITE_EXTERNAL_STORAGE表示同時(shí)賦予SD卡讀和寫的能力。

從android 4.4版本以上開(kāi)始,選取相冊(cè)中的圖片不再返回圖片的真實(shí)的Uri了,而是一個(gè)封裝過(guò)的Uri。

8.4 播放多媒體文件(聽(tīng)音樂(lè)和放電影)

8.4.1 播放音頻:

使用MediaPlayer類來(lái)實(shí)現(xiàn)。以下為常用的控制方法:

setDataSource() // 設(shè)置要播放的音頻文件的位置

prepare() // 在開(kāi)始播放前完成準(zhǔn)備工作

start() // 開(kāi)始播放

pause() // 暫停播放

reset() // 將MediaPlayer對(duì)象重置到剛剛創(chuàng)建的狀態(tài)

seekTo() // 從指定位置開(kāi)始播放音頻

stop() // 停止播放音頻,調(diào)用之后無(wú)法再次播放

release() // 釋放掉MediaPlayer對(duì)象相關(guān)的資源

isPlaying() // 判斷當(dāng)前MediaPlayer是否

getDutation() // 獲取載入的音頻文件的時(shí)長(zhǎng)

MediaPlayer的工作流程。首先需要?jiǎng)?chuàng)建出一個(gè)MediaPlayer對(duì)象,然后調(diào)用setDataSource()方法來(lái)設(shè)置音頻文件的路徑,再調(diào)用prepare()方法來(lái)設(shè)置音頻文件的路徑,再調(diào)用start()方法就可以播放音頻。pause方法就會(huì)暫停播放,調(diào)用reset()方法就會(huì)停止播放。

新建一個(gè)PlayAudioTest項(xiàng)目

1、修改activity_main.xml中的代碼

<LinearLayout xmlns:android=“http://schmas.android.com/apk/res/android

android:orientation=“vertical”

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<Button

android:id=“@+id/play”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

andrdoi:text=“Play” />

<Button

android:id=“@+id/pause”

android:layout_width=“match_parent”

andrdoi:layout_height=“wrap_content”

androdi:text=“Pause” />

<Button

android:id=“@+id/stop”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“Stop” />

</LinearLayout>

2、修改MainActivity的代碼

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private MediePlayer mediaPlayer = new MediaPlayer();

@Override 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(MainActivity.this, Mainifest.permission.WRITE_ETERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

// 去授權(quán),之后會(huì)調(diào)用到授權(quán)接口方法中

ActivityCompat.requestPermission(MainActivity.this, new String[] { Mainifest.permission,WRITE_EXTERNAL_STORAGE}, 1);

} else {

initMediaPlayer(); // 初始化MediaPlayer

}

}

private void initMediaPlayer() {

try {

FIle file = new FIle(Environment.getExternalStorageDirectioty(), music.mp3);

mediaPlayer.setDataSource(file.getPath()); // 指定音頻文件的路徑

mediaPlayer.prepare(); // 進(jìn)入準(zhǔn)備狀態(tài)

} catch (Exception e) {

e.printStactTrace();

}

}

// 獲取授權(quán)結(jié)果

@Override

public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantReults) {

switch(requestCode) {

case 1:

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

initMediaPlayer();

} else {

Toast.makeText(this, “拒絕權(quán)限將無(wú)法使用程序”, Toast.LENGH_SHORT).show();

finish();

}

break;

default:

}

}

@Override

public void onClick(View v) {

switch (v.getId()) {

case R.id.play:

if (!mediaPlayer.isPlaying()) {

mediaPlayer.start();

}

break;

case R.id.pause:

if (mediaPlayer.isPlaying()) {

mediaPlayer.pause();

}

break;

case R.id.stop:

if (mediaPlayer.isPlaying()) {

mediaPlayer.reset();

initMediaPlayer();

}

break;

default:

break;

}

}

@Override

protected void onDestroy() {

super.onDestroy();

if (mediaPlayer != null ) {

mediaPlayer.stop();

mediaPlayer.release();

}

}

}

動(dòng)態(tài)申請(qǐng)WRITE_EXTERNAL_STORAGE權(quán)限。這是因?yàn)槲覀冃枰赟D卡中放置一個(gè)音頻文件。

在onRequestPermissionsResult方法中,如果用戶拒絕了權(quán)限申請(qǐng),那么就調(diào)用finish方法將程序直接關(guān)掉。

3、在AndroidMainfest.xml中聲明用到的權(quán)限

<mainifest xmlns:nadroid=“http://schmas.android.com/apk/res/android

package=“com.example.palyaudiotest” >

<used-permission android:name=“android.permission.WRITE_EXTENAL_STORAGE” />

</mainifest>

8.4.2 播放視頻

播放視頻使用VideoView類來(lái)實(shí)現(xiàn),這個(gè)類將視頻的顯示與控制集于一身。與MediaPlayer類似,有以下常用方法。

setVideoPath() 設(shè)置要播放的視頻文件的位置

start() 開(kāi)始播放

pause() 暫停播放視頻

resume() 將視頻從頭開(kāi)始播放

seekTo() 從指定位置開(kāi)始播放

isPlaying() 判斷是否正在播放

getDuration() 獲取載入的視頻文件的時(shí)長(zhǎng)

現(xiàn)在創(chuàng)建一個(gè)PlayVideoTest項(xiàng)目

1、修改activity_main.xml中的代碼(放置了三個(gè)按鈕,并且底部放置了一個(gè)VideoView)

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

android:orientation=“vertical”

android:layout_width=“match_parent”

android:layout_height=“match_parent” >

<LinearLayout

android:layout_width=“match_parent”

android:layout_height=“wrap_content” >

<Button

android:id=“@+id/play”

android:layout_width=“0dp”

android:layout_height=“wrap_content”

andrdoi:layout_weight=“1”

android:text=“Play” />

<Button

android:id=“@+id/pause”

android:layout_width=“0dp”

android:layout_height=“wrap_content”

android:layout_weight=“1”

android:text=“Pause” />

<Button

android:id=“@+id/replay“

android:layout_width=“0dp”

android:layout_height=“wrap_content”

andrdoi:layout_weight=“1”

android:text=“Replay” />

</LinearLayout>

<VideoView

android:id=“@+id/video_view“

android:layout_width=“match_parent”

android:layout_height=“wrap_content” />

</LinearLayout>

2、修改MainActivity的代碼

public class MainActivity extends AppCompatActivity implements View.OnClickLister {

private VideoVIew videoVIew;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreat(savedInstanceState);

setContetnVIew(R.layout.activity_main);

videoView = (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, Mainfest.permission.WRITE_EXTENAL_STORAGE) != PackageManager.PERMISSION_GRNTED) {

ActivityCompat.requestPermissions(MainActivity.this, new String[] {Mainifest.permission.WRITE_EXTENAL_STORAGE }, 1);

} else {

initVidePath(); // 初始化

}

}

private void initVideoPath() {

File file = new File(Environment.getExternalStorageDirectory(), “movie.mp4”);

videoView.setVideoPath(file.getPath()); // 指定路徑

}

@Override

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {

switch (requestCode) {

case 1:

if (gran

….

}

@Override

protected void onDestroy() {

super.onDestroy();

if (videoView != null) {

videoVIew.suspend(); // 釋放資源

}

}

}

3、修改AndroidMainfest.xml文件權(quán)限聲明

<mainifest xmlns:android=“http://schemas.android.com/apk/res/android

package=“com.example.playvideotest” >

<uses-permission android:name=“android.permission.WRITE_EXTERNAL_STORAGE” />

</mainifest>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容