如何在Android应用中播放音频和视频文件

By reading this article, you will learn how to write an audio-playing application that interacts with the user. Based on the official documentation:

通过阅读本文,您将学习如何编写与用户交互的音频播放应用程序。 根据官方文件

“The Android multimedia framework includes support for playing variety of common media types, so that you can easily integrate audio, video and images into your applications. You can play audio or video from media files stored in your application’s resources (raw resources), from standalone files in the filesystem, or from a data stream arriving over a network connection, all using MediaPlayer APIs.”

“ Android多媒体框架包括对播放各种常见媒体类型的支持,因此您可以轻松地将音频,视频和图像集成到您的应用程序中。 您可以使用MediaPlayer API从存储在应用程序资源(原始资源)中的媒体文件,文件系统中的独立文件,或通过网络连接到达的数据流中播放音频或视频。”

In other words, you can actually use it to play media files such as videos and images. Having said that, this article focuses on playing only audio files as a brief introduction to this API. Feel free to explore further after that.

换句话说,您实际上可以使用它来播放媒体文件,例如视频和图像。 话虽如此,本文仅着眼于播放音频文件,作为对此API的简要介绍。 之后,请继续探索。

Let’s get started on your Android project.

让我们开始您的Android项目。

1.设定 (1. Setup)

Make sure you have installed Android Studio and the necessary SDK before you proceed. Add the following permission for INTERNET access, as we will be attempting to play both the local and online audios later on:

在继续之前,请确保已安装Android Studio和必要的SDK。 为INTERNET访问添加以下权限,因为稍后我们将尝试播放本地和在线音频:

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

Inside the res folder, create a new folder called raw. Place your audio files inside the raw folder just like in the image below. Feel free to name it anything that you prefer as long as it follows the correct naming convention:

res文件夹中,创建一个名为raw的新文件夹。 如下图所示,将音频文件放在raw文件夹中。 只要遵循正确的命名约定,即可随意命名任何您喜欢的名称:

Image for post

For online audio files, you can either set up your own server or just get a valid audio file URL to test it. Let’s proceed to the next section and start writing Java code.

对于在线音频文件,您可以设置自己的服务器,也可以仅获取有效的音频文件URL对其进行测试。 让我们继续下一节并开始编写Java代码。

2.实施 (2. Implementation)

进口 (Import)

Add the following import declaration on top of your Java class:

在Java类的顶部添加以下导入声明:

import android.media.AudioAttributes;
import android.media.MediaPlayer;

初始化 (Initialization)

If you are going to play just one audio file, you can initialize it inside the onCreate function. Pass in the respective resource based on the name of the audio file:

如果只播放一个音频文件,则可以在onCreate函数中对其进行初始化。 根据音频文件的名称传入相应的资源:

mp = MediaPlayer.create(this, R.raw.alarm);

开始或继续播放 (Start or Resume Playback)

After that, you can just call the following function to start playing the audio file:

之后,您只需调用以下函数即可开始播放音频文件:

mp.start();

In theory, you have to make sure that the state is prepared before calling start. In this case, the create function automatically does that for you. Let’s have a look at the following diagram to know more about the flow. You will encounter IllegalStateException if you do not follow the correct sequences.

从理论上讲,您必须在调用start之前确保状态已准备就绪。 在这种情况下, create功能会自动为您执行此操作。 让我们看一下下图,以了解有关流程的更多信息。 如果不遵循正确的顺序,则会遇到IllegalStateException

Image for post
Android. Android提供

暂停 (Pause)

Call the following function to pause it. You can resume it easily by calling start again:

调用以下函数将其暂停。 您可以通过再次调用start轻松恢复它:

mp.pause();

正在玩 (isPlaying)

If you need to check whether MediaPlayer is running, call the following function:

如果需要检查MediaPlayer是否正在运行,请调用以下函数:

if (mp.isPlaying()) {}

停止 (Stop)

Stopping the media can be easily done via the following function call:

可以通过以下函数调用轻松停止媒体:

mp.stop()

You need to call prepare() or prepareAsync() before you can call start() again.

您需要先调用prepare()prepareAsync()然后才能再次调用start()

释放 (Release)

Once you are done with it and no longer need the MediaPlayer object, it is highly recommended to release it and set it to null to free up the resources:

一旦完成操作,不再需要MediaPlayer对象,强烈建议将其释放并将其设置为null以释放资源:

mp.release();
mp = null;

在线网址 (Online URL)

For loading online resources, you need to call setDataSource instead of the create function. Initialize it and pass in the string of the URL that points to the respective online audio files. You have to wrap it inside a try catch block to catch the IOException error. It might take some time for it to load the file. It’s recommended to test it out with a clip lasting a few seconds.

为了加载在线资源,您需要调用setDataSource而不是create函数。 对其进行初始化,并传入指向相应在线音频文件的URL字符串。 您必须将其包装在try catch块中才能捕获IOException错误。 加载文件可能需要一些时间。 建议使用持续几秒钟的剪辑对其进行测试。

3.结论 (3. Conclusion)

Let’s recap what we have learned today. We started off with setting the permission for INTERNET access and storing the audio files inside the res folder.

让我们回顾一下我们今天学到的东西。 我们首先设置了INTERNET访问权限并将音频文件存储在res文件夹中。

Then we explored the MediaPlayer API, including how to initialize the object based on local resources. There are a lot of functions available that can be used to start, pause, resume, or stop the playback.

然后,我们探索了MediaPlayer API,包括如何基于本地资源初始化对象。 有很多可用的功能可用于开始,暂停,继续或停止播放。

Moreover, we even wrote a function to load resources from an online URL. Feel free to explore this topic further and try it on video files.

此外,我们甚至编写了一个从在线URL加载资源的功能。 随意进一步探讨此主题,并在视频文件上尝试一下。

Thanks for reading. I hope to see you again in the next article!

谢谢阅读。 我希望在下一篇文章中再见!

翻译自: https://medium.com/better-programming/how-to-play-audio-and-video-files-in-your-android-app-15b846411cb2

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值