Callback for Audio Focus #268

Closed
Garoe opened this Issue Oct 22, 2012 · 9 comments

Comments

Projects
None yet
3 participants

Garoe commented Oct 22, 2012

Hi,
I have been experimenting with the Android sound API, and I got this sample code that
starts playing a song. The Android developer guide recommends to use the audio focus
to avoid an application to continue playing while it is in background.
http://developer.android.com/guide/topics/media/mediaplayer.html#audiofocus
According to the tutorial the interface is onAudioFocusChange and it is registered through requestAudioFocus. The song starts playing nicely and stops when the screen in touched.
But the issue here is that onAudiofocusChange is only called once and never again.
Any ideas on why is this happening?

Thanks

require 'ruboto/activity'

java_import "android.media.MediaPlayer"
java_import "android.media.AudioManager"
java_import "android.net.Uri"          

  class AudioFocus
      def onAudioFocusChange focusChange
              puts "On focus change #{focusChange}"
              nil
      end

      def toString
              self.class.to_s
      end    
  end

$activity.start_ruboto_activity "$glsurface" do

  def on_create(bundle)    
        java_file = java.io.File.new("/mnt/sdcard/jruby/media/Engel.mp3")
        uri = Uri.fromFile(java_file)
        @player = MediaPlayer.create(self, uri);

        @listener = AudioFocus.new
        context = $activity.getApplicationContext
        audio_manager = context.getSystemService(Context::AUDIO_SERVICE)

        focus = audio_manager.requestAudioFocus(@listener, AudioManager::STREAM_MUSIC, AudioManager::AUDIOFOCUS_GAIN)         
        @player.start
        rescue Exception => e
         puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
  end

  def on_touch_event event
        @player.stop
        return true
  end        

end

PD. I had the same problem with on_touch_event, but that was because I forgot the return true line.

@ghost ghost assigned donv Dec 7, 2012

Owner

donv commented Dec 7, 2012

Sorry for the late response. Did you solve this?

Garoe commented Dec 8, 2012

No problem, I've seen that you have been quite busy with the new releases of ruboto. Sadly I have not yet been able to solve the issue.

Owner

donv commented Mar 4, 2013

@neochuky postponing this to Ruboto 0.10.3 to get 0.10.2 our the door.

Owner

donv commented Jun 1, 2013

Hi @neochuky !

Looking at this now. Is it still a problem?

Owner

donv commented Jun 1, 2013

Hi @neochuky !

I updated the example a little:

java_import 'android.media.MediaPlayer'
java_import 'android.media.AudioManager'
java_import 'android.net.Uri'

class AudioFocusActivity
  def on_create(bundle)    
    super

    java_file = java.io.File.new('/mnt/sdcard/jruby/media/Engel.mp3')
    uri = Uri.fromFile(java_file)
    @player = MediaPlayer.create(self, uri)

    @listener = AudioFocus.new
    context = getApplicationContext
    audio_manager = context.getSystemService(Context::AUDIO_SERVICE)

    audio_manager.requestAudioFocus(@listener, AudioManager::STREAM_MUSIC, AudioManager::AUDIOFOCUS_GAIN)
    @player.start
  rescue Exception => e
    puts "#{ e } (#{ e.class } #{e.message} #{e.backtrace.inspect} )!"
  end

  def on_touch_event(event)
    @player.stop
    true
  end        

end

class AudioFocus
  def onAudioFocusChange focusChange
    puts "On focus change #{focusChange}"
    nil
  end

  def toString
    self.class.to_s
  end    
end

As far as I can see, the app behaves as it should. You get a callback when you loose audio focus, but you need to request it again if you want it back.

Garoe commented Jun 2, 2013

I am not sure what is happening now but onFocusChange never executes on my device. I'll do more testing to try to find out why, anyway this is not critical.

Owner

donv commented Jun 2, 2013

Do you think Ruboto is doing something wrong, or that you are using AudioManager wrongly, maybe? If you don't think Ruboto is doing something wrong, I'd like to close this issue.

Owner

rscottm commented Jun 3, 2013

It should work, but just for the sake of it, try just sending a proc
instead of an instance. When an interface just has a single method, using a
proc simplifies things greatly.

@Listener = proc{|i| }
On Jun 2, 2013 11:58 AM, "Garoe" notifications@github.com wrote:

I am not sure what is happening now but onFocusChange never executes on my
device. I'll do more testing to try to find out why, anyway this is not
critical.


Reply to this email directly or view it on GitHubhttps://github.com/ruboto/ruboto/issues/268#issuecomment-18811625
.

Garoe commented Jun 3, 2013

Using proc seems to do the trick, I close the issue then. Thanks

@Garoe Garoe closed this Jun 3, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment