我想在“警报”对话框中添加媒体播放器控件

时间:2015-09-15 10:42:57

标签: android android-mediaplayer alertdialog

我正在开发一个项目,该项目需要在对话框的onClick选项中播放,暂停或停止背景文本到语音。目前它在listview onclick之后正常播放音频,但我无法弄清楚如何暂停或停止正在进行的音频。这是MainActivity代码:

    listViewSMS.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0,
                View v, int position, long arg3) {
            // when user clicks on ListView Item , onItemClick is called
            // with position and View of the item which is clicked
            // we can use the position parameter to get index of clicked item


            TextView textViewSMSSender = (TextView) v.findViewById(R.id.textViewSMSSender);
            TextView textViewSMSBody = (TextView) v.findViewById(R.id.textViewMessageBody);

            smsSender = textViewSMSSender.getText().toString();//to get the sender in string
            smsBody = textViewSMSBody.getText().toString();// content of the sms into string


            // Show The Dialog with Selected SMS  when particular sms selected
            AlertDialog dialog = new AlertDialog.Builder(context).create();
            dialog.setTitle("SMS From : " + smsSender);
            dialog.setIcon(R.drawable.iltts);
            dialog.setMessage(smsBody);

            //Dialog box button click event to read the selected sms from the shown list
            dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Play",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            Log.d("send", smsSender);
                            Log.d("body", smsBody + " " + spinnerValue);

                            //***************############################################28112013
                            //online part added

                            MyTask myTask = new MyTask();
                            myTask.execute(smsBody);

                            //#############################################################

                            return;
                        }
                    });

            dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Pause", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {

                            //Log.d("send", smsSender);
                            //Log.d("body", smsBody + " " + spinnerValue);

                            //***************############################################28112013
                            //online part added
                            if(mediaPlayer.isPlaying()){
                            mediaPlayer.pause();}
                            //#############################################################

                            return;
                        }
                    }
            );
            dialog.setButton(DialogInterface.BUTTON_NEUTRAL,"Stop",new DialogInterface.OnClickListener()
            {
                        public void onClick(DialogInterface dialog, int which){
                            mediaPlayer.stop();
                            mediaPlayer.release();
                        }
                    }
            );



            dialog.show();

        }
    });
}

@Override
public void onInit(int status) {

}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

//ToGo to text to speech engine selection
public void goPreferences(View v)
{
    //f=2;

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();

}
//Online part with Asynctask ########################################################################################################

private class MyTask extends AsyncTask<String, Void, String>{

    @Override
    protected void onPreExecute() {
        pd=ProgressDialog.show(MainActivity.this,"","Please Wait",false); 

    }


    protected String doInBackground(String... params) {

        //Some calculations...

        String nt=params[0];        

        lang=spinnerValue.toLowerCase();

        //for bengali hts 
        if(lang.equals("bengali")){
            lang="hts"+lang;
            Log.d("bengali language test ********************* ", lang);

        }
        //for gujarati hts
        else if(lang.equals("gujarati")){
            lang="hts"+lang+"_male";
            Log.d("gujarati language test ********************* ", lang);

        }

        //for kannada hts
        else if(lang.equals("kannada")){
            lang="hts"+lang+"_female";
            Log.d("kannada language test ********************* ", lang);

        }

        /* <item>English_Hindi</item>                      
            <item>English_Tamil</item>        
            <item>English_Telugu</item> */

        else if(lang.equals("english_hindi")){
            lang="htsenglish";
            Log.d("English (hindi) language test ********************* ", lang);

        }
        //htstelugueng
        else if(lang.equals("english_telugu")){
            lang="htstelugueng";
            Log.d("English (Telugu) language test ********************* ", lang);

        }
        else if(lang.equals("english_tamil")){
            lang="htstamileng";
            Log.d("English (Tamil) language test ********************* ", lang);
        }
        else{
            Log.d(" language test ********************* ", lang);
        }
        //rate
        speed=rateselected.toLowerCase();
        vc="";
        Log.d("language selected now ", lang);
        //PART1.1
        //link="http://tts.cdacmumbai.in/festival_cs_apps.php";   //amr type v.1.0
        //link="http://202.141.151.29/festival_cs_apps.php"; 
        //link="http://iltts.in/festival_cs_apps.php";//2014 Sep 11 v.1.1
        link="http://tdil-dc.in/tts/festival_cs_apps.php";//2015 march 14 july 02 v1.4
        Log.d("Value of lang" , spinnerValue);
        //generating random number
        rannum=Math.random();
        Log.d("Randum number generated ", Double.toString(rannum));
        count=Double.toString(rannum);
        Log.d("body from params0", nt);
        Log.d("body of smsBody", smsBody);
        Log.d("desired url", link);
        Log.d("counter", count);

        try{

            // Creating HTTP client
            HttpClient httpClient = new DefaultHttpClient();

            // Creating HTTP Post
            HttpPost httpPost = new HttpPost(link);

            //Name value pair created what you want to send to server
            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
            nameValuePair.add(new BasicNameValuePair("Languages",lang));
            nameValuePair.add(new BasicNameValuePair("voice",vc));
            nameValuePair.add(new BasicNameValuePair("ex","execute"));
            nameValuePair.add(new BasicNameValuePair("op",smsBody));
            nameValuePair.add(new BasicNameValuePair("count",count));
            nameValuePair.add(new BasicNameValuePair("speed",speed));
            //httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "UTF-8")); // UTF 8 added to send indic string to server
            HttpResponse httpResponse = httpClient.execute(httpPost);

            final int statusCode = httpResponse.getStatusLine().getStatusCode();
            Log.d("Response Status Code",String.valueOf(statusCode));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        //Part1.2

        //link2="http://tts.cdacmumbai.in/wav_output/"; 

        //link2="http://202.141.151.29/wav_output/"; 

        //link2="http://iltts.in/wav_output/";//2014 Sep 11 v1.1
        link2="http://tdil-dc.in/tts/wav_output/";//2015 March 14  July 02 v 1.4
        //file type
        file2download=link2+"fest_out"+count+".amr";
        Log.d("Now Listen this ",file2download);
        return file2download;
    }
    protected void onPostExecute(String result) {

        //Log.d("Listen file in postexecute   check and enjoy*********************", result);

        pd.dismiss();
        String url = result;// your URL here
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(url);             

        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            mediaPlayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // might take long! (for buffering, etc)
        mediaPlayer.start();
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                // TODO Auto-generated method stub
                mp.release();
            }
        });

    }//post execute ends

}//MyTask  class ends

} // MainActivity结束

0 个答案:

没有答案