尝试从列表播放时出现 MediaPlayer 错误

MediaPlayer error when trying to play from list

我正在尝试构建一个录音机应用程序,MainActivity 记录,其他 activity 用于显示所有记录和媒体播放器的列表视图,媒体播放器出了点问题,在我构建第二个 activity 之前它运行良好,然后开始一些本机错误,所以我想我将构建第二个 activity 并修复它......仍然会出现一些本机错误。请帮助我修复它!

我记录了文件路径,似乎没问题。

Log.d(文件路径, "The path exist");

08-08 12:05:49.298 2801-2801/com.example.tsuryohananov.voicerecorder D//storage/emulated/0/Recordings/07-08-2017 06:37:15 PM.mp4: 路径存在

我的代码(记录Activity):

    public class RecordsActivity extends AppCompatActivity {

    Button play;
    static ListView recordList;
    ArrayAdapter<String> listAdapter;
    String recordToPlay;
    MediaPlayer mp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_records);

        mp = new MediaPlayer();

        play = (Button) findViewById(R.id.playbutton);
        recordList = (ListView) findViewById(R.id.list);

// Adapter with an ArrayList from the MainActivity that contains all the file names.

        listAdapter = new ArrayAdapter<String>(RecordsActivity.this, R.layout.support_simple_spinner_dropdown_item, MainActivity.listRecord);
        recordList.setAdapter(listAdapter);


        recordList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //int chosenPosition = position;
                recordToPlay = listAdapter.getItem(position);

//made that toast to check if the name of the file set properly, it looks like it is.

                Toast.makeText(getApplicationContext(), recordToPlay+ " Chosen", Toast.LENGTH_LONG).show();
                }
            });

            play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String filePath = Environment.getExternalStorageDirectory()+"/Recordings/"+recordToPlay;


                    try {
                        mp.setDataSource(filePath);
                        mp.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            //finalMp1.start();
                            mp.start();

                        }
                    });

                }
            });
        }
    }

logcat-

08-07 18:01:25.330 9157-9157/? I/zygote: Not late-enabling -Xcheck:jni (already on) 08-07 18:01:25.445 9157-9157/? W/zygote: Unexpected CPU variant for X86 using defaults: x86 08-07 18:01:25.822 9157-9157/com.example.tsuryohananov.voicerecorder I/InstantRun: starting instant run server: is main process 08-07 18:01:26.392 9157-9206/com.example.tsuryohananov.voicerecorder D/OpenGLRenderer: HWUI GL Pipeline

