在Android应用程序中播放mp3文件
我正在制作一个 Android 应用程序,一旦收到包含“PhoneAlarm”的短信,它将播放 mp3 文件。 这是对我有用的最终代码。
请注意:“我不会使用这里的所有编码” 看看我的代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.gsm.SmsMessage;
import android.widget.DigitalClock;
import android.widget.Toast;
import android.media.MediaPlayer;
public class IncomingSmsCaptureApp extends BroadcastReceiver {
MediaPlayer mp1;
@Override
public void onReceive(Context context, Intent intent) {
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"Notes\file.txt");
//Read text from file
String text = new String();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
String Message = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
Message = msgs[i].getMessageBody().toString();
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
if (Message == "PhoneAlarm") {
//Play alarm sound
mp1 = MediaPlayer.create(context, R.raw.alarm);
mp1.start();
}
}
}
}
I am making an android application that will play an mp3 file once a sms containing "PhoneAlarm" is received.
This is the final code that works for me.
Note that: "I will not use all of the coding that's in here"
Take a look at my code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.telephony.gsm.SmsMessage;
import android.widget.DigitalClock;
import android.widget.Toast;
import android.media.MediaPlayer;
public class IncomingSmsCaptureApp extends BroadcastReceiver {
MediaPlayer mp1;
@Override
public void onReceive(Context context, Intent intent) {
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"Notes\file.txt");
//Read text from file
String text = new String();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
String Message = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
Message = msgs[i].getMessageBody().toString();
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
if (Message == "PhoneAlarm") {
//Play alarm sound
mp1 = MediaPlayer.create(context, R.raw.alarm);
mp1.start();
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
setDataSource()
方法需要媒体文件的字符串路径,而不仅仅是资源 ID,您可以使用create(context, resid)
相反,它将是准备好播放你的声音:The
setDataSource()
method expects a string path to the media file, not just a resource id, you can create it using thecreate(context, resid)
instead, and it will be ready to play your sound: