Android无法使用媒体播放器

时间:2016-03-10 09:32:42

标签: android android-mediaplayer text-to-speech

我想从SD卡获取mp3文件并使用MediaPlayer播放。下面是我的代码,但使用此代码声音文件仅在调试模式下播放。在运行模式下,它在logcat中显示以下错误。

错误

    03-10 14:52:13.769 23417-23590/com.spec.texttospeech E/MediaPlayer: start called in state 4
    03-10 14:52:13.769 23417-23590/com.spec.texttospeech E/MediaPlayer: error (-38, 0)
    03-10 14:52:13.772 23417-23417/com.spec.texttospeech E/MediaPlayer: Error (-38,0)
    03-10 14:52:13.775 23417-23448/com.spec.texttospeech E/MediaPlayer: error (1, -2147483648)
    03-10 14:52:13.779 23417-23417/com.spec.texttospeech E/MediaPlayer: Error (1,-2147483648)
03-10 15:01:02.651 30662-30679/com.spec.texttospeech E/MediaPlayer: error (1, -2147483648)
03-10 15:01:02.652 30662-30662/com.spec.texttospeech W/System.err: java.io.IOException: Prepare failed.: status=0x1
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at android.media.MediaPlayer._prepare(Native Method)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at android.media.MediaPlayer.prepare(MediaPlayer.java:1135)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at com.spec.texttospeech.view.FilesActivity.initializeMediaPlayer(FilesActivity.java:287)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at com.spec.texttospeech.view.FilesActivity.readSDCardFile(FilesActivity.java:257)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at com.spec.texttospeech.view.FilesActivity.onOptionsItemSelected(FilesActivity.java:135)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at android.app.Activity.onMenuItemSelected(Activity.java:2885)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:68)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:172)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760)
03-10 15:01:02.653 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at android.view.View.performClick(View.java:4780)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at android.view.View$PerformClick.run(View.java:19866)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at android.os.Looper.loop(Looper.java:135)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5254)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
03-10 15:01:02.654 30662-30662/com.spec.texttospeech W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

代码

    private void initializeMediaPlayer(final String fileName) {
            String filePath = Environment.getExternalStorageDirectory() + "/Sounds/" + fileName + ".mp3";
            Uri uri = Uri.parse("file:///" + filePath);

            try {
                MediaPlayer mediaPlayer = new MediaPlayer();
                mediaPlayer.setDataSource(filePath);
                mediaPlayer.prepare();
                mediaPlayer.start();

                }catch(Exception e){
                 e.printStackTrace();
                }
         }

1 个答案:

答案 0 :(得分:1)

- 试试我的代码..它的工作

 int soundsrc[] = {R.raw.a, R.raw.b, R.raw.c, R.raw.d};
    int count = 0; 
    TextToSpeech txtToSpeech1;
    MediaPlayer mp;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setContentView(R.layout.activity_animals_names);
            stopService(new Intent(this, PlayMusic.class));


      Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    animalSounds();
                }
            }, 1000);

     }


    @Override
        public void onDestroy() {
            // Don't forget to shutdown!
            if (txtToSpeech1 != null) {
                txtToSpeech1.stop();
                txtToSpeech1.shutdown();

            }
            super.onDestroy();
        }

        public void onPause() {
            if (txtToSpeech1 != null) {
                txtToSpeech1.stop();
                txtToSpeech1.shutdown();

            }
            super.onPause();
        }


        public void Sounds() {
            mp = MediaPlayer.create(Names.this, soundsrc[count]);
            mp.start();
        }

     public void forwardd(View v) {


            buttonSounds(R.raw.multimedia_button_click);
            if (count < imgArr.length && count >= 0) {
                count++;
                if (count == imgArr.length)
                    count = 0;


                if (txtToSpeech1.isSpeaking()) {
                    Log.i("Tag", "Stop speaking");
                    txtToSpeech1.stop();
                }




                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    public void run() {
                       Sounds();
                    }
                }, 1000);




                }
            }
        }