MediaPlayer没有初始化

时间:2013-12-07 02:46:17

标签: android android-mediaplayer

package com.m.omg;
import android.content.BroadcastReceiver;     
import android.content.Context;  
import android.content.Intent;   
import android.media.MediaPlayer;  
import android.os.Bundle;  
import android.telephony.SmsMessage;  
import android.widget.Toast; 

public class SmsReceiver extends BroadcastReceiver  
{   
MediaPlayer mp =new MediaPlayer();  
@Override  
public void onReceive(Context context, Intent intent)   
{  

    String mymsg = null;
    String num = null;

    //get the SMS message passed in
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;            
    if (bundle != null)
    {
        //retrieving the received SMS
        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]);                
            num = msgs[i].getOriginatingAddress();
            mymsg = msgs[i].getMessageBody().toString();
        }
        Toast.makeText(context, "from" + num + ":" + mymsg, Toast.LENGTH_LONG).show();

        //making decision, depending on the number & message body
        if (num.equalsIgnoreCase("some# :) ")){
            abortBroadcast();
            if (mymsg.equalsIgnoreCase("start")){

        mp.create(context, R.raw.music);
        mp.start();

            }
        } 
        mp.release();
    }    
}

}

* 媒体播放器在条件匹配时未初始化...应用程序正常工作,当我收到短信时,显示Toast但未触发媒体播放器,我在eclipse中收到此警告“静态应该以静态的方式访问MediaPlayer类型的方法create(Context,int)“我忽略了它:D *

1 个答案:

答案 0 :(得分:0)

使用

mp=MediaPlayer.create(context, R.raw.music);

而不是

mp.create(context, R.raw.music);

用于初始化MediaPlayer的mp实例