应用程序无法播放音频文件

App won't play audio file

我正在尝试制作一个应用程序来播放保存在原始文件中的音频文件。我需要应用程序通过音乐扬声器播放音调,然后才能通过听筒播放。现在它没有播放任何东西,我无法弄清楚为什么,即使经过数小时的研究。有人请帮忙!!!

public class RightSpeaker3 extends Activity {
        int count = 0; //Keeps a count of the number of times the audio tone is played
        int counter = 1; //Keeps a track of number of timer the audio playback occurs
        int default_mode; //Saves the default mode of the device
        int music_volume; //Saves the default volume of the music stream
        int call_volume; //Saves the default volume of the in call stream
        String device_type = null;//Stores the device type
        MediaRecorder speaker_recorder = null; //Recorder object used to record the audio tone
        MediaPlayer mPlayer = null; //Media object which has the playback control of audio and video files
        String path = null; //Stores the path of the media files that is been recorded
        AudioManager audioManager; //Object to provide access to system volume controls and settings
        String model = android.os.Build.MODEL;
        String Manufacturer = android.os.Build.MANUFACTURER;
        boolean isNexus = false;
        TextView title_text;
        MediaPlayer mp;
        public final static String log_tag = "RightSpeaker";
        Handler mHandler = new Handler();
        /*
        * (non-Javadoc)
        * @see android.app.Activity#onCreate(android.os.Bundle).
        * First function entered when the application is created
        * This function is used to initialize the layout and the device type
        * based on the model and device Manufacturer.This function also stores
        * the default audio stream value and modes to reset it back to these
        * values once we exit the application
        */
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            /*
             * API's to launch the application when the tablet is locked or
             * display is turned off
             */
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
            setContentView(R.layout.activity_right_speaker);


            title_text = ((TextView) findViewById(R.id.textView3));
            title_text.setTextColor(Color.RED);
            title_text.setText("SPEAKER AND MIC TESTING IN PROGRESS" + "\n" + "FOR RIGHT SPEAKER AT 40% VOLUME");
            Context mContext = getApplicationContext();
            createTempFile("Status.txt", "PROGRESS");

            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            default_mode = audioManager.getMode();
            music_volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
            call_volume = audioManager.getStreamVolume(AudioManager.STREAM_VOICE_CALL);

            //Setting the volume level
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 10, AudioManager.FLAG_SHOW_UI);
            audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, 10, AudioManager.FLAG_SHOW_UI);
            audioManager.setSpeakerphoneOn(false);

            start_playing();

        }





                /*
         * Function to start playing the audio tone.
         */
        private void start_playing() {
            audioManager.setMode(AudioManager.MODE_IN_CALL);

            //Set the speakerPhone to ON
            audioManager.setSpeakerphoneOn(false);

            //Obtain the tone which is to be played. This tone is present in the application raw directory
            mp = MediaPlayer.create(getApplicationContext(), R.raw.tone_right);

            mp.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);


            mp.start();

            mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                    /*
                     * (non-Javadoc)
                     * @see android.media.MediaPlayer.OnCompletionListener#onCompletion(android.media.MediaPlayer)
                     * Since the tone played is a short one we play the tone three times totally
                     */
                public void onCompletion(MediaPlayer mp) {
                    mHandler.postDelayed(new Runnable() {
                        public void run() {
                            exit_function();
                        }
                    }, 10000);
                }
            });

        }

        private void exit_function() {
            if (mp != null) {
                mp.release();
                mp = null;
            }
            onDestroy();
        }

        @Override
        /*
         * (non-Javadoc)
         * @see android.app.Activity#onDestroy()
         * Function invoked before we exit the application . Reset all the volume
         * and stream values in this function
         */
        protected void onDestroy() {
            Log.i(log_tag,"Entered onDestroy()");
            super.onDestroy();
            //mp.reset();
            if (mp != null) {
                mp.release();
            }
            if (mPlayer != null) {
                mPlayer.release();
            }
            if (speaker_recorder != null) {
                speaker_recorder.release();
            }
            //Reset to the default settings here
            audioManager.setMode(default_mode);

            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, music_volume, AudioManager.FLAG_SHOW_UI);
            audioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, call_volume, AudioManager.FLAG_SHOW_UI);

            this.finish();

        }

我已经试过了并且有效

private void playSound(boolean speakers){
    mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.pop_tone);
    if (speakers){
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }else {
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
    }
    mMediaPlayer.start();
    mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            // Whatever you do after media source has completed
        }
    });
}

调用方法 true 通过扬声器播放,false 通过听筒播放。

灵感来自 THIS POST