Problems with MediaPlayer, raw resources, stop and start

前端 未结 7 683
天命终不由人
天命终不由人 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;
        }
    
    0 讨论(0)
  • 2020-12-25 12:58

    Recheck your passing parameters not null

    Possible reasons

    1. Context may be null
    2. Your media file may be corrupted
    0 讨论(0)
  • 2020-12-25 12:59

    You can use

    mp.pause();
    mp.seekTo(0);
    

    to stop music player.

    0 讨论(0)
  • 2020-12-25 13:05

    Finally, the way it works for me:

    public class MainStart extends Activity {
    
        ImageButton buttonImage;
        MediaPlayer mp;
        Boolean playing = false;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main);
    
            buttonImage = (ImageButton)findViewById(R.id.ButtonID);
    
    
            buttonImage.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
    
                    if(playing){
                        mp.stop();
                        playing = false;
                    }else{
                        mp = MediaPlayer.create(getApplicationContext(), R.raw.sound_u_want);
                        mp.start();
                        playing = true;
                    }
                }
            });
        }
    }
    
    0 讨论(0)
  • 2020-12-25 13:06

    Here is what I did to load multiple resources with a single MediaPlayer:

    /**
     * Play a sample with the Android MediaPLayer.
     *
     * @param resid Resource ID if the sample to play.
     */
    private void playSample(int resid)
    {
        AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
    
        try
        {   
            mediaPlayer.reset();
            mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
            mediaPlayer.prepare();
            mediaPlayer.start();
            afd.close();
        }
        catch (IllegalArgumentException e)
        {
            Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
        }
        catch (IllegalStateException e)
        {
            Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
        }
        catch (IOException e)
        {
            Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e);
        }
    

    mediaPlay is a member variable that get created and released at other points in the class. This may not be the best way (I am new to Android myself), but it seems to work. Just note that the code will probably fall trough to the bottom of the method before the mediaPlayer is done playing. If you need to play a series of resources, you will still need to handle this case.

    0 讨论(0)
  • 2020-12-25 13:07

    Or, you could access the resource in this way:

    mediaPlayer.setDataSource(context, Uri.parse("android.resource://com.package.name/raw/song"));
    

    where com.package.name is the name of your application package

    0 讨论(0)
提交回复
热议问题