Android的:如何停止播放时的具体毫秒来的媒体(MP3)?具体、媒体、Android

由网友(長野)分享简介:我有一个mp3文件,我想在其中发挥一个特定的词。我有一个开始时间(6889毫秒)和结束时间(7254毫秒) 我的这些codeS:的包com.example.playword;进口java.io.IOException异常;进口android.app.Activity;进口android.media.MediaPl...

我有一个mp3文件,我想在其中发挥一个特定的词。我有一个开始时间(6889毫秒)和结束时间(7254毫秒)

我的这些codeS:的

包com.example.playword;

进口java.io.IOException异常;
进口android.app.Activity;
进口android.media.MediaPlayer;
进口android.os.Bundle;
进口android.widget.TextView;

公共类PlayWord延伸活动{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        最后的TextView电视=新的TextView(本);

        tv.setText(打......);
        的setContentView(电视);

        最后的MediaPlayer MPLAYER = MediaPlayer.create(这一点,R.raw.nicholas);

        尝试 {
            MPLAYER prepare()。
        }赶上(IllegalStateException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }

        mPlayer.seekTo(6889); //这是开始时间
        mPlayer.start();
        //我怎么可以结束它在7254毫秒?
    }
}

解决方案

最好的方法是使用一个处理器来一次播放的停止。启动播放器,然后使用handler的 postDelayed 来安排的Runnable 将停止播放器的执行。你也应该开始只准在首次寻求就完成了球员。事情是这样的:

 公共类PlayWord扩展活动实现MediaPlayer.OnSeekCompleteListener {
    处理器mHandler;
    MediaPlayer的MPLAYER;
    INT mStartTime = 6889;
    INT mEndTime = 7254;
    最终的可运行mStopAction =新的Runnable(){
        @覆盖
        公共无效的run(){
            mPlayer.stop();
        }
    };

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        最后的TextView电视=新的TextView(本);
        tv.setText(打......);
        的setContentView(电视);
        mHandler =新的处理程序();
        MPLAYER = MediaPlayer.create(这一点,R.raw.nicholas);
        mPlayer.setOnSeekCompleteListener(本);
        mPlayer.seekTo(mStartTime);
    }

    @覆盖
    公共无效的onDestroy(){
        mPlayer.release();
    }

    @覆盖
    公共无效onSeekComplete(MediaPlayer的MP){
        mPlayer.start();
        mHandler.postDelayed(mStopAction,mEndTime  -  mStartTime);
    }
}
 

另请注意, MediaPlayer.create 您使用的方法返回一个已经被prepared一个的MediaPlayer和 prepare 应的没有的被称为再喜欢你正在做的c.on屏幕上的$ C $。我还添加了调用发布()活动时退出。

另外,如果你想更新UI的时候寻求完成后,要知道,这种方法通常是从一个非UI线程调用。你将不得不使用handler张贴任何UI相关的操作。

体验 Android 11 半年之后,我们整理了升级正式版的 11 个理由

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)

I have these codes:

package com.example.playword;

import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
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 TextView tv = new TextView(this);

        tv.setText("Playing...");
        setContentView(tv);

        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
        mPlayer.start();
        //how can I end it at 7254 ms?
    }
}

解决方案

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.

阅读全文

相关推荐

最新文章