How to play an mp3 file between a range of millise

2019-06-10 13:18发布

I am able to play an mp3 file using android's MediaPlayer object. But I would like to play between a range of milliseconds for example between 30000 ms to 40000 ms ( 10 seconds only ). How can I achieve this?

Currently the following code is what I have,

private MediaPlayer mPlayer;
public void play() {
    try {
        mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.mp3_file);
        if (mPlayer != null) {
            int currentPosition = mPlayer.getCurrentPosition();
            if (currentPosition + 30000 <= mPlayer.getDuration()) {
                mPlayer.seekTo(currentPosition + 30000);
            } else {
                mPlayer.seekTo(mPlayer.getDuration());
            }
            mPlayer.start();
        }
    }
    catch(Exception e) {
    }
}

Any help is greatly appreciated. Thank you!

2条回答
Juvenile、少年°
2楼-- · 2019-06-10 13:36

The best approach is to use a Handler to time the stopping of the playback. Start the player and then use the Handler's postDelayed to schedule the execution of a Runnable that will stop the player. You should also start the player only after the initial seek completes. Something like this:

public class PlayWord extends Activity implements MediaPlayer.OnSeekCompleteListener {
    Handler mHandler;
    MediaPlayer mPlayer;
    int mStartTime = 6889;
    int mEndTime = 7254;
    final Runnable mStopAction = new Runnable() {
        @Override
        public void run() {
            mPlayer.stop();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        final TextView tv = new TextView(this);
        tv.setText("Playing...");
        setContentView(tv);
        mHandler = new Handler();
        mPlayer = MediaPlayer.create(this, R.raw.nicholas);
        mPlayer.setOnSeekCompleteListener(this);
        mPlayer.seekTo(mStartTime);
    }

    @Override
    public void onDestroy() {
        mPlayer.release();
    }

    @Override
    public void onSeekComplete (MediaPlayer mp) {
        mPlayer.start();
        mHandler.postDelayed(mStopAction, mEndTime - mStartTime);
    }
}

Note also that the MediaPlayer.create method you are using returns a MediaPlayer that has already been prepared and prepare should not be called again like you are doing in your code.on the screen. I also added a call to release() when the activity exits.

Also, if you want to update the UI when the seek completes, be aware that this method is usually called from a non-UI thread. You will have to use the handler to post any UI-related actions.


I'm copied this from: Android: How to stop media (mp3) in playing when specific milliseconds come?

查看更多
不美不萌又怎样
3楼-- · 2019-06-10 13:43

You can use the method:

public int getCurrentPosition ()

to obtain the current time in milSeconds maybe inside a Handler that runs every 1000 milSeconds and tests to see:

if(mPlayer.getCurrentPosition() >= (mPlayer.getDuration + 40000));

Dont forget to release the media file when you're done using it:

public void release();
mPlayer.release();

Releases resources associated with this MediaPlayer object. It is considered good practice to call this method when you're done using the MediaPlayer. In particular, whenever an Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called), this method should be invoked to release the MediaPlayer object, unless the application has a special need to keep the object around. In addition to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a MediaPlayer object is no longer needed may also lead to continuous battery consumption for mobile devices, and playback failure for other applications if no multiple instances of the same codec are supported on a device. Even if multiple instances of the same codec are supported, some performance degradation may be expected when unnecessary multiple instances are used at the same time.

查看更多
登录 后发表回答