开发者

Android: How to stop a playing media (mp3) in specific duration? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Android: How to stop media (mp3) in playing when specific milliseconds come?

I have an mp3 file and I want to play a specific word in it. I have a start time (6889 ms) and end time (7254 ms) so their duration is 365 ms. I want to play that 365 ms part of my mp3 file.

When I run these codes it shoud play the word "He" but it plays "He wan". The word after "He" is "wanted". I want to play only the word "He". Is that possible?

package com.example.playword;

import java.io.IOException;


import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class PlayWord extends Activity {
    开发者_如何学Python/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final Handler mHandler = new Handler();

        final TextView labelTxt = (TextView) findViewById(R.id.labelTxt);

        labelTxt.setText("Playing word...");

        final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.nicholas);

        try {
            mPlayer.prepare();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        mPlayer.seekTo(6889); //this is the start time of the word i want to play
        mPlayer.start();

        //how can I end it at 7254?

        mHandler.post(new Runnable(){

            public void run(){ 

                //7254 is the end time of the word i want to play
                //7410 is the start time of another word
                if(mPlayer.getCurrentPosition() > 7254 && mPlayer.getCurrentPosition() < 7410 ){ 
                    labelTxt.setText("Stop: " + mPlayer.getCurrentPosition() );
                    mPlayer.stop();
                }

                mHandler.postDelayed(this, 1);
            }    
        });


    }
}


Either the MediaPlayer won't respond that rapidly to requests, in which case there's not much you can do, or your repeated delayed handler isn't being processed soon enough. I don't know if it will help but you could try posting a single Runnable to your handler which loops instead of re-posts itself. It might be more responsive.

    mHandler.post(new Runnable(){
        public void run(){ 
            while(mPlayer.isPlaying()) {
              if(mPlayer.getCurrentPosition() > 7254 && mPlayer.getCurrentPosition() < 7410 ){ 
                  labelTxt.setText("Stop: " + mPlayer.getCurrentPosition() );
                  mPlayer.stop();
                  break;
              }
            }
        }    
    });


Haha :) No offense man but your question really fun. Anyway there is an absolute way and so much easier to do such a task. Record a audio which only plays "He" =)) and put in raw resource so your MediaPlayer would only play one work he :>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