1

我在使用 MediaPlayer 的 BroadcastReceiver 中遇到了奇怪的情况。播放一定数量的 mp3 文件后应用程序崩溃。

所以,我有活动

public class MyActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        initButtons();
    }

    private void onStartButtonClicked()
    {
        Intent service = new Intent("com.ggspot.action.LAUNCH_MY_SERVICE");
        startService(service);
    }

    protected void onStopButtonClicked()
    {
        MyAlarm.stop(this);
    }

    private void initButtons()
    {
        Button startButton = (Button) findViewById(R.id.startButton);
        Button stopButton = (Button) findViewById(R.id.stopButton);

        startButton.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                onStartButtonClicked();
            }
        });

        stopButton.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                onStopButtonClicked();
            }
        });
    }
}

此活动通过按下按钮启动我的服务

public class MyService extends Service
{
    @Override
    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        MyAlarm alarm = new MyAlarm(getApplicationContext(), 3);
        return START_STICKY;
    }    
}

服务只是创建 BroadcastReceiver

public class MyAlarm extends BroadcastReceiver
{
    public MyAlarm()
    {
    }

    public MyAlarm(Context context, int timeoutInSeconds)
    {
        AlarmManager alarmMgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, MyAlarm.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        Calendar time = Calendar.getInstance();
        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(),
                timeoutInSeconds * 1000, pendingIntent);
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        File file = new File(Environment.getExternalStorageDirectory()
            + "/music/beep-7.mp3");

        Uri uri = Uri.fromFile(file);
        MediaPlayer player = MediaPlayer.create(context, uri);
        player.start();
    }

    public static void stop(Context context)
    {
        AlarmManager alarmMgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, MyAlarm.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmMgr.cancel(pendingIntent);
    }
}

文件/music/beep-7.mp3只是持续不到半秒的短哔声。

我的安卓清单:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".MyService">
        <intent-filter>
            <action android:name="com.ggspot.action.LAUNCH_MY_SERVICE"/>
        </intent-filter>
    </service>

    <receiver
        android:name=".MyAlarm">
    </receiver>
</application>

所以,我希望在按下开始按钮后,我会在三秒内听到一次哔哔声。即使我关闭应用程序,蜂鸣声也会继续。为了停止哔哔声,我应该启动活动并按停止按钮。
我现在拥有的对我来说真的很奇怪:

  • 按下开始按钮后,我听到哔哔声 7 次,然后应用程序崩溃
  • 崩溃后按下“强制关闭”按钮后,我可以再次听到哔哔声 7 次。然后我想服务崩溃了。

我不太了解LogCat。它说:
无法启动接收器 com.ggspot.test.services.MyAlarm: java.lang.NullPointerException
但我不明白在哪里。以及为什么只有在第七次尝试播放 mp3 文件之后。

更新:

LogCat 跟踪:

10-09 08:42:23.479: E/AndroidRuntime(12578): FATAL EXCEPTION: main
10-09 08:42:23.479: E/AndroidRuntime(12578): java.lang.RuntimeException: Unable to start receiver com.ggspot.test.services.MyAlarm: java.lang.NullPointerException
10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:1809)
10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.app.ActivityThread.access$2400(ActivityThread.java:117)
10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.os.Looper.loop(Looper.java:123)
10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.app.ActivityThread.main(ActivityThread.java:3687)
10-09 08:42:23.479: E/AndroidRuntime(12578):    at java.lang.reflect.Method.invokeNative(Native Method)
10-09 08:42:23.479: E/AndroidRuntime(12578):    at java.lang.reflect.Method.invoke(Method.java:507)
10-09 08:42:23.479: E/AndroidRuntime(12578):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
10-09 08:42:23.479: E/AndroidRuntime(12578):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
10-09 08:42:23.479: E/AndroidRuntime(12578):    at dalvik.system.NativeStart.main(Native Method)
10-09 08:42:23.479: E/AndroidRuntime(12578): Caused by: java.lang.NullPointerException
10-09 08:42:23.479: E/AndroidRuntime(12578):    at com.ggspot.test.services.MyAlarm.onReceive(MyAlarm.java:58)
10-09 08:42:23.479: E/AndroidRuntime(12578):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:1798)
10-09 08:42:23.479: E/AndroidRuntime(12578):    ... 10 more

更新
的解决方案是将 CompletionListener 添加到媒体播放器并在播放 mp3 后释放它:

@Override
public void onReceive(Context context, Intent intent)
{
    File file = new File(Environment.getExternalStorageDirectory()
            + "/music/beep-7.mp3");

    Uri uri = Uri.fromFile(file);
    MediaPlayer player = MediaPlayer.create(context, uri);
    player.start();

    player.setOnCompletionListener(new OnCompletionListener()
    {

        @Override
        public void onCompletion(MediaPlayer mp)
        {
            mp.release();                
        }
    });
}
4

1 回答 1

1

阅读 LogCat

Caused by: java.lang.NullPointerException
    at com.ggspot.test.services.MyAlarm.onReceive(MyAlarm.java:58)

这表示 NullPointerException 发生在 MyAlarm.java 的第 58 行。

问题
我相信您正在尝试使用重复警报来重播beep-7.mp3,这是不正确的。当第二个警报响起时,onReceive()您正在尝试重新创建 MediaPlayer:

MediaPlayer player = MediaPlayer.create(context, uri);

但是您从未发布过第一个,此 MediaPlayer 的一个副本可以随时存在。所以第二个playernull并且player.start()(第 58 行)抛出 NullPointerException:

解决方案
要重复beep-7.mp3,只需使用MediaPlayer#setLooping()

player.setLooping(true);

完成 MediaPlayer 后,您必须player.release()在尝试创建第二个 MediaPlayer 之前调用,否则您可能会遇到同样的问题。

于 2012-10-09T16:39:06.543 回答