MusicDroid - Audio Player Part I
SDK Version:
M3
<!-- google_ad_section_start --><!--paging_filter-->
<!--break-->Introduction
As you can imagine a popular way to load music onto a cell phone will be via removable storage, such as an SD card. In part 1 of our media player tutorial we will build a simple media player that will allow the user to select a song from the SD card and play it.
Click here if you would like to download the source for this tutorial.
Note: there is a known issue with the Emulator where the mixer will cut in and out on some systems resulting in very choppy audio, hopefully this will be addressed in the next SDK release.
Layouts
This project only consists of one Activity, a ListActivity. So, for a ListActivity we need a ListView for the actual list, and another view that will be used for each item in the list. You can get fancy, but for this example we will just use a TextView to display the name of each file.
First, here is our ListView (songlist.xml):
-
<?xml version="1.0" encoding="UTF-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent">
-
-
<ListView id="@id/android:list"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:layout_weight="1"
-
android:drawSelectorOnTop="false"/>
-
-
<TextView id="@id/android:empty"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
android:text="No songs found on SD Card."/>
-
</LinearLayout>
Very standard ListView. The TextView entry will display when there are no items in the ListView because it's using the built-in id of "@id/android:empty".
And for each file here is the TextView to be used (song_item.xml):
-
<?xml version="1.0" encoding="utf-8"?>
-
<TextView id="@+id/text1" xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"/>
Again, very basic, shouldn't be anything you haven't seen before.
You may be thinking, why does the screenshot above show a black ListView, when nothing in these layouts mentions color? Well, that is determined by the "theme" in the AndroidManifest.xml. In the "application" element you can define the theme by adding "android:theme="@android:style/Theme.Dark"".
The ListActivity
We now must work on our ListActivity which we will call MusicDroid. Here is the declaration of this class and it's onCreate() function:
-
-
-
private static final String MEDIA_PATH =
new String("/sdcard/");
-
private List<String> songs = new ArrayList<String>();
-
-
private int currentPosition = 0;
-
-
@Override
-
public void onCreate
(Bundle icicle
) {
-
super.onCreate(icicle);
-
setContentView
(R.
layout.
songlist);
-
updateSongList();
-
}
First we set up some private member variables to be used by this Activity. The first one is MEDIA_PATH, and we set it to "/sdcard" because that is the location of the SD card. Next comes a List of Strings that will hold the filename for each song on the list. And of course we need a MediaPlayer object, which we call mp. The final one up there is currentPosition, which we will use to store the index of the song currently playing.
The onCreate() function is pretty basic, we set our view to be the songlist view that we created above and call the function updateSongList(), here is that function:
-
public void updateSongList() {
-
-
if (home.listFiles(new Mp3Filter()).length > 0) {
-
for (File file : home.
listFiles(new Mp3Filter
())) {
-
songs.add(file.getName());
-
}
-
-
ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
-
R.
layout.
song_item, songs
);
-
setListAdapter(songList);
-
}
-
}
Here we create a File Object called "home" which points to "/sdcard". We loop through the files returned by home.ListFiles(), adding each file to our List object "songs". Once we have this list filled we create an ArrayAdapter passing in the songs list and then set it to be our ListActivity's ListAdapter on line 47. This will populate our ListView.
You may have noticed the object above called "Mp3Filter". This is an object that implements FilenameFilter. This object is used to filter out what files should be returned, this is done by implementing the accept(File, String) function. Here is the object that we can use to filter so that listFiles() only returns MP3 files:
-
-
-
return (name.endsWith(".mp3"));
-
}
-
}
Now we should be able to build a list of all the MP3 files in /sdcard. So now we just need to be able to select a song and play it. Fist things first, let's override onListItemClick() so we will be notified when a song is clicked on:
-
@Override
-
protected void onListItemClick
(ListView l,
View v,
int position,
long id
) {
-
currentPosition = position;
-
playSong(MEDIA_PATH + songs.get(position));
-
}
Pretty basic function here. We set currentPosition to hold the index of the position that was clicked on and then pass in the path of the song to playSong(String), so lets take a look at what's happening in playSong(String):
-
private void playSong
(String songPath
) {
-
try {
-
-
mp.reset();
-
mp.setDataSource(songPath);
-
mp.prepare();
-
mp.start();
-
-
// Setup listener so next song starts automatically
-
mp.setOnCompletionListener(new OnCompletionListener() {
-
-
-
nextSong();
-
}
-
-
});
-
-
-
Log.
v(getString
(R.
string.
app_name), e.
getMessage());
-
}
-
}
The MediaPlayer object makes things really easy for us here. First we call mp.reset(), which will reset the MediaPlayer to its normal state. This is required if you were playing a song and want to change the data source. The reset() function will also stop whatever is playing, so if a song is playing and then you select another it will stop that one before starting the next song.
We then pass in the path to the song to mp.setDataSource(String) and call prepare() and start(). At this point the MediaPlayer will start playing your song.
Next job is to setup an OnCompletionListener starting on line 66. The function onCompletion(MediaPlayer) will be called when the song is over. All we do there is call the function nextSong() from our Activity. Here is nextSong():
-
private void nextSong() {
-
if (++currentPosition >= songs.size()) {
-
// Last song, just reset currentPosition
-
currentPosition = 0;
-
} else {
-
// Play next song
-
playSong(MEDIA_PATH + songs.get(currentPosition));
-
}
-
}
Here we check to make sure this isn't the last song on the list, if it is we won't do anything, if not we'll play the next song using the playSong(String) function.
So that's it for the code, on the next page we'll figure out how to get this thing running...
分享到:
相关推荐
link rel =" stylesheet " type =" text/css " href =" https://cdn.jsdelivr.net/gh/greghub/green-audio-player/dist/css/green-audio-player.min.css " > < script src =" ...
"toolsoft-audio-player"是一款专业的音频播放软件,专为用户提供高质量的音乐体验。这款播放器支持多种音频格式,能够满足用户对不同音频文件的播放需求。在深入了解这个软件之前,我们先来看一下它的核心特点。 1...
$ npm i react-h5-audio-player 或者 $ yarn add react-h5-audio-player 用法 import AudioPlayer from 'react-h5-audio-player' ; import 'react-h5-audio-player/lib/styles.css' ; // import 'react-h
ISO_IEC标准 14496-3-Part3-Audio
在给定的项目中,我们看到一个基于Vue.js的音频播放器组件——vue-audio-better,它被用于非工程化的项目,意味着它可能不是通过现代前端构建工具(如webpack)进行模块化管理的,而是直接在HTML文件中引入Vue.js和...
XMOS-Stereo-USB-Audio-Class2-Driver-3033_v4.13.0公板
vue使用js-audio-recorder实现一句话识别功能:以vue前端框架+腾讯云语音识别为基础,实现客户端长按录音,获取语音转换编译出来的识别码,传给后端去腾讯云语音识别库去换取识别回来的文本尽心客户端的文本查询!...
这个压缩包“HTML5-Audio-player.rar_audio player_html5 audio_player”包含了实现这一功能的相关文件。下面将详细探讨HTML5 Audio Player的核心知识点及其在网页开发中的应用。 首先,HTML5 Audio是HTML5标准中...
M-Audio FireWire Audiophile声卡可用的Windows 10驱动 1.下载FireWire Solo的驱动,传送门。它与FireWire Audiophile其实是可以兼容通用的,而且可以经过下面的步骤可以在Windows 10下使用并向下兼容。 2.不...
GD32F4-I2S-Audio-Player.rar
"(0151)-iOS/iPhone/iPad/iPod源代码-音频声效(Audio)-Audio Player Controller"这个项目提供了一个实现这一目标的实例。这个项目的核心在于使用Audio Player来播放mp3格式的音乐,其效果类似于原生的iPhone...
React音频播放器预先要求 npm i -g gruntnpm i建造 npm run build发射 npm run serve # localhost:3000发展 npm run dev # localhost:3000
在"XMOS-Stereo-USB-Audio-Class2-Driver-3033_v4.13.0.exe"这个压缩包内,包含的是安装驱动程序所需的可执行文件。安装过程一般包括识别硬件设备、拷贝驱动文件到系统目录、注册驱动以及配置系统设置等步骤。对于...
安装ngx-audio-player可通过和使用npm: $ npm install ngx-audio-player --save 使用纱: $ yarn add ngx-audio-player入门NgxAudioPlayerModule需要角度材质。 确保您已安装以下依赖项,并具有与上述版本相同或更...
创建 React 应用程序入门 这个项目是用引导的。 可用脚本 在项目目录中,您可以运行: yarn start 在开发模式下运行应用程序。 打开在浏览器中查看。 如果您进行编辑,页面将重新加载。 您还将在控制台中看到任何 ...
React本地音频记录播放器 这是用于音频记录器和播放器的本机链接模块。 这不是播放列表音频模块,并且该库为android和ios平台提供了简单的记录器和播放器功能。 这仅支持每个平台的默认文件扩展名。...
美奥多M-Audio ProFire 610声卡驱动程序,专门优化,美奥多生产专业化的声卡,是发烧友必备。M-AUDIO 610声卡支持windowsxp/vista/win7系统的驱动程序v6.0.9版,很好用比较稳定。,欢迎下载体验
这个"webrtc-audio-processing-1.0例子"压缩包包含了一个关于WebRTC音频处理的实例,提供了源代码以及音频测试文件,帮助开发者更好地理解和使用WebRTC中的音频处理功能。 1. **WebRTC音频处理** WebRTC音频处理...
m-audio 64位驱动 专业声卡驱动
本文将深入探讨“XMOS-Stereo-USB-Audio-Class2-Driver-3079_v2.26.0”这一驱动程序,以及它在xmos外置声卡中的应用。 首先,我们需要理解什么是驱动程序。驱动程序是操作系统与硬件设备之间的一个桥梁,它负责解释...