许多在线音乐播放器应用程序在其应用程序中需要这么多的歌曲和音频文件。因此,要处理这么多文件,我们必须使用任何类型的数据库并管理所有这些文件。在应用程序内部存储文件将不是更好的方法。因此,在本文中,我们将介绍在Android应用程序中实现动态音频播放器的过程。
我们将在本文中构建什么?
在本文中,我们将构建一个简单的应用程序,在该程序中,我们将b从Web URL播放音频文件,并在运行时更改该音频文件URL以更新我们的音频文件。下面提供了一个示例视频,以使您对我们在本文中将要做的事情有个大概的了解。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
第2步:将您的应用连接到Firebase
创建新项目后。导航到顶部栏上的“工具”选项。在里面单击Firebase。单击Firebase后,您可以在屏幕快照中看到下面提到的右列。
在该列内,导航到Firebase实时数据库。单击该选项,您将在“将应用程序连接到Firebase”和“将Firebase实时数据库添加到您的应用程序”中看到两个选项。单击立即连接选项,您的应用程序将连接到Firebase。之后,单击第二个选项,现在您的应用已连接到Firebase。
完成此过程后,您将看到以下屏幕。
现在,验证您的应用是否已连接到Firebase。转到您的build.gradle文件。导航至应用程序> Gradle脚本> build.gradle(app)文件,并确保在依赖项部分中添加了以下依赖项。
implementation ‘com.google.firebase:firebase-database:19.6.0’
添加此依赖关系后,在Gradle文件中添加ExoPlayer的依赖关系。
步骤3:为Internet添加权限
当我们从Internet加载视频时,我们必须在Manifest文件中添加Internet权限。导航至应用程序> AndroidManifest.xml文件,然后在其中添加以下权限。
XML
XML
Java
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
// creating a variable for
// button and media player
Button playBtn, pauseBtn;
MediaPlayer mediaPlayer;
// creating a string for storing
// our audio url from firebase.
String audioUrl;
// creating a variable for our Firebase Database.
FirebaseDatabase firebaseDatabase;
// creating a variable for our
// Database Reference for Firebase.
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// below line is used to get the instance
// of our Firebase database.
firebaseDatabase = FirebaseDatabase.getInstance();
// below line is used to get reference for our database.
databaseReference = firebaseDatabase.getReference("url");
// calling add value event listener method for getting the values from database.
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
// this method is call to get the realtime updates in the data.
// this method is called when the data is changed in our Firebase console.
// below line is for getting the data from snapshot of our database.
audioUrl = snapshot.getValue(String.class);
// after getting the value for our audio url we are storing it in our string.
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
// calling on cancelled method when we receive any error or we are not able to get the data.
Toast.makeText(MainActivity.this, "Fail to get audio url.", Toast.LENGTH_SHORT).show();
}
});
// initializing our buttons
playBtn = findViewById(R.id.idBtnPlay);
pauseBtn = findViewById(R.id.idBtnPause);
// setting on click listener for our play and pause buttons.
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calling method to play audio.
playAudio(audioUrl);
}
});
pauseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checking the media player
// if the audio is playing or not.
if (mediaPlayer.isPlaying()) {
// pausing the media player if
// media player is playing we are
// calling below line to stop our media player.
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
// below line is to display a message when media player is paused.
Toast.makeText(MainActivity.this, "Audio has been paused", Toast.LENGTH_SHORT).show();
} else {
// this method is called when media player is not playing.
Toast.makeText(MainActivity.this, "Audio has not played", Toast.LENGTH_SHORT).show();
}
}
});
}
private void playAudio(String audioUrl) {
// initializing media player
mediaPlayer = new MediaPlayer();
// below line is use to set the audio stream type for our media player.
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
// below line is use to set our
// url to our media player.
mediaPlayer.setDataSource(audioUrl);
// below line is use to prepare
// and start our media player.
mediaPlayer.prepare();
mediaPlayer.start();
// below line is use to display a toast message.
Toast.makeText(this, "Audio started playing..", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// this line of code is use to handle error while playing our audio file.
Toast.makeText(this, "Error found is " + e, Toast.LENGTH_SHORT).show();
}
}
}
步骤4:使用activity_main.xml文件
转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。
XML格式
步骤5:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
// creating a variable for
// button and media player
Button playBtn, pauseBtn;
MediaPlayer mediaPlayer;
// creating a string for storing
// our audio url from firebase.
String audioUrl;
// creating a variable for our Firebase Database.
FirebaseDatabase firebaseDatabase;
// creating a variable for our
// Database Reference for Firebase.
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// below line is used to get the instance
// of our Firebase database.
firebaseDatabase = FirebaseDatabase.getInstance();
// below line is used to get reference for our database.
databaseReference = firebaseDatabase.getReference("url");
// calling add value event listener method for getting the values from database.
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
// this method is call to get the realtime updates in the data.
// this method is called when the data is changed in our Firebase console.
// below line is for getting the data from snapshot of our database.
audioUrl = snapshot.getValue(String.class);
// after getting the value for our audio url we are storing it in our string.
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
// calling on cancelled method when we receive any error or we are not able to get the data.
Toast.makeText(MainActivity.this, "Fail to get audio url.", Toast.LENGTH_SHORT).show();
}
});
// initializing our buttons
playBtn = findViewById(R.id.idBtnPlay);
pauseBtn = findViewById(R.id.idBtnPause);
// setting on click listener for our play and pause buttons.
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// calling method to play audio.
playAudio(audioUrl);
}
});
pauseBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// checking the media player
// if the audio is playing or not.
if (mediaPlayer.isPlaying()) {
// pausing the media player if
// media player is playing we are
// calling below line to stop our media player.
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
// below line is to display a message when media player is paused.
Toast.makeText(MainActivity.this, "Audio has been paused", Toast.LENGTH_SHORT).show();
} else {
// this method is called when media player is not playing.
Toast.makeText(MainActivity.this, "Audio has not played", Toast.LENGTH_SHORT).show();
}
}
});
}
private void playAudio(String audioUrl) {
// initializing media player
mediaPlayer = new MediaPlayer();
// below line is use to set the audio stream type for our media player.
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
// below line is use to set our
// url to our media player.
mediaPlayer.setDataSource(audioUrl);
// below line is use to prepare
// and start our media player.
mediaPlayer.prepare();
mediaPlayer.start();
// below line is use to display a toast message.
Toast.makeText(this, "Audio started playing..", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// this line of code is use to handle error while playing our audio file.
Toast.makeText(this, "Error found is " + e, Toast.LENGTH_SHORT).show();
}
}
}
第6步:在Firebase控制台中为您的音频文件添加URL
用于在Firebase控制台中添加音频URL。在浏览器中浏览Firebase,然后单击右上角的“转到控制台”选项,如以下屏幕截图所示。
单击“转到控制台”选项后,您将看到您的项目。从可用的项目列表中单击您的项目名称。
单击项目后。单击左侧窗口中的实时数据库选项。
单击此选项后,您将看到右侧的屏幕。在此页面上,单击顶部栏中的“规则”选项。您将看到以下屏幕。
在此项目中,我们添加了适用于读取和写入的规则,因为我们没有使用任何身份验证来验证用户。因此,我们目前将其设置为true以测试我们的应用程序。更改规则后。单击右上角的“发布”按钮,您的规则将保存在此处。现在再次回到“数据”选项卡。现在,我们将从Firebase本身将数据手动添加到Firebase。
在Firebase实时数据库中。导航到“数据”选项卡。在数据库部分的此选项卡内,单击“ +”图标。单击“ +”图标后,您将看到两个输入字段,即“名称”和“值”字段。在“名称”字段中,您必须添加视频文件的引用,在本例中为“ url ”。在值字段中,我们必须添加音频文件的URL。在此字段中添加值之后。单击添加按钮,您的数据将被添加到Firebase控制台。
添加视频的网址后。现在运行您的应用程序,并在下面查看该应用程序的输出:
输出:
您可以动态更改音频的URL。