[ 08-07 18:01:26.423 9157: 9206 D/ ] HostConnection::get() New Host Connection established 0xa3d645c0, tid 9206 08-07 18:01:26.655 9157-9206/com.example.tsuryohananov.voicerecorder I/OpenGLRenderer: Initialized EGL, version 1.4 08-07 18:01:26.655 9157-9206/com.example.tsuryohananov.voicerecorder D/OpenGLRenderer: Swap behavior 1 08-07 18:01:26.655 9157-9206/com.example.tsuryohananov.voicerecorder W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 08-07 18:01:26.655 9157-9206/com.example.tsuryohananov.voicerecorder D/OpenGLRenderer: Swap behavior 0 08-07 18:01:26.657 9157-9206/com.example.tsuryohananov.voicerecorder D/EGL_emulation: eglCreateContext: 0xa139c420: maj 2 min 0 rcv 2 08-07 18:01:26.658 9157-9206/com.example.tsuryohananov.voicerecorder D/EGL_emulation: eglMakeCurrent: 0xa139c420: ver 2 0 (tinfo 0xa265dad0) 08-07 18:01:26.664 9157-9206/com.example.tsuryohananov.voicerecorder W/android.hardware.graphics.mapper@2.0::Mapper: getService: found null hwbinder interface 08-07 18:01:26.667 9157-9206/com.example.tsuryohananov.voicerecorder I/vndksupport: sphal namespace is not configured for this process. Loading /system/lib/hw/gralloc.ranchu.so from the current namespace instead. 08-07 18:01:26.718 9157-9206/com.example.tsuryohananov.voicerecorder D/EGL_emulation: eglMakeCurrent: 0xa139c420: ver 2 0 (tinfo 0xa265dad0) 08-07 18:01:34.512 9157-9206/com.example.tsuryohananov.voicerecorder D/EGL_emulation: eglMakeCurrent: 0xa139c420: ver 2 0 (tinfo 0xa265dad0) 08-07 18:01:34.535 9157-9183/com.example.tsuryohananov.voicerecorder I/zygote: Background concurrent copying GC freed 4737(843KB) AllocSpace objects, 0(0B) LOS objects, 64% free, 855KB/2MB, paused 10.284ms total 110.547ms 08-07 18:01:34.558 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: Do partial code cache collection, code=13KB, data=21KB 08-07 18:01:34.558 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: After code cache collection, code=13KB, data=21KB 08-07 18:01:34.558 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: Increasing code cache capacity to 128KB 08-07 18:01:34.563 9157-9206/com.example.tsuryohananov.voicerecorder D/EGL_emulation: eglMakeCurrent: 0xa139c420: ver 2 0 (tinfo 0xa265dad0) 08-07 18:01:34.565 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: Do partial code cache collection, code=13KB, data=38KB 08-07 18:01:34.566 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: After code cache collection, code=13KB, data=38KB 08-07 18:01:34.567 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: Increasing code cache capacity to 256KB 08-07 18:01:34.567 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: JIT allocated 72KB for compiled code of void android.widget.TextView.(android.content.Context, android.util.AttributeSet, int, int) 08-07 18:01:34.589 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: Compiler allocated 4MB to compile void android.widget.TextView.(android.content.Context, android.util.AttributeSet, int, int) 08-07 18:01:34.624 9157-9206/com.example.tsuryohananov.voicerecorder D/EGL_emulation: eglMakeCurrent: 0xa139c420: ver 2 0 (tinfo 0xa265dad0) 08-07 18:01:34.642 9157-9206/com.example.tsuryohananov.voicerecorder I/chatty: uid=10093(u0_a93) RenderThread identical 1 line 08-07 18:01:34.692 9157-9206/com.example.tsuryohananov.voicerecorder D/EGL_emulation: eglMakeCurrent: 0xa139c420: ver 2 0 (tinfo 0xa265dad0) 08-07 18:01:34.698 9157-9206/com.example.tsuryohananov.voicerecorder D/OpenGLRenderer: endAllActiveAnimators on 0x92173c00 (RippleDrawable) with handle 0x921263f0 08-07 18:01:35.380 9157-9206/com.example.tsuryohananov.voicerecorder D/EGL_emulation: eglMakeCurrent: 0xa139c420: ver 2 0 (tinfo 0xa265dad0) 08-07 18:01:36.248 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: Do full code cache collection, code=125KB, data=69KB 08-07 18:01:36.253 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: After code cache collection, code=123KB, data=54KB 08-07 18:01:36.934 9157-9206/com.example.tsuryohananov.voicerecorder D/EGL_emulation: eglMakeCurrent: 0xa139c420: ver 2 0 (tinfo 0xa265dad0) 08-07 18:01:36.958 9157-9206/com.example.tsuryohananov.voicerecorder D/RenderScript HIDL Adaptation: IRenderScriptDevice::getService() 08-07 18:01:36.962 9157-9206/com.example.tsuryohananov.voicerecorder W/android.hardware.renderscript@1.0::Device: getService: found null hwbinder interface 08-07 18:01:36.963 9157-9206/com.example.tsuryohananov.voicerecorder D/RenderScript HIDL Adaptation: IRenderScriptDevice::getService() returned 0x0 08-07 18:01:36.963 9157-9206/com.example.tsuryohananov.voicerecorder D/RenderScript HIDL Adaptation: Using Fallback Path. 08-07 18:01:36.975 9157-9206/com.example.tsuryohananov.voicerecorder D/RenderScript: Successfully queried cache dir: /data/user_de/0/com.example.tsuryohananov.voicerecorder/code_cache 08-07 18:01:36.976 9157-9206/com.example.tsuryohananov.voicerecorder D/RenderScript: Setting cache dir: /data/user_de/0/com.example.tsuryohananov.voicerecorder/code_cache 08-07 18:01:36.985 9157-9206/com.example.tsuryohananov.voicerecorder D/EGL_emulation: eglMakeCurrent: 0xa139c420: ver 2 0 (tinfo 0xa265dad0) 08-07 18:01:38.488 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: Do partial code cache collection, code=124KB, data=67KB 08-07 18:01:38.488 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: After code cache collection, code=124KB, data=67KB 08-07 18:01:38.488 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote: Increasing code cache capacity to 512KB

08-07 18:01:38.805 9157-9324/com.example.tsuryohananov.voicerecorder E/MediaPlayerNative: 错误 (1, -2147483648)

08-07 18:01:38.809 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: java.io.IOException: 准备失败。: status=0x1 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err:在 android.media.MediaPlayer._prepare(本机方法) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.错误:在 android.media.MediaPlayer.prepare(MediaPlayer.java:1259) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.错误:在 com.example.tsuryohananov.voicerecorder.记录Activity$2.onClick(RecordsActivity.java:67) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: 在 android.view.View.performClick(View.java:6219) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.错误:在 android.view.View$PerformClick.run(View.java:24482) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorderW/System.错误:在 android.os.Handler.handleCallback(Handler.java:769) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorderW/System.错误:在 android.os.Handler.dispatchMessage(Handler.java:98) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorderW/System.错误:在 android.os.Looper.loop(Looper.java:164) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorderW/System.错误:在 android.app.ActivityThread.main(ActivityThread.java:6540) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err:在 java.lang.reflect.Method.invoke(本机方法) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.错误:在 com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.错误:在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

我想你必须使用 recordToPlay = MainActivity.listRecord.get(position);

所以原因是: 以DateTime命名的文件,当格式是这样的:

String dateTime = new SimpleDateFormat("dd-MM-yyyy  hh:mm:ss aa", Locale.getDefault()).format(new Date());

MediaPlayer 由于某种原因出错。

所以我将其更改为:

String dateTime = new SimpleDateFormat("dd-MM-yyyy  hh-mm-ss aa", Locale.getDefault()).format(new Date());

虽然我更喜欢hh:mm:ss