[《第一行Android代码》读书笔记] 第8章 丰富你的程序——运用手机多媒体

第8章 丰富你的程序——运用手机多媒体

Android提供了一系列的API,使得我们可以在程序中调用很多手机的多媒体资源,从而编写更加丰富多彩的应用程序。

8.1 将程序运行在手机上

首先通过数据线将手机连接到电脑上,然后打开设置 -> 开发者选项界面,勾选USB调试选项。需要注意的是,Android4.2系统开始,开发者选项默认是隐藏的,需要先进入到"关于手机"界面,然后连续点击版本号栏目,就会让开发者选项显示出来。

8.2 使用通知

当应用程序想要向用户发出提示信息,而应用程序又不在前台运行时,可以通过通知来实现。

8.2.1 通知的基本用法

通知既可以在活动里创建(场景较少,因为一般只有程序进入后台时才需要使用通知),也可以在广播接收器里创建,还可以在服务里创建。

创建通知的详细步骤:

  1. 调用ContextgetSystemService()方法获得一个NotificationManager来对通知进行管理。getSystemService()传入一个字符串参数来确定获取系统的那个服务,这里传入Context.NOTIFIVATION_SERVICE即可,代码如下:

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
  2. 由于几乎每一个系统版本都会对通知这部分功能进行或多或少的修改,因此API不稳定性问题在通知上面显得尤为严重,因此可以使用supprot库中提供的兼容API。通过support-v4库中提供的一个NotificationCompact类的构造器来创建Notification对象,并且在最终的build()方法之前连缀任意多的设置方法来创建一个丰富的Notification对象。代码如下:

    Notification notification = new NotificationCompat.Builder(context).build();
    
    Notification notification = new NotificationCompat.Builder(context)
        .setContentTitle("This is content title")
        .setContentText("This is content text")
        .setWhen(System.currentTimeMillis())
        .setSmallIcon(R.drawable.small_icon)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.large_icon))
        .build();
    
  3. 接着,只需要调用NotificationManagernotifiy()方法就可以让通知显示出来。notifiy()方法接收两个参数,第一个参数是id,要保证为每个通知所指定的id都是不同的。第二个参数则是Notification对象。因此,代码如下:

     manager.notify(1, notification);
    

使用示例

下面通过具体例子演示。首先新建一个NotificationTest项目,并修改activity_main.xml的代码,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
            android:id="@+id/send_notice"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Send notice" />

</LinearLayout>

接下来修改MainActivity中的代码,如下所示:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice = 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) getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(this)
                    .setContentTitle("This is content title")
                    .setContentText("This is content text")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground))
                    .build();
                manager.notify(1, notification);
                break;
            default:
                break;
        }
    }
}

若想要实现通知的点击效果,还需要在代码中进行相应的设置,这就涉及了一个新的概念:PendingIntent

PendingIntentIntent的共同点与区别:

  • PendingIntentIntent一样都可以去指明某一个“意图”,都可以用于启动活动、启动服务以及发送广播等。
  • 但Intent更倾向于去立即执行某个动作,而PendingIntent更加倾向于在某个合适的时机去执行某个动作。因此,可以把PendingIntent简单地理解为延迟执行的Intent。

PendingIntent的使用方法:通过该类的静态方法获取实例,可以根据需求选择使用getActivity()方法、getBroadcast()方法、getService()方法。这些方法的参数都是相同的。第一个参数为Context,第二个参数一般用不到,通常传入0即可。第三个参数是Intent对象,我们可以通过这个对象构建出PendingIntent的“意图”。第四个参数用于确定PendingIntent的行为,通常情况传入0即可。

可以通过Notification.Builder再连缀一个setContentIntent()方法,把PendingIntent作为参数传入,就可以实现用户点击这条通知时执行相应的逻辑。

使用示例

新建一个活动NotificationActivity,布局起名为notification_layout。然后修改notification_layout.xml中的代码,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="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:text="This is notification layout"
              android:textSize="24sp" />
</RelativeLayout>

