Hun*_*unt 7 android android-mediaplayer
好吧,我正在尝试实现录音的基本功能,比如
录制,播放/暂停,停止
我可以做所有这些,但唯一的问题是如何在音频播放结束后收到通知.我的意思是如果我播放音频文件然后一旦它完成播放我想要一个通知它现在停止.
到目前为止我用过
mPlayer.start() // to start the audio
mPlayer.stop(); // to stop the audio
mPlayer.pause(); //to pause the audio
Run Code Online (Sandbox Code Playgroud)
我只是试图找出一旦音频文件自动完成播放我将如何知道
And*_*lva 26
您可以使用Media Player类的Completion Listener来执行此操作.
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
Log.i("Completion Listener","Song Complete");
Toast.makeText(context, "Media Completed", Toast.LENGTH_SHORT).show();
}
});
Run Code Online (Sandbox Code Playgroud)
尝试使用此代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button id="@+id/cmd_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play the music !!!"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
MusicPlayer活动代码
public class MusicPlayer extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// Find the Button from the xml-file.
Button cmd_play = (Button)this.findViewById(R.id.cmd_play);
cmd_play.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
MediaPlayer mp = MediaPlayer.create(MusicPlayer.this,
R.raw.everlast);
mp.prepare();
mp.start();
// i.e. react on the end of the music-file:
mp.setOnCompletionListener(new OnCompletionListener(){
// @Override
public void onCompletion(MediaPlayer arg0) {
// File has ended !!!
Toast.makeText(context, "Media Completed with Success", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
在资源文件夹中放置文件声音
| 归档时间: |
|
| 查看次数: |
11918 次 |
| 最近记录: |