Android學習(六)Android 多媒體
Android 多媒體應用
手機更新換代越來越快,衍生出了很多多媒體技術。持續更新一些比較常用的多媒體技術,供大家參考。
通知(Notification)
這個功能大家應該都體驗過,app經常彈出通知,可以進行簡單消息瀏覽和點擊跳轉的交互。
基本用法
要注意通知的兼容性,不然同樣代碼可能會不兼容不同版本。其中高版本版本要求,羅列如下:
- Android 9.0
不顯示問題跳轉。
createNotificationChannel()方法必須填寫。
- Android 8.0
要求必須指定通道ID,setChannelId()方法必須填寫。
簡單代碼如下:
使用NotificationCompat創建通知需要添加依賴:
implementation 'com.android.support:support-v4:26.1.0'
佈局文件代碼
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.testapplication.FifthActivity">
<Button
android:id="@+id/start_normal_notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="簡易通知"/>
</android.support.constraint.ConstraintLayout>
activity代碼如下
package com.example.testapplication;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class FifthActivity extends AppCompatActivity implements View.OnClickListener {
private NotificationManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fifth);
initView();
}
private void initView() {
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Button button = findViewById(R.id.start_normal_notification);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start_normal_notification:
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("test title")
.setContentText("test text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher))
.setChannelId(getPackageName()) //8.0 必須填寫
.setContentIntent(pendingIntent) //點擊事件的交互意圖
.build();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
getPackageName(),
"會話消息",
NotificationManager.IMPORTANCE_DEFAULT
);
manager.createNotificationChannel(channel); ////9.0後必須指定
}
manager.notify(1, notification);
break;
default:
break;
}
}
}
高級用法
- 通知時播放音樂:setSound()
- 通知時手機震動:setsetVibrate()
- 通知時手機前置LED燈閃爍:setLights()
- 通知時使用默認效果:setDefault()
音頻播放(MediaPlayer)
現在有很多優秀的開源音頻播放庫。但是對於部分app來說只是簡單的播放音頻,完全可以用Android提供的MediaPlayer類來實現。
- MediaPlayer狀態圖
使用Mediaplayer時,需要嚴格按照官方提供的狀態圖執行。如果在錯誤的週期調用了錯誤的方法,就會導致app無聲音播放,甚至報錯崩潰。詳情可以查看。
2. 基本使用
可以看郭大神。我就不獻醜,如果後面有機會再獻。