接下来修改MainActivity中的代码,给通知加入点击功能,如下所示:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button sendNotice = findViewById(R.id.send_notice);
        sendNotice.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_notice:
                /*使用Intent表达出我们想要启动Notificaton的"意图",然后传入*/
                Intent intent = new Intent(this, NotificationActivity.class);
                PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);

                NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notification = new NotificationCompat.Builder(this)
                    .setContentTitle("This is content title")
                    .setContentText("This is content text")
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.drawable.ic_launcher_foreground)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_foreground))
                    .setContentIntent(pi) //给通知加入点击功能
                    .build();
                manager.notify(1, notification);
                break;
        }
    }
}

此时当点击通知后,通知图标仍然显示在系统状态栏上,取消的方法有两种:

  • NotificationCompat.Builder中再连缀一个setAutoCancel(true)方法,

  • 显式地调用NotificationManagerCancel()方法,代码如下:

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.cancel(1);//这里的1是创建通知时指定的id,想要取消哪个通知就传入哪个通知的id
    
    

8.2.2 通知的进阶技巧

Notification.Builder中提供了非常丰富的API来让我们创建更加多样的通知效果。

(1)setSound()方法setSound()方法可以设置通知的音频,它接收一个音频对应的Uri参数。比如设置为/system/media/audio/ringtones目录下的Luna.ogg音频,代码如下:

Notification notification = new NotificationCompat.Builder(this)
    ...
    .setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna/ogg")))
    .build();

(2)setVibreate()方法。它接收一个长整型的数组,用于设置手机静止和振动的时长,以毫秒为单位。下标为0的值表示手机静止的时长,下标为1的值表示手机振动的时长,下标为2的值又表示手机静止的时长,以此类推。所以,如果想要让手机在通知到来的时候立刻振动1秒,然后静止1秒,再振动1秒,再振动1秒,代码可以写成:

Notification notification = new NotificationCompat.Builder(this)
    ...
    .setVibrate(new long[]{0,1000,1000,1000})
    .build();

但是,想要控制手机振动还需要声明权限。因此,我们还需要修改一下AndroidManifest.xml文件,加入如下声明:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.notificationtest">
    ...
    <uses-permission android:name="android.permission.VIBRATE"/>
    ...
</manifest>

(3)setLights()方法。当有未接电话或未读短信,而此时手机又处于锁屏状态时,LED灯就会不停地闪烁以提醒用户查看。它接收3个参数,第一个参数用于指定LED灯的颜色,第二个参数用于指定LED灯亮起的时长,以毫秒为单位,第三个参数用于指定LED灯暗去的时长,,也是以毫秒为单位。所以,当通知到来时,如果想实现LED灯以绿色的灯光一闪一闪的效果,就可以写成:

Notification notification = new NotificationCompat.Builder(this)
    ...
    .setLights(Color.GREEN,1000,1000)
    .build();

当然,也可以直接使用通知的默认效果,它会根据当前手机的环境来决定播放什么铃声,以及如何振动,写法如下:

Notification notification = new NotificationCompat.Builder(this)
    ...
    .setDefaults(NotificationCompat.DEFAULT_ALL)
    .build();

需要注意的是,以上这些进阶技巧都要在手机上运行才能看得到效果,模拟器无法表现出振动以及LED灯闪烁等功能。

8.2.3 通知的高级功能

(1)setStyle()方法。这个方法允许我们构建出富文本的通知内容。它接收一个NotificationCompat.Style参数,这个参数就是用来构建具体的富文本信息的,如长文字、图片等。

如果我们想要将通知内容设置成很长的文字,可以这样写:

Notification notification = new NotificationCompat.Builder(this)
    ...
    .setStyle(new NotificationCompat.BigTextStyle().bigText("文本内容"))
    .build();

如果我们要显示一张大图片,可以这样写:

Notification notification = new NotificationCompat.Builder(this)
    ...
    .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.big_image)))
    .build();

(2)setPriority()方法。它可以用于设置通知的重要程度。它接收一个整型参数用于设置这条通知的重要程度,

按照程度分级一共有5个常量值可选。具体写法如下:

Notification notification = new NotificationCompat.Builder(this)
    ...
    .setPriority(NotificationCompat.PRIORITY_MAX)
    .build();

8.3 调用摄像头和相册

