Calling another activity when mediaplayer get finished playing

十年热恋 提交于 2019-12-18 04:27:14

问题


Hi I used the following code to call an activity when audio play get finished. But I want to play the audio in current activity, when play get finished it has to jump for the next activity. But here it jumps to the next activity and play the audio. Any suggestions.

package com.fsp.mus;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Gravity;
import android.widget.Toast;

public class MyRecording extends Activity{
    String mFileName;
     MediaPlayer mPlayer; 
     Boolean stopplay;
    @Override

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myrecording);

        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/audiorecordtest.3gp";
        mPlayer = new MediaPlayer(); 

         try {

            mPlayer.setDataSource(mFileName);
        } catch (IllegalArgumentException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IllegalStateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
          try {
            mPlayer.prepare();
        } catch (IllegalStateException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        mPlayer.start();    
       if(mPlayer.getCurrentPosition()== mPlayer.getDuration())
       {
           Intent stopplay=new Intent(MyRecording.this,Recorded_Message.class);
            startActivity(stopplay); 
       }

    }
}

回答1:


mPlayer.setOnCompletionListener(new
    OnCompletionListener() {        
        @Override
        public void onCompletion(MediaPlayer arg0) {
        Intent stopplay= new Intent(MyRecording.this,Recorded_Message.class);
        startActivity(stopplay);                
    }
});



回答2:


I'll try to explain what is wrong in your code.

onCreate calls only one time when an Activity creates. So, in your code you set a source for your mPlayer:

mPlayer.setDataSource(mFileName);

then prepare it:

mPlayer.prepare();

and then starts playing:

mPlayer.start();    

As you just started mPlayer, of cource if statment will be false and the code in if will never be performed:

if(mPlayer.getCurrentPosition()== mPlayer.getDuration())
{
    Intent stopplay=new Intent(MyRecording.this,Recorded_Message.class);
    startActivity(stopplay); 
}

I remind you that onCreate is called only once.

So, use event as described above.



来源:https://stackoverflow.com/questions/6001497/calling-another-activity-when-mediaplayer-get-finished-playing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!