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

Posted

技术标签:

【中文标题】尝试从列表播放时出现 MediaPlayer 错误【英文标题】:MediaPlayer error when trying to play from list 【发布时间】:2018-01-14 23:17:56 【问题描述】:

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

我记录了filePath,看起来不错。

Log.d(filePath, "路径存在");

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:路径存在

我的代码(记录活动):

    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: 不延迟启用 -Xcheck:jni (已经开启)08-07 18:01:25.445 9157-9157/? W/zygote:意外的 CPU 使用默认值的 X86 变体:x86 08-07 18:01:25.822 9157-9157/com.example.tsuryohananov.voicerecorder I/InstantRun: 启动即时运行服务器:是主进程 08-07 18:01:26.392 9157-9206/com.example.tsuryohananov.voicerecorder D/OpenGLRenderer: HWUI GL 管道

[08-07 18:01:26.423 9157:9206 D/] HostConnection::get() 新主机连接建立 0xa3d645c0, tid 9206 08-07 18:01:26.655 9157-9206/com.example.tsuryohananov.voicerecorder I/OpenGLRenderer: 初始化 EGL,版本 1.4 08-07 18:01:26.655 9157-9206/com.example.tsuryohananov.voicerecorder D/OpenGLRenderer: 交换行为 1 08-07 18:01:26.655 9157-9206/com.example.tsuryohananov.voicerecorder W/OpenGLRenderer: 无法使用 EGL_SWAP_BEHAVIOR_PRESERVED 选择配置,正在重试 没有... 08-07 18:01:26.655 9157-9206/com.example.tsuryohananov.voicerecorder D/OpenGLRenderer: 交换行为 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: 版本 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: 发现空 hwbinder 接口 08-07 18:01:26.667 9157-9206/com.example.tsuryohananov.voicerecorder I/vndksupport:shal 没有为此进程配置命名空间。正在加载 /system/lib/hw/gralloc.ranchu.so 来自当前命名空间。 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: 版本 2 0 (tinfo 0xa265dad0) 08-07 18:01:34.535 9157-9183/com.example.tsuryohananov.voicerecorder I/zygote:后台并发复制 GC 释放 4737(843KB) AllocSpace 对象,0(0B) 个 LOS 对象,64% 空闲,855KB/2MB,暂停 10.284ms 总计 110.547ms 08-07 18:01:34.558 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote:做部分 代码缓存集合,代码=13KB,数据=21KB 08-07 18:01:34.558 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote:代码后 缓存集合,代码=13KB,数据=21KB 08-07 18:01:34.558 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote:增加 代码缓存容量为 128KB 08-07 18:01:34.563 9157-9206/com.example.tsuryohananov.voicerecorder D/EGL_emulation: eglMakeCurrent: 0xa139c420: 版本 2 0 (tinfo 0xa265dad0) 08-07 18:01:34.565 9157-9168/com.example.tsuryohananov.voicerecorder i/zygote:做部分代码缓存收集,代码=13KB,数据=38KB 08-07 18:01:34.566 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote:代码缓存收集后,code=13KB,data=38KB 08-07 18:01:34.567 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote:将代码缓存容量增加到 256KB 08-07 18:01:34.567 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote:JIT 为 void 的编译代码分配 72KB 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:编译器 分配 4MB 来编译 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: 版本 2 0 (tinfo 0xa265dad0) 08-07 18:01:34.642 9157-9206/com.example.tsuryohananov.voicerecorder I/chatty: uid=10093(u0_a93) RenderThread 相同 1 行 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: 0x92173c00 (RippleDrawable) 上的 endAllActiveAnimators 带句柄 0x921263f0 08-07 18:01:35.380 9157-9206/com.example.tsuryohananov.voicerecorder D/EGL_emulation: eglMakeCurrent: 0xa139c420: 版本 2 0 (tinfo 0xa265dad0) 08-07 18:01:36.248 9157-9168/com.example.tsuryohananov.voicerecorder i/zygote:做全码缓存收集,code=125KB,data=69KB 08-07 18:01:36.253 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote:代码缓存收集后,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 适配: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 接口 08-07 18:01:36.963 9157-9206/com.example.tsuryohananov.voicerecorder D/RenderScript HIDL 适配:IRenderScriptDevice::getService() 返回 0x0 08-07 18:01:36.963 9157-9206/com.example.tsuryohananov.voicerecorder D/RenderScript HIDL 适配:使用后备路径。 08-07 18:01:36.975 9157-9206/com.example.tsuryohananov.voicerecorder D/RenderScript:成功查询缓存目录: /data/user_de/0/com.example.tsuryohananov.voicerecorder/code_cache 08-07 18:01:36.976 9157-9206/com.example.tsuryohananov.voicerecorder D/RenderScript:设置缓存目录: /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:做部分 代码缓存集合,代码=124KB,数据=67KB 08-07 18:01:38.488 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote:代码后 缓存集合,代码=124KB,数据=67KB 08-07 18:01:38.488 9157-9168/com.example.tsuryohananov.voicerecorder I/zygote:增加 代码缓存容量为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: Prepare failed.: 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.err:在 android.media.MediaPlayer.prepare(MediaPlayer.java:1259) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W / System.err:在 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:在 android.view.View.performClick(View.java:6219) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W / System.err:在 android.view.View$PerformClick.run(View.java:24482) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err:在 android.os.Handler.handleCallback(Handler.java:769) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err:在 android.os.Handler.dispatchMessage(Handler.java:98) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err:在 android.os.Looper.loop(Looper.java:164) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W/System.err:在 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(Native Method) 08-07 18:01:38.810 9157-9157/com.example.tsuryohananov.voicerecorder W / System.err:在 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:在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

【问题讨论】:

这样做了(我认为):) 登录filePath查看路径是否存在 现在尝试在手机上(xperia z5 premium)这是新的,只是进入唱片活动,这在logcat-08-07 18:42:11.827 17751-17751 / com.example上.tsuryohananov.voicerecorder E/ExtMediaPlayer-JNI: QCMediaPlayer 无法定位.... 08-07 18:42:11.827 17751-17751/com.example.tsuryohananov.voicerecorder E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0 08- 07 18:42:11.827 17751-17751/com.example.tsuryohananov.voicerecorder E/ExtMediaPlayer-JNI: QCMediaPlayer 无法定位.... 08-07 18:42:11.827 17751-17751/com.example.tsuryohananov.voicerecorder E/MediaPlayer-JNI: JNIMediaPlayerFactory: bIsQCMediaPlayerPresent 0 我登录了filePath,看起来不错。 Log.d(filePath, "路径存在"); 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:路径存在 【参考方案1】:

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

【讨论】:

同样的错误 08-07 18:37:50.333 27580-27593/com.example.tsuryohananov.voicerecorder E/MediaPlayerNative: error (1, -2147483648)【参考方案2】:

所以原因是: 以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

【讨论】:

以上是关于尝试从列表播放时出现 MediaPlayer 错误的主要内容,如果未能解决你的问题,请参考以下文章

使用 Android MediaPlayer 播放声音时出现问题

尝试使用 Spotify Web Api 将歌曲添加到另一个协作播放列表时出现 403 禁止错误

尝试使用其 API 在 Spotify 中创建播放列表时出现“请求失败,状态码为 401”错误

使用 Spotipy 将曲目添加到播放列表时出现多个错误

尝试播放某些曲目时出现 cocoalibspotify 错误

MediaPlayer不使用本地文件