android - How to Control Media Sound with Phonestatelistener in my Radio App -
can me implement phonestatelistener control media sound of app on incomming call , during call ? if calling , i`m listen radio station app, want mute current sound. best solution me "mute" sound during call , not stop.
so far i've found out need phonestatelistener , telephonymanager.
in androidmanifest.xml
<uses-permission android:name="android.permission.read_phone_state"/>
and
<intent-filter> <action android:name="android.intent.action.phone_state"/> </intent-filter>
what have , put myradio.java ?
package com.radiob.myradio; import android.app.activity; import android.app.alertdialog; import com.google.ads.*; import com.spoledge.aacplayer.aacfilechunkplayer; import com.spoledge.aacplayer.aacplayer; import com.spoledge.aacplayer.arrayaacplayer; import com.spoledge.aacplayer.arraydecoder; import com.spoledge.aacplayer.decoder; import com.spoledge.aacplayer.history; import com.spoledge.aacplayer.playercallback; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.content.serviceconnection; import android.media.audiomanager; import android.net.connectivitymanager; import android.os.bundle; import android.os.handler; import android.os.messagequeue.idlehandler; import android.util.log; import android.view.view; import android.widget.arrayadapter; import android.widget.autocompletetextview; import android.widget.button; import android.widget.edittext; import android.widget.progressbar; import android.widget.seekbar; import android.widget.textview; // main activity. public class myradio extends activity implements view.onclicklistener, playercallback { private history history; private autocompletetextview urlview; private button btnfaad2; private button btnffmpeg; private button btnopencore; private button btnmmswma; private button btnstop; private textview txtstatus; private textview txtplaystatus; private edittext txtbufaudio; private edittext txtbufdecode; private progressbar progress; private handler uihandler; private audiomanager maudiomanager; // decoder features private int dfeatures; private aacplayer aacplayer; private aacfilechunkplayer aacfilechunkplayer; //////////////////////////////////////////////////////////////////////////// // playercallback //////////////////////////////////////////////////////////////////////////// private boolean playerstarted; public void playerstarted() { uihandler.post( new runnable() { public void run() { txtbufaudio.setenabled( false ); txtbufdecode.setenabled( false ); btnfaad2.setenabled( false ); btnffmpeg.setenabled( false ); btnopencore.setenabled( false ); btnmmswma.setenabled( false ); btnstop.setenabled( true ); txtplaystatus.settext( r.string.text_buffering ); progress.setprogress( 0 ); progress.setvisibility( view.visible ); playerstarted = true; } }); } /** * method called periodically pcmfeed. * * @param isplaying false means pcm data being buffered, * audio not playing yet * * @param audiobuffersizems buffered audio data expressed in milliseconds of playing * @param audiobuffercapacityms total capacity of audio buffer expressed in milliseconds of playing */ public void playerpcmfeedbuffer( final boolean isplaying, final int audiobuffersizems, final int audiobuffercapacityms ) { uihandler.post( new runnable() { public void run() { progress.setprogress( audiobuffersizems * progress.getmax() / audiobuffercapacityms ); if (isplaying) txtplaystatus.settext( r.string.text_playing ); } }); } public void playerstopped( final int perf ) { uihandler.post( new runnable() { public void run() { enablebuttons(); btnstop.setenabled( false ); txtbufaudio.setenabled( true ); txtbufdecode.setenabled( true ); txtstatus.settext( r.string.text_stopped ); txtplaystatus.settext( "" + perf + " %" ); progress.setvisibility( view.invisible ); playerstarted = false; } }); } public void playerexception( final throwable t) { uihandler.post( new runnable() { public void run() { new alertdialog.builder( myradio.this ) .settitle( r.string.text_exception ) .setmessage( t.tostring()) .setneutralbutton( r.string.button_close, new dialoginterface.onclicklistener() { public void onclick( dialoginterface dialog, int id) { dialog.cancel(); } } ) .show(); txtstatus.settext( r.string.text_stopped ); if (playerstarted) playerstopped( 0 ); } }); } //////////////////////////////////////////////////////////////////////////// // onclicklistener //////////////////////////////////////////////////////////////////////////// /** * called when view has been clicked. */ public void onclick( view v ) { try { switch (v.getid()) { case r.id.view_main_button_faad2: start( decoder.decoder_faad2 ); txtstatus.settext( r.string.text_using_faad2 ); break; case r.id.view_main_button_ffmpeg: start( decoder.decoder_ffmpeg ); txtstatus.settext( r.string.text_using_ffmpeg ); break; case r.id.view_main_button_opencore: start( decoder.decoder_opencore ); txtstatus.settext( r.string.text_using_opencore ); break; case r.id.view_main_button_mmswma: start( decoder.decoder_ffmpeg_wma ); txtstatus.settext( r.string.text_using_mmswma ); break; /* case r.id.view_main_button_file: stop(); aacfilechunkplayer = new aacfilechunkplayer( geturl(), 8000, 8000 ); aacfilechunkplayer.start(); txtstatus.settext( r.string.text_using_file_chunks ); break; */ case r.id.view_main_button_stop: stop(); break; } } catch (exception e) { log.e( "myradio", "exc" , e ); } } //////////////////////////////////////////////////////////////////////////// // protected //////////////////////////////////////////////////////////////////////////// @override protected void oncreate( bundle savedinstancestate ) { super.oncreate( savedinstancestate ); setcontentview( r.layout.main ); // volume final audiomanager audiomanager = (audiomanager)getsystemservice(context.audio_service); int maxvolume = audiomanager.getstreammaxvolume(audiomanager.stream_music); int curvolume = audiomanager.getstreamvolume(audiomanager.stream_music); seekbar volcontrol = (seekbar)findviewbyid(r.id.volbar); volcontrol.setmax(maxvolume); volcontrol.setprogress(curvolume); volcontrol.setonseekbarchangelistener(new seekbar.onseekbarchangelistener() { public void onstoptrackingtouch(seekbar arg0) { // todo auto-generated method stub } public void onstarttrackingtouch(seekbar arg0) { // todo auto-generated method stub } public void onprogresschanged(seekbar arg0, int arg1, boolean arg2) { // todo auto-generated method stub audiomanager.setstreamvolume(audiomanager.stream_music, arg1, 0); } }); // btnfaad2 = (button) findviewbyid( r.id.view_main_button_faad2 ); btnffmpeg = (button) findviewbyid( r.id.view_main_button_ffmpeg ); btnopencore = (button) findviewbyid( r.id.view_main_button_opencore ); btnmmswma = (button) findviewbyid( r.id.view_main_button_mmswma ); //button b3 = (button) findviewbyid( r.id.view_main_button_file ); btnstop = (button) findviewbyid( r.id.view_main_button_stop ); urlview = (autocompletetextview) findviewbyid( r.id.view_main_edit_url ); txtstatus = (textview) findviewbyid( r.id.view_main_text_what ); txtplaystatus = (textview) findviewbyid( r.id.view_main_text_status ); txtbufaudio = (edittext) findviewbyid( r.id.view_main_text_bufaudio ); txtbufdecode = (edittext) findviewbyid( r.id.view_main_text_bufdecode ); progress = (progressbar) findviewbyid( r.id.view_main_progress ); txtbufaudio.settext( string.valueof( aacplayer.default_audio_buffer_capacity_ms )); txtbufdecode.settext( string.valueof( aacplayer.default_decode_buffer_capacity_ms )); btnfaad2.setonclicklistener( ); btnffmpeg.setonclicklistener( ); btnopencore.setonclicklistener( ); btnmmswma.setonclicklistener( ); //b3.setonclicklistener( ); btnstop.setonclicklistener( ); dfeatures = arraydecoder.getfeatures(); enablebuttons(); history = new history( ); history.read(); if (history.size() == 0 ) { history.addurl( "/sdcard/local/cro" ); } urlview.setadapter( history.getarrayadapter()); uihandler = new handler(); } @override protected void onpause() { super.onpause(); history.write(); } @override protected void ondestroy() { super.ondestroy(); stop(); } //////////////////////////////////////////////////////////////////////////// // private //////////////////////////////////////////////////////////////////////////// private void start( int decoder ) { stop(); aacplayer = new arrayaacplayer( arraydecoder.create( decoder ), this, getint( txtbufaudio ), getint( txtbufdecode )); aacplayer.playasync( geturl()); } private void stop() { if (aacfilechunkplayer != null) { aacfilechunkplayer.stop(); aacfilechunkplayer = null; } if (aacplayer != null) { aacplayer.stop(); aacplayer = null; } } private string geturl() { string ret = urlview.gettext().tostring(); history.addurl( ret ); return ret; } private void enablebuttons() { if ((dfeatures & decoder.decoder_faad2) != 0) btnfaad2.setenabled( true ); if ((dfeatures & decoder.decoder_ffmpeg) != 0) btnffmpeg.setenabled( true ); if ((dfeatures & decoder.decoder_opencore) != 0) btnopencore.setenabled( true ); if ((dfeatures & decoder.decoder_ffmpeg_wma) != 0) btnmmswma.setenabled( true ); } private int getint( edittext et ) { return integer.parseint( et.gettext().tostring()); } public void onreceive(context context, intent intent) { // todo auto-generated method stub } }
rather using phonestatelistener
, might instead use audiomanager.requestaudiofocus
. allow tell android using particular audio stream, , you'll callbacks whenever application wants play audio (like when phone receives phone call). then, can choose either stop playing, or pause until other application finished. example:
audiomanager.requestaudiofocus(new audiomanager.onaudiofocuschangelistener() { @override public void onaudiofocuschange(int reason) { switch (reason) { case audiomanager.audiofocus_loss: // lost focus, pause playback break; case audiomanager.audiofocus_gain: // gained focus, start playback break } } });
Comments
Post a Comment