当前位置: 首页 > 知识库问答 >
问题:

播放铃声时出错

洪胤
2023-03-14

我试图在电池完全充电时播放铃声。为了实现这一点,我提供了服务。当我第一次执行我的代码时,它工作了,第二次没有,我在日志中不断地得到这些错误。

05-13 10:03:13.925: D/dalvikvm(26079): GC_EXTERNAL_ALLOC freed 25K, 49% free   2788K/5379K, external 518K/518K, paused 29ms
05-13 10:03:13.985: D/CLIPBOARD(26079): Hide Clipboard dialog at Starting input: finished by someone else... !
05-13 10:03:16.210: D/AndroidRuntime(26079): Shutting down VM
05-13 10:03:16.210: W/dalvikvm(26079): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-13 10:03:16.215: E/AndroidRuntime(26079): FATAL EXCEPTION: main
05-13 10:03:16.215: E/AndroidRuntime(26079): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000000 (has extras) } in com.zafar.batterynotify.BatteryService$1@4053f718
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.os.Handler.handleCallback(Handler.java:587)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.os.Looper.loop(Looper.java:130)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at java.lang.reflect.Method.invoke(Method.java:507)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at dalvik.system.NativeStart.main(Native Method)
05-13 10:03:16.215: E/AndroidRuntime(26079): Caused by: java.lang.NullPointerException: uriString
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.net.Uri$StringUri.<init>(Uri.java:420)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.net.Uri$StringUri.<init>(Uri.java:410)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.net.Uri.parse(Uri.java:382)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at com.zafar.batterynotify.BatteryService$1.onReceive(BatteryService.java:49)
05-13 10:03:16.215: E/AndroidRuntime(26079):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
05-13 10:03:16.215: E/AndroidRuntime(26079):    ... 9 more

根据logcat,错误在Uri=Uri上的某个地方。解析(报警)

这是我的服务类代码

public class BatteryService extends Service {
Notify notification = new Notify();
BatteryAlarm alarm = new BatteryAlarm();
private MediaPlayer mMediaPlayer;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    notification.initNotification(this);
    this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    return START_STICKY;
}

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context c, Intent i) {

        int level = i.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int plugged = i.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);

        SharedPreferences getAlarm = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        String alarms = getAlarm.getString("ringtones", null);
        Uri uri = Uri.parse(alarms);

        if(plugged == 2) {

            if(level < 100) {

                if(uri != null) {
                    playAlarm(c, uri);
                }
                //CharSequence t = (CharSequence) uri;
                //Toast.makeText(c, "Set", Toast.LENGTH_LONG).show();

            }

        } else if (plugged == 0) {
            if(uri != null) {
                stopAlarm();
            }
            Toast.makeText(c, "Unplugged", Toast.LENGTH_LONG).show();

        }
    }
};

private void playAlarm(Context c, Uri uri) {
    //Uri uri = Uri.parse(uri);
    mMediaPlayer = new MediaPlayer();
    try {
        mMediaPlayer.reset();
        mMediaPlayer.setDataSource(getBaseContext(), uri);
        final AudioManager audioManager = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
        if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        onDestroy();
    }
}

private void stopAlarm() {
    mMediaPlayer.stop();
    onDestroy();
}

public void onDestroy() {
    super.onDestroy();
    notification.cancelNotification(this);
    unregisterReceiver(this.mBatInfoReceiver);
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();
}

}

请帮帮我。我在哪里犯了错误,因为我一直在犯这些错误

添加日志后的Logcat。d(“标签”,警报)

05-13 10:29:43.450: D/AndroidRuntime(28062): Shutting down VM
05-13 10:29:43.450: W/dalvikvm(28062): threadid=1: thread exiting with uncaught exception (group=0x4001e578)
05-13 10:29:43.455: E/AndroidRuntime(28062): FATAL EXCEPTION: main
05-13 10:29:43.455: E/AndroidRuntime(28062): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.BATTERY_CHANGED flg=0x60000000 (has extras) } in com.zafar.batterynotify.BatteryService$1@40547e78
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.os.Handler.handleCallback(Handler.java:587)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.os.Looper.loop(Looper.java:130)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.app.ActivityThread.main(ActivityThread.java:3691)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at java.lang.reflect.Method.invokeNative(Native Method)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at java.lang.reflect.Method.invoke(Method.java:507)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at dalvik.system.NativeStart.main(Native Method)
05-13 10:29:43.455: E/AndroidRuntime(28062): Caused by: java.lang.NullPointerException: println needs a message
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.util.Log.println_native(Native Method)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.util.Log.d(Log.java:137)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at com.zafar.batterynotify.BatteryService$1.onReceive(BatteryService.java:51)
05-13 10:29:43.455: E/AndroidRuntime(28062):    at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709)
05-13 10:29:43.455: E/AndroidRuntime(28062):    ... 9 more

共有1个答案

陶元凯
2023-03-14

我怀疑这就是问题所在:

String alarms = getAlarm.getString("ringtones", null);
Uri uri = Uri.parse(alarms);

看起来警报仍然为空——所以大概在共享首选项中没有“铃声”字符串

首先,您应该验证这种情况(在您尝试解析它之前记录报警),然后找出预期首选项不存在的原因。

 类似资料:
  • 本文向大家介绍C#播放铃声最简单实现方法,包括了C#播放铃声最简单实现方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#播放铃声最简单实现方法。分享给大家供大家参考。具体分析如下: 因为只是做一个软件的闹铃播放效果,到网上找的时候试了几种,哎,都失败了,而且代码挺杂的,最终一句搞定了: 当然,你要先把铃声文件放进去项目:可直接拖放到项目上(自定义位置),然后属性—>生成操作—>选:嵌

  • 当类初始化时,我试图播放铃声,并使用滑块按钮关闭。播放器在初始化时成功创建,但由于某些原因,由于以下错误,无法使用滑块按钮关闭它。 E/flutter(6529):[ERROR: flutter/lib/ui/ui_dart_state.cc(166)]未处理的异常:缺少插件异常(未找到实现方法hasVibrator对通道振动)E/flutter(6529):#0方法通道。_invokeMetho

  • 问题内容: 我正在Android上创建游戏,并且已经将这个问题搁置了一段时间,现在又回到了问题上。在我的游戏中,我有背景音乐,枪声,爆炸声等,而且我需要能够同时玩它们。现在,当我在SoundPool类上调用play时,当前正在播放的声音被打断,新的声音开始播放。下面是我的SoundManager类及其用法。任何帮助将不胜感激,因为这确实是我需要拥有如此多音效的第一款游戏。谢谢! …这是我如何使用该

  • 播放(播放音效/播放录音)

  • 问题内容: 我想创建2个可以启动和停止背景音频的设备。 这是我的代码: 当我单击时,可以正常工作,可以播放音频。 但是,当我单击时,会出现错误,如果再次单击,将不会再有声音。 这是错误: 我的程序有什么错误? 问题答案: 此SSCCE在这里是“无效结果”,因为音频无例外地重新启动(尝试至少3次)。