Android 4.4系统 - 播放默认的音乐播放器 [英] Android 4.4 - Play default music player

查看:697
本文介绍了Android 4.4系统 - 播放默认的音乐播放器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我这是为什么不工作的奇巧了,如何解决呢?

 私人意图的球员;
    玩家=新意​​图(Intent.ACTION_MEDIA_BUTTON);
    同步(本){
        player.putExtra(Intent.EXTRA_KEY_EVENT,新的KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEY code_MEDIA_PLAY));
        sendOrderedBroadcast(播放器,NULL);        player.putExtra(Intent.EXTRA_KEY_EVENT,新的KeyEvent(KeyEvent.ACTION_UP,KeyEvent.KEY code_MEDIA_PLAY));
        sendOrderedBroadcast(播放器,NULL);
    }


解决方案

有在被专为此目的创建API级别19新方法AudioManager.dispatchMediaKeyEvent(KeyEvent)方法。

<一个href=\"http://developer.android.com/reference/android/media/AudioManager.html#dispatchMediaKeyEvent(android.view.KeyEvent)\" rel=\"nofollow\">http://developer.android.com/reference/android/media/AudioManager.html#dispatchMediaKeyEvent(android.view.KeyEvent)

这code适用于奇巧:

  AudioManager上午=(AudioManager)context.getSystemService(Context.AUDIO_SERVICE);长EVENTTIME = SystemClock.uptimeMillis() -  1;
KeyEvent的downEvent =新的KeyEvent(EVENTTIME,EVENTTIME,KeyEvent.ACTION_DOWN,KeyEvent.KEY code_MEDIA_PLAY_PAUSE,0);
am.dispatchMediaKeyEvent(downEvent);EVENTTIME ++;
KeyEvent的upEvent =新的KeyEvent(EVENTTIME,EVENTTIME,KeyEvent.ACTION_UP,KeyEvent.KEY code_MEDIA_PLAY_PAUSE,0);
am.dispatchMediaKeyEvent(upEvent);

Can anyone tell me why this is not working on Kitkat anymore and how to solve it?

    private Intent player;
    player = new Intent(Intent.ACTION_MEDIA_BUTTON);
    synchronized (this) {
        player.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY));
        sendOrderedBroadcast(player, null);

        player.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY));
        sendOrderedBroadcast(player, null);
    }

解决方案

There is new method AudioManager.dispatchMediaKeyEvent(KeyEvent) in API level 19 that was created specifically for this purpose.

http://developer.android.com/reference/android/media/AudioManager.html#dispatchMediaKeyEvent(android.view.KeyEvent)

This code works on KitKat:

AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);

long eventtime = SystemClock.uptimeMillis() - 1;
KeyEvent downEvent = new KeyEvent(eventtime, eventtime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);
am.dispatchMediaKeyEvent(downEvent);

eventtime++;
KeyEvent upEvent = new KeyEvent(eventtime,eventtime,KeyEvent.ACTION_UP,KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE, 0);         
am.dispatchMediaKeyEvent(upEvent);

这篇关于Android 4.4系统 - 播放默认的音乐播放器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