开发者

How to handle tempo and pitch in android using MediaPlayer or SoundPool

开发者 https://www.devze.com 2023-03-10 23:41 出处:网络
I try to handle tempo and pitch using SoundPool. In the play() method, if we change the rate (last argument) the tempo and pitch both are changing. And I am not able to play the full sound file using

I try to handle tempo and pitch using SoundPool. In the play() method, if we change the rate (last argument) the tempo and pitch both are changing. And I am not able to play the full sound file using SoundPool. I got solution using FileDescriptor in SoundPool we can mention the length of the file to play but its not working. Pl开发者_开发百科ease share your ideas.

Thank You


Try this out for pitch changing in Android

This may be of some use. After trying everything out, i decided to go with SoundPool to change the Pitch, A playback rate of 2.0 causes the sound to play at a freq double its original, and a playback rate of 0.5 causes it to play at half its original frequency. The playback rate range is 0.5 to 2.0. But it did work with freq lower and higher than 0.5 and 2.0.

I am posting my working code,

but as its just for demo purpose, Here you need to Manually change the "playback rate", everytime you install the application for eg: "sp.play(explosion, 1,1,0,0,1.5f)" here "1.5f" is the playback rate. One can easily create an EditView,or something similar to set the value of playback rate at run time.

In this app, you just need to tap on the app's screen to play the music at the set playback rate.

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SoundPoolActivity extends Activity implements OnClickListener {

    SoundPool sp;
    int explosion = 0;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       View v = new View(this);
       v.setOnClickListener(this);
       setContentView(v);
       sp = new SoundPool(1,AudioManager.STREAM_MUSIC,0);
       //explosion = sp.load(this, R.raw.hh,0);
       explosion = sp.load("/sdcard/hh.m4a",0);


    }


    public void onClick(View v){
         if (explosion!=0){

             sp.play(explosion, 1,1,0,0,2.3f);
         }

    }
}


As my knowledge Mediaplayer or Soundpool can not let us change the pitch and temp(speed) independently.

You can try using soundtouch, a github project maintained by xanaduo.

It made my day after 2 days of searching over the net.

0

精彩评论

暂无评论...
验证码 换一张
取 消