Android audio playback rate adjustment to achieve

  Recent exposure to a project, there are audio playback, switch the playback rate and drag the progress needs somewhere to play, since only seen before and have not tried to switch the playback rate, and began to research and, ultimately, following a brief look at this record times the research process.

  • MediaPlayer

Play Audio MediaPlayer first thought is that Android offers a native API in Android 6.0+ (23+) MediaPlayer playback rate can be changed by setSpeed

In the code, we need to:

// set the music playback speed 
public  static  void changeplayerSpeed ( a float Speed) { 
     IF (MPlayer == null ) {
         return ; 
    } 
    IF (Build.VERSION.SDK_INT> = Build.VERSION_CODES.M) { 
         // the API 23 is (6.0) above by setting Speed change the playback rate music 
        iF (mPlayer.isPlaying ()) {
             // determine whether you are playing, when not playing, to be set up after Speed, pause music playback 
            mPlayer.setPlaybackParams (mPlayer.getPlaybackParams (). setSpeed ( Speed)); 
        } the else {  
            mPlayer.setPlaybackParams (mPlayer.getPlaybackParams () setSpeed (Speed)).;
            mPlayer.pause (); 
        } 
    } the else {
         // the Android6.0 before processing required another way, subsequent found a good way to add 
    } 
}

Actual implementation process, I found the hands of testers Honor V9 After you do this, the silent play, not only did not achieve switch play rate, the player can not be restored. Helpless, he had to find another method.

  • PLMediaPlayer 

PLDroidPlayer seven cattle SDK provides a set of API,  PLMediaPlayerto achieve a variety of basic functions and interfaces media player, with the official Android  MediaPlayer design remained the same.

The key line of code

    private PLOnPreparedListener mOnPreparedListener = new PLOnPreparedListener() {
        @Override
        public void onPrepared(int preparedTime) {
            Log.i(TAG, "On Prepared !");
            mMediaPlayer.start();
       //设置播放速率为2x mMediaPlayer.setPlaySpeed(
2.0f); mIsStopped = false; } };

 The actual implementation process, switching normal playback rate, but a high probability seekTo operation fails, so check it out on github how to find ISSUE still the problem, then give up.

  • ijkPlayer

ijkplayer station b is the Lightweight Android ffplay / iOS video player, the function of the cross-platform, easy to integrate the API; build configurations can be cut, easy to control the size of the installation package.

ijkMediaPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "soundtouch", 1);
ijkMediaPlayer.setSpeed(2.0f);

api and usage similar mediaplayer, the key code is only one line.

The actual implementation process, seekTo normal, and normal playback rate switch (only switching to the presence of stress 0.5x slow time), but can not begin with https url play audio files, search a bit need to compile the source code to support ijkplayer https, then give up.

  • ExoPlayer

 The final choice is google's exoPlayer to achieve, api similar MediaPlayer, but also some differences, the key code is posted below playback control section.

package com.weex.app.media;

import android.content.Context;
import android.net.Uri;

import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;

import java.io.File;

public class AudioPlayerManager {

    private static final String TAG = "AudioPlayerManager";
    public static Float[] speedArray = new Float[]{1.0f, 1.25f, 1.75f, 0.5f, 0.75f};
    private static AudioPlayerManager instance;
    private Context context;
    private SimpleExoPlayer mediaPlayer;
    private DataSource.Factory dataSourceFactory;

    private AudioPlayerManager(Context context) {
        this.context = context;
        createPlayer();
    }

    public static AudioPlayerManager getInstance(Context context) {
        the synchronized (AudioPlayerManager. class ) {
             IF (instance == null ) { 
                instance = new new AudioPlayerManager (context); 
            } 
        } 

        return instance; 
    } 

    public ExoPlayer getMediaPlayer () {
         return mediaPlayer; 
    } // set the playback URL
    
   public  void setAudioUrl (String audioUrl ) {
         the try {
             // this is a representative of the media to be played in MediaSource 
            MediaSource MediaSource = new newExtractorMediaSource.Factory (DataSourceFactory) 
                    .createMediaSource (Uri.parse (audioUrl)); 
            mediaPlayer.prepare (MediaSource); 
            mediaPlayer.setPlayWhenReady ( to false ); 
        } the catch (Exception E) { 
            e.printStackTrace (); 
        } 
    } // set the playback File
    
   public  void setAudioFile (File File) {
         the try {
             // this is a representative will be MediaSource play media 
            MediaSource MediaSource = new new ExtractorMediaSource.Factory (DataSourceFactory) 
                    .createMediaSource (Uri.fromFile (File)); 
            mediaPlayer.prepare (MediaSource);
            mediaPlayer.setPlayWhenReady(false);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  //开始播放
    public void start() {
        mediaPlayer.setPlayWhenReady(true);
    }
  //判断是否是播放状态
    public boolean isPlaying() {
        int playbackState = mediaPlayer.getPlaybackState();
        if (playbackState == SimpleExoPlayer.STATE_READY && mediaPlayer.getPlayWhenReady()) {
            return true;
        }
        return false;
    }
  //播放,带回调事件
    public void playWithCompletionListener(String url, Player.EventListener listener) {
        if (listener != null) {
            mediaPlayer.addListener(listener);
        }
        if (url.startsWith("http")) {
            setAudioUrl(url);
        } else {
            setAudioFile(new File(url));
        }
        mediaPlayer.setPlayWhenReady(true);
    }
  //播放or暂停
    public void playOrPause () {
        IF (isPlaying ()) { 
            mediaPlayer.setPlayWhenReady ( to false ); 
        } the else { 
            mediaPlayer.setPlayWhenReady ( to true ); 
        } 
    } // switch the playback rate
    
   public  void switchSpeed ( int speedIndex) {
         // the API 23 is (6.0) above, by providing the speed change the playback speed of music 
        iF (isPlaying ()) {
             // if the determination is being played, when not playing, after setting to speed, pause the music playback 
            getMediaPlayer () setPlaybackParameters (. new new PlaybackParameters (speedArray [speedIndex])); 
        } the else {
            getMediaPlayer().setPlaybackParameters(new PlaybackParameters(speedArray[speedIndex]));
            getMediaPlayer().setPlayWhenReady(false);
        }
    }
  //停止播放
    public void stop(boolean reset) {
        if (mediaPlayer != null) {
            mediaPlayer.stop(reset);
        }
    }
  //释放资源
    public void release() {
        if (mediaPlayer != null) {
            mediaPlayer.release();
        }
    }

    // Create a new Player 
    Private  void the createPlayer () {
         // create a bandwidth 
        BandwidthMeter bandwidthMeter = new new DefaultBandwidthMeter ();
         // Create a track selection facility 
        TrackSelection.Factory videoTrackSelectionFactory = new new AdaptiveTrackSelection.Factory (bandwidthMeter);
         // Create a track selector examples 
        TrackSelector trackSelector = new new DefaultTrackSelector (videoTrackSelectionFactory);
         // Step2 created player. 
        mediaPlayer = ExoPlayerFactory.newSimpleInstance (context, trackSelector);
         // Create a DataSource object, through which to download the multimedia data
        dataSourceFactory = new DefaultDataSourceFactory(context,
                Util.getUserAgent(context, "loader"));
    }

}

 The actual self-test process, the performance of normal, and switching the playback rate, there is no case of a heavy (Chong) sound, but has not been tested on the following equipment 6.0.

 

 

Guess you like

Origin www.cnblogs.com/fuyaozhishang/p/11098596.html