logo
down
shadow

Android Studio and mediaPlayer


Android Studio and mediaPlayer

By : Spengreb
Date : November 29 2020, 09:01 AM
wish help you to fix your issue Implement MediaPlayer.OnCompletionListener then add your message to onCompletion method:
code :
@Override
public void onCompletion(MediaPlayer mp) {
   //message
}


Share : facebook icon twitter icon
Android Studio Mediaplayer how to fade in and out

Android Studio Mediaplayer how to fade in and out


By : Maria Petrova
Date : March 29 2020, 07:55 AM
it helps some times Looking at the linked example, you would have to call fadeIn()/fadeOut() in a loop, to increase/decrease the volume over a period of time. deltaTime would be the time between each iteration of the loop.
You'd have to do this in a separate thread from your main UI thread, so you don't block it and cause your app to crash. You can do this by either putting this loop inside a new Thread/Runnable/Timer.
code :
float volume = 0;

private void startFadeIn(){
    final int FADE_DURATION = 3000; //The duration of the fade
    //The amount of time between volume changes. The smaller this is, the smoother the fade
    final int FADE_INTERVAL = 250;
    final int MAX_VOLUME = 1; //The volume will increase from 0 to 1
    int numberOfSteps = FADE_DURATION/FADE_INTERVAL; //Calculate the number of fade steps
    //Calculate by how much the volume changes each step
    final float deltaVolume = MAX_VOLUME / (float)numberOfSteps;

    //Create a new Timer and Timer task to run the fading outside the main UI thread
    final Timer timer = new Timer(true);
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            fadeInStep(deltaVolume); //Do a fade step
            //Cancel and Purge the Timer if the desired volume has been reached
            if(volume>=1f){
                timer.cancel();
                timer.purge();
            }
        }
    };

    timer.schedule(timerTask,FADE_INTERVAL,FADE_INTERVAL);
}

private void fadeInStep(float deltaVolume){
    mediaPlayer.setVolume(volume, volume);
    volume += deltaVolume;

}
**Audio track #1 is playing but coming to the end**
startFadeOut();
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.setDataSource(context,audiofileUri);
mediaPlayer.prepare();
mediaPlayer.start();
startFadeIn();
**Audio track #2 has faded in and is now playing**
I can't get the mediaplayer working in android studio

I can't get the mediaplayer working in android studio


By : Rob B.
Date : March 29 2020, 07:55 AM
Hope that helps As you can see in the supported Media formats .mav is not supported by android. you should convert it to mp3 or any of the supported file formats
How would I reset my MediaPlayer in Android Studio 2.2.3?

How would I reset my MediaPlayer in Android Studio 2.2.3?


By : CuBoulderHippie
Date : March 29 2020, 07:55 AM
I wish this help you So I am trying to make a button that plays a sound, at one point it asked me something about opening the sound file I want on my computer, I did not really see it and click on my PC, so every time I run my app the file gets played on my PC and the app never gets launched, anyone know how I would reset that? (Tried a new project, restarted.) , MainActivity.java:
code :
public class MainActivity extends AppCompatActivity {

private MediaPlayer mediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mediaPlayer = MediaPlayer.create(this, R.raw.diwali);

    Button play = (Button) findViewById(R.id.play);
    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mediaPlayer.start();
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
                @Override
                public void onCompletion(MediaPlayer mp) {
                    Toast.makeText(MainActivity.this, "I'm done!", Toast.LENGTH_SHORT).show();
                }

            });
        }
    });

    Button pause = (Button) findViewById(R.id.pause);
    pause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mediaPlayer.pause();
        }
    });
}
<Button
    android:id="@+id/play"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/play" />

<Button
    android:id="@+id/pause"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/pause" />
Using MediaPlayer for Android Studio

Using MediaPlayer for Android Studio


By : Ulysses San Juan
Date : March 29 2020, 07:55 AM
this one helps. Use getActivity(). It gives the Media Player the context it needs. Try to use this code:
code :
MediaPlayer mp = MediaPlayer.create( getActivity() , R.raw.alphabetlist );
MediaPlayer mp; 
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    mp=MediaPlayer.create(getActivity(), R.raw.alphabetlist); 
    mp.start():  

    return inflater.inflate(R.layout.fragment_alphabet, container, false);
}
Android Studio| MediaPlayer| My function can run one time, then the app will crash | MediaPlayer: Error (1,-19)

Android Studio| MediaPlayer| My function can run one time, then the app will crash | MediaPlayer: Error (1,-19)


By : Dazed_And_Confuzed
Date : March 29 2020, 07:55 AM
This might help you
code :
//just initialize inside ClickListener, speak_rost, and after .setOnCompletionListener, I put speak_rost().start; that was ALL, very nice problem, now all my 50+ buttons/layouts are "in the right form" to play 100+ time, every time I needed. Thank you.
layoutRost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                speak_rost = MediaPlayer.create(hundred.this, R.raw.asa);
                speak_rost .setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    public void onCompletion(MediaPlayer mysound) {
                        mysound.release();
                    }

                });
                speak_rost .start();
            }
        });
shadow
Privacy Policy - Terms - Contact Us © animezone.co