8.3.1 调用摄像头拍照

使用示例

新建一个CameraAlbumTest项目,然后修改activity_main.xml中的代码,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              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>

然后开始编写调用摄像头的具体逻辑,修改MainActivity中的代码,如下所示:

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 = findViewById(R.id.take_photo);
        picture = findViewById(R.id.picture);
        takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建File对象,用于存储拍照后的图片
                //调用getExternalCacheDir()方法可以得到应用关联缓存目录。使用这个位置可以避免进行运行时权限处理。
                File outputImage = new File(getExternalCacheDir(), "output image.jpg");
                try {
                    if (outputImage.exists()) {
                        outputImage.delete();
                    }
                    outputImage.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (Build.VERSION.SDK_INT >= 24) {
                    //系统版本在Android7.0及以上时,调用FileProvider的getUriForFile()方法
                    //因为从Android7.0开始,直接使用本地真实路径的Uri被认为是不安全的,而FileProvider使用了和内容提供器类似的机制,提高了安全性
                    imageUri = FileProvider.getUriForFile(MainActivity.this,
                                                          "com.example.cameraalbumtest.fileprovider", outputImage);
                } else {
                    //系统版本低于Android7.0时,将File对象转为Uri对象,该对象标识这张图片的真实路径
                    imageUri = Uri.fromFile(outputImage);
                }
                //启动相机程序
                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, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case TAKE_PHOTO:
                //如果拍照成功
                if (requestCode == RESULT_OK) {
                    try {
                        //将拍摄的照片显示出来
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        picture.setImageBitmap(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }
}


还需要在AndroidManifest.xml中对内容提供器进行注册,同时为了兼容老版本系统的手机,加入了声明访问SD卡的权限,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.cameraalbumtest">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
       	...
        <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>
    </application>

</manifest>

8.3.2 从相册中选择照片

使用示例

CameraAlbumTest项目的基础上进行修改,编辑activity_main.xml文件,在布局中加一个按钮用于从相册中选择照片,代码如下所示:

<?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" >

  	...

    <Button
        android:id="@+id/choose_from_album"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Choose From Album" />

   	...

</LinearLayout>

然后修改MainActivity中的代码,加入从相册选择照片的逻辑,代码如下所示:

public class MainActivity extends AppCompatActivity {
    public static final int TAKE_PHOTO = 1;

    public static final int CHOOSE_PHOTO = 2;

    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);
        Button chooseFromAlbum = (Button) findViewById(R.id.choose_from_album);
        picture = (ImageView) findViewById(R.id.picture);
        takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public 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();
                }
                if (Build.VERSION.SDK_INT < 24) {
                    imageUri = Uri.fromFile(outputImage);
                } else {
                    imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.cameraalbumtest.fileprovider", outputImage);
                }
                // 启动相机程序
                Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(intent, TAKE_PHOTO);
            }
        });
        chooseFromAlbum.setOnClickListener(new View.OnClickListener() {
            @Override
            public 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); // 打开相册
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, 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;
            default:
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case TAKE_PHOTO:
                if (resultCode == RESULT_OK) {
                    try {
                        // 将拍摄的照片显示出来
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
                        picture.setImageBitmap(bitmap);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
            case CHOOSE_PHOTO:
                if (resultCode == RESULT_OK) {
                    // 判断手机系统版本号
                    if (Build.VERSION.SDK_INT >= 19) {
                        // 4.4及以上系统使用这个方法处理图片
                        handleImageOnKitKat(data);
                    } else {
                        // 4.4以下系统使用这个方法处理图片
                        handleImageBeforeKitKat(data);
                    }
                }
                break;
            default:
                break;
        }
    }

    @TargetApi(19)
    private void handleImageOnKitKat(Intent data) {
        String imagePath = null;
        Uri uri = data.getData();
        Log.d("TAG", "handleImageOnKitKat: uri is " + uri);
        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]; // 解析出数字格式的id
                String 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 contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
                imagePath = getImagePath(contentUri, 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;
        // 通过Uri和selection来获取真实的图片路径
        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 displayImage(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();
        }
    }

}


8.4 播放多媒体文件

8.4.1 播放音频

