2014-09-22 85 views
0

我想在下面的程序中添加onCompletionlistener mediaplayer,所以當我的音頻完成後它應該通過Toast進行通知,並且應該將textshow設置爲「」。 但它並不會損害我的代碼播放,反之亦然。oncompletionlistener mediaplayer插入代碼

package project.kalmas;通過調用setOnCompletionListener

MediaPlayer.OnCompletionListener completionListener 
         = new MediaPlayer.OnCompletionListener(){  
    @Override 
    public void onCompletion(MediaPlayer arg0) { 
    // TODO Auto-generated method stub 
     // set TextView text here 
     // show Toast here 
    } 
}; 

將其設置爲MediaPlayer

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Color; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Toast; 

public class one extends Activity { 
    MediaPlayer mp=null; 
    TextView textshow; 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.one); 


    } 

    public void onclick2(View view) 
    { 
     Intent i=new Intent("project.two"); 
     startActivity(i); 
    } 

       public void onclick3(View view) { 
        textshow=(TextView)findViewById(R.id.textshow); 
        if (mp == null) { 
         mp = MediaPlayer.create(one.this, R.raw.one); 
         mp.start(); 
         Toast.makeText(getBaseContext(), "Playing", Toast.LENGTH_SHORT).show(); 
         textshow.setText("Playing..."); 
         textshow.setTextColor(Color.GREEN); 
        } else { 
         mp.stop(); 
         mp = null; 
         Toast.makeText(getBaseContext(), "Stopped", Toast.LENGTH_SHORT).show(); 
         textshow.setText("Stopped..."); 
         textshow.setTextColor(Color.RED); 
        } 

} 
} 

回答

0

通過創建MediaPlayer.OnCompletionListener聽衆做它

mp = MediaPlayer.create(one.this, R.raw.one); 
mp.setOnCompletionListener(completionListener); 
mp.start(); 
+0

我不知道在哪裏編輯它,你可以在我的文章中添加它並重新發布它嗎?我將不勝感激。 – 2014-09-22 19:27:29

+0

@RameelHashmi:在'onClick3'內使用'onCreate'方法和'setOnCompletionListener'後,使用'completionListener'代碼點擊 – 2014-09-22 19:28:57

+0

不能正常工作,當我點擊時有一個雙聲音。 – 2014-09-22 19:37:32

0

這由你的建議我編輯的東西被更新的代碼。
package project.kalmas;

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Color; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.TextView; 
import android.widget.Toast; 

public class one extends Activity { 
    MediaPlayer mp=null; 
    TextView textshow; 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.one); 


    } 
     MediaPlayer.OnCompletionListener completionListener 
     = new MediaPlayer.OnCompletionListener(){  
     @Override 
    public void onCompletion(MediaPlayer arg0) { 

    textshow.setText(" "); 
    Toast.makeText(getBaseContext(), "Finished", Toast.LENGTH_SHORT).show(); 
} 
}; 

    public void onclick2(View view) 
    { 
     Intent i=new Intent("project.two"); 
     startActivity(i); 
    } 

       public void onclick3(View view) { 
        textshow=(TextView)findViewById(R.id.textshow); 
        if (mp == null) { 
         mp = MediaPlayer.create(one.this, R.raw.one); 
         mp.start(); 
         Toast.makeText(getBaseContext(), "Playing", Toast.LENGTH_SHORT).show(); 
         textshow.setText("Playing..."); 
         textshow.setTextColor(Color.GREEN); 
        } else { 
         mp.stop(); 
         mp = null; 
         Toast.makeText(getBaseContext(), "Stopped", Toast.LENGTH_SHORT).show(); 
         textshow.setText("Stopped..."); 
         textshow.setTextColor(Color.RED); 
        }    
        mp = MediaPlayer.create(one.this, R.raw.one); 
        mp.setOnCompletionListener(completionListener); 
        mp.start(); 

} 
} 
+0

@ρяσѕρєяK my code – 2014-09-22 19:49:35