2011-03-28 59 views
3

可能重複:
Android: How to stop media (mp3) in playing when specific milliseconds come?Android:如何在特定的時間內停止播放媒體(mp3)?

我有一個mp3文件,我想在其中發揮一個特定的單詞。我有一個開始時間(6889毫秒)和結束時間(7254毫秒),所以他們的持續時間是365毫秒。我想播放我的mp3文件的365 ms部分。

當我運行這些代碼時,它會播放單詞「他」,但播放「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); 
      }  
     }); 


    } 
} 
+0

別問了(幾乎)同樣的問題:http://stackoverflow.com/questions/5454452/android-how-to-stop-media-mp3-in-playing-when-specific-milliseconds-來吧 – 2011-03-28 11:21:43

+0

謝謝你把它鏈接到我的其他問題:) – Kris 2011-03-28 23:33:37

回答

2

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

哈哈:)沒有冒犯的人,但你的問題真的很有趣。無論如何,有一個絕對的方法,並且更容易做這樣的任務。錄製只播放「He」=)的音頻)並放入原始資源,以便MediaPlayer僅播放一首他的作品:>

+0

謝謝你的主意男人!它只是即時通訊有點新的Android開發。 :) – Kris 2011-03-28 23:38:18

相關問題