Problems with MediaPlayer, raw resources, stop and start

前端 未结 7 734
天命终不由人
天命终不由人 2020-12-25 12:37

I\'m new to Android development and I have a question/problem.

I\'m playing around with the MediaPlayer class to reproduce some sounds/music. I am playing raw resou

7条回答
  •  借酒劲吻你
    2020-12-25 12:52

    this is how MediaPlayer.create method works to open a raw file:

        public static MediaPlayer create(Context context, int resid) {
             try {
                 AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
                 if (afd == null) return null;
    
                 MediaPlayer mp = new MediaPlayer();
                 mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                 afd.close();
                 mp.prepare();
                return mp;
            } catch (IOException ex) {
                Log.d(TAG, "create failed:", ex);
                // fall through
            } catch (IllegalArgumentException ex) {
                Log.d(TAG, "create failed:", ex);
               // fall through
            } catch (SecurityException ex) {
                Log.d(TAG, "create failed:", ex);
                // fall through
            }
             return null;
        }
    

提交回复
热议问题