【问题标题】:Android: How to stop a playing media (mp3) in specific duration? [duplicate]Android:如何在特定时间内停止播放媒体(mp3)? [复制]
【发布时间】:2011-03-28 17:43:21
【问题描述】:

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

我有一个 mp3 文件,我想在其中播放特定的单词。我有一个开始时间(6889 毫秒)和结束时间(7254 毫秒),所以它们的持续时间是 365 毫秒。我想播放我的 mp3 文件的 365 毫秒部分。

当我运行这些代码时,它应该播放“He”这个词,但它播放“He wan”。 “他”后面的字是“想要的”。我只想弹奏“他”这个词。那可能吗?

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 {
    /** 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);
            }    
        });


    }
}

【问题讨论】:

标签: android media-player


【解决方案1】:

MediaPlayer 不会对请求做出如此快速的响应,在这种情况下,您无能为力,或者您重复延迟的处理程序没有得到足够快的处理。我不知道它是否会有所帮助,但您可以尝试将单个 Runnable 发布到您的处理程序,该处理程序循环而不是重新发布自身。它可能会更敏感。

    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;
              }
            }
        }    
    });

【讨论】:

    【解决方案2】:

    哈哈 :) 没有冒犯,但你的问题真的很有趣。无论如何,有一种绝对的方法,而且做这样的任务要容易得多。录制只播放“He”=)) 的音频并放入原始资源,这样您的 MediaPlayer 就只能播放他的一部作品:>

    【讨论】:

    • 谢谢你的想法!它只是我对 android dev 来说有点新。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多