在Android中播放音频文件一般都是使用MediaPlayer类来实现的,下表列出了MediaPlayer类中的一些较为常用的控制方法。

方法名 功能描述
setDataSource() 设置要播放的音频文件的位置
prepare() 在开始播放之前调用这个方法完成准备工作
start() 开始或继续播放音频
pause() 暂停播放音频
reset() MideaPlayer对象重置到刚刚创建的状态
seekTo() 从指定的位置开始播放音频
stop() 停止播放音频。调用这个方法后的MediaPlayer对象无法再播放音频
release() 释放当前MediaPlayer对象相关的资源
isPlaying() 判断当前MediaPlayer是否正在播放音频
getDuration() 获取载入的音频文件的时长

MediaPlayer的工作流程:

  1. 创建出一个MediaPlayer对象
  2. 调用setDataSource()方法来设置音频文件的路径
  3. 再调用prepare()方法使MediaPlayer进入到准备状态
  4. 接下来调用start()方法开始播放音频
  5. 调用pause()方法就会暂停播放,调用reset()方法就会停止播放

使用示例

新建一个PlayAudioTest项目,然后修改activity_main.xml中的代码,如下所示:

<?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" >

    <Button
        android:id="@+id/play"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Play" />

    <Button
        android:id="@+id/pause"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Pause" />

    <Button
        android:id="@+id/stop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Stop" />

</LinearLayout>

然后修改MainActivity中的代码,如下所示:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private MediaPlayer mediaPlayer = new MediaPlayer();

    @Override
    protected 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, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission. WRITE_EXTERNAL_STORAGE }, 1);
        } else {
            initMediaPlayer(); // 初始化MediaPlayer
        }
    }

    private void initMediaPlayer() {
        try {
            File file = new File(Environment.getExternalStorageDirectory(), "music.mp3");
            mediaPlayer.setDataSource(file.getPath()); // 指定音频文件的路径
            mediaPlayer.prepare(); // 让MediaPlayer进入到准备状态
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, 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:
        }
    }

    @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();
        }
    }

}


另外,千万不能忘记在AndroidManifest.xml文件中声明用到的权限,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.playaudiotest">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	...
</manifest>

8.4.2 播放视频

播放视频主要是使用VideoView类来实现,用法和MediaPlayer也比较类似,主要有以下常用方法:

方法名 功能描述
setVideoPath() 设置要播放的视频文件的位置
start() 开始或继续播放视频
pause() 暂停播放视频
resume() 将视频重头开始播放
seekTo() 从指定的位置开始播放视频
isPlaying() 判断当前是否正在播放视频
getDuration() 获取载入视频文件的时长

使用示例

新建PlayVideoTest项目,然后修改activity_main.xml的代码,如下所示:

<?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" >

    <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"
            android: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"
            android:layout_weight="1"
            android:text="Replay" />

    </LinearLayout>

    <VideoView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

接下来修改MainActivity中的代码,如下所示:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(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, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission. WRITE_EXTERNAL_STORAGE }, 1);
        } else {
            initVideoPath(); // 初始化MediaPlayer
        }
    }

    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 (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    initVideoPath();
                } else {
                    Toast.makeText(this, "拒绝权限将无法使用程序", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.play:
                if (!videoView.isPlaying()) {
                    videoView.start(); // 开始播放
                }
                break;
            case R.id.pause:
                if (videoView.isPlaying()) {
                    videoView.pause(); // 暂停播放
                }
                break;
            case R.id.replay:
                if (videoView.isPlaying()) {
                    videoView.resume(); // 重新播放
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (videoView != null) {
            videoView.suspend();
        }
    }

}

另外,仍然始终记得在AndroidManifest.xml文件中声明用到的权限,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.playvideotest">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	...
</manifest>

8.5 小结和点评

本章我们主要对Android系统中的各种媒体技术进行了学习,其中包括通知的使用技巧、调用摄像头拍照、从相册中选出照片,以及播放音频和视频文件。

发布了3 篇原创文章 · 获赞 2 · 访问量 74

猜你喜欢

转载自blog.csdn.net/weixin_41297079/article/details/104398044
今日推荐