简体   繁体   English

使用MediaPlayer在Google TV上播放提示音

[英]Using MediaPlayer to play a notification beep on Google TV

I have some code that plays a small notification beep from an mp3 (included in res/raw) that works fine on regular Android devices (various 2.2 and up), which I am trying to also use on Google TV and it doesn't seem to do anything, no errors, just no sound. 我有一些代码可以播放mp3的小提示音(包含在res / raw中),在正常的Android设备(2.2及更高版本)上可以正常运行,我也尝试在Google TV上使用它,但似乎没有做任何事情,没有错误,只有声音。

What could I be doing wrong (this has to be something simple that I am missing?). 我可能做错了什么(这一定是我所缺少的简单东西吗?)。

Here is the basic code I'm using: 这是我正在使用的基本代码:

private void beep() {
   AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
   int result = audioManager.requestAudioFocus(null, AudioManager.STREAM_NOTIFICATION, AudioManager.AUDIOFOCUS_GAIN);
   if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
      try {
         Log.d(App.LOG_TAG, "playing the damn beep ****************");
         MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.beep);
         // tried with and without volume 
         ///mediaPlayer.setVolume(1.0f, 1.0f);
         mediaPlayer.setLooping(false);
         mediaPlayer.start();
      } catch (Exception e) {
         Log.e("beep", "error: " + e.getMessage(), e);
      }
   } else {
      Log.d(App.LOG_TAG, "could  not gain audio focus");
   }   
}

(NOTE: In real life I don't recreate the MediaPlayer and AudioManager every time, but I tried just putting everything in one method for test purposes. This works fine on the regular Android emulator, and on regular Android [phone] devices, but plays no sound on GTV emulator or Logitech Revue -- even though it does get into the MediaPlayer block fine and logs that it's playing the beep.) (注意:在现实生活中,我不会每次都重新创建MediaPlayer和AudioManager,但是我只是尝试将所有内容都放在一种方法中以进行测试。这在常规的Android模拟器和常规的Android [phone]设备上都可以正常工作,但是在GTV模拟器或Logitech Revue上不播放声音-即使它确实进入MediaPlayer块并记录其正在播放哔声。)

I had the same problem. 我有同样的问题。 For whatever reason, it looks like the files need to be in ogg format. 无论出于何种原因,看起来文件都必须为ogg格式。 I tried .mp3, and a few versions of .wav. 我尝试了.mp3和.wav的几个版本。 Even the GTV dev docs ( https://developers.google.com/tv/android/docs/gtv_media_formats#StandardFormats ) say .mp3 is okay. 甚至GTV开发人员文档( https://developers.google.com/tv/android/docs/gtv_media_formats#StandardFormats )都说.mp3可以。

My HiSense Pulse Google TV only plays the files if they are .ogg files. 如果它们是.ogg文件,则我的HiSense Pulse Google TV仅播放这些文件。

This online converter seems to work: http://media.io/ 这个在线转换器似乎可以正常工作: http : //media.io/

您是否在“设置”->“图片和声音”->“通知声音”->“声音”->“默认”中启用了通知声音?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM