Search code examples
javaandroidfilelistviewandroid-mediaplayer

MediaPlayer error when trying to play from list


I am trying to build a recorder app, MainActivity records, other activity is for a listview that shows all the records and the media player, something went wrong with the media player, it worked fine before I built the second activity and then started some native error, so I thought I will build the second activity and will fix it... still get some native error. please help me to fix it!

I loged the filePath, seems fine.

Log.d(filePath, "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: The path exist

my code (Records 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/[email protected]::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/[email protected]::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: error (1, -2147483648)

08-07 18:01:38.809 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: java.io.IOException: Prepare failed.: status=0x1 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: at android.media.MediaPlayer._prepare(Native Method) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: at android.media.MediaPlayer.prepare(MediaPlayer.java:1259) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: at com.example.tsuryohananov.voicerecorder.RecordsActivity$2.onClick(RecordsActivity.java:67) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: at android.view.View.performClick(View.java:6219) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: at android.view.View$PerformClick.run(View.java:24482) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: at android.os.Handler.handleCallback(Handler.java:769) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: at android.os.Handler.dispatchMessage(Handler.java:98) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: at android.os.Looper.loop(Looper.java:164) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6540) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: at java.lang.reflect.Method.invoke(Native Method) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)


Solution

  • So the reason were: the file named with DateTime, when the format were like that:

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

    the MediaPlayer gets an error for some reason.

    So I Changed it to:

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

    although I really prefer the hh:mm:ss