在Android应用程序中播放mp3文件

发布于 2024-12-23 13:20:46 字数 1914 浏览 3 评论 0原文

我正在制作一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小傻瓜 2024-12-30 13:20:46

setDataSource() 方法需要媒体文件的字符串路径,而不仅仅是资源 ID,您可以使用 create(context, resid) 相反,它将是准备好播放你的声音:

//When creating your player:
mp1 = MediaPlayer.create(context, R.raw.alarm);

//When playing sound, note that prepare() should not be called:
mp1.start();

//When you don't need the player anymore:
mp1.release();

The setDataSource() method expects a string path to the media file, not just a resource id, you can create it using the create(context, resid) instead, and it will be ready to play your sound:

//When creating your player:
mp1 = MediaPlayer.create(context, R.raw.alarm);

//When playing sound, note that prepare() should not be called:
mp1.start();

//When you don't need the player anymore:
mp1.release();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文