我还有一个疑问,我正在使用从Firebase播放流声音的应用程序。该应用程序首次能够正常运行,但是当我包含admob依赖项并在应用程序中做广告时,当我播放声音时,该应用程序在模拟器和真实设备中的运行速度非常慢,为什么?
这是我的代码,当playAudio.setOnClickListener ...启动缓慢的问题。
public class CuentoMediaPlayerActivity extends AppCompatActivity implements MediaPlayer.OnPreparedListener {
private ImageView appBarImage;
private Button playAudio, stopAudio, pauseAudio;
private Cuento cuento;
private MediaPlayer mMediaplayer = new MediaPlayer();
private FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
private StorageReference httpsReference;
private boolean pause = false;
private SeekBar seekBar;
private Runnable mUpdateSeekbar;
private Handler mSeekbarUpdateHandler;
private TextView currentTime, finishTime;
private ProgressBar progressBarMediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cuento_media_player);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
cuento = (Cuento) getIntent().getSerializableExtra("Cuento");
httpsReference = firebaseStorage.getReferenceFromUrl(cuento.getUrl());
getSupportActionBar().setTitle(cuento.getTitle());
progressBarMediaPlayer = findViewById(R.id.progressBarMediaPlayer);
progressBarMediaPlayer.setVisibility(View.INVISIBLE);
currentTime = findViewById(R.id.currentTime);
finishTime = findViewById(R.id.finishTIme);
mSeekbarUpdateHandler = new Handler();
mUpdateSeekbar = new Runnable() {
@Override
public void run() {
seekBar.setProgress(mMediaplayer.getCurrentPosition());
int seconds = (seekBar.getProgress() / 1000) % 60 ;
int minutes = ((seekBar.getProgress() / (1000*60)) % 60);
currentTime.setText(String.valueOf(minutes) + ":" + String.valueOf(seconds));
mSeekbarUpdateHandler.postDelayed(this, 50);
}
};
seekBar = findViewById(R.id.progressBar);
seekBar.getProgressDrawable().setColorFilter(
Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
appBarImage = findViewById(R.id.app_bar_image);
playAudio = findViewById(R.id.playAudio);
stopAudio = findViewById(R.id.stopAudio);
pauseAudio = findViewById(R.id.pauseAudio);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
mMediaplayer.seekTo(progress);
int seconds = (progress / 1000) % 60 ;
int minutes = ((progress / (1000*60)) % 60);
currentTime.setText(String.valueOf(minutes) + ":" + String.valueOf(seconds));
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
Glide.with(getApplicationContext()).load(cuento.getImage()).into(appBarImage);
playAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (pause == true) {
pause = false;
playAudio.setVisibility(View.INVISIBLE);
mMediaplayer.start();
mSeekbarUpdateHandler.postDelayed(mUpdateSeekbar, 0);
} else {
progressBarMediaPlayer.setVisibility(View.VISIBLE);
httpsReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
try {
mMediaplayer.setDataSource(uri.toString());
mMediaplayer.setOnPreparedListener(CuentoMediaPlayerActivity.this);
mMediaplayer.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
});
pauseAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(mMediaplayer != null && mMediaplayer.isPlaying()){
pause = true;
pauseAudio.setVisibility(View.INVISIBLE);
playAudio.setVisibility(View.VISIBLE);
mMediaplayer.pause();
mSeekbarUpdateHandler.removeCallbacks(mUpdateSeekbar);
}
}
});
stopAudio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mMediaplayer != null) {
pause = true;
stopAudio.setVisibility(View.INVISIBLE);
playAudio.setVisibility(View.VISIBLE);
mMediaplayer.pause();
mMediaplayer.seekTo(0);
}
}
});
}
@Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
seekBar.setMax(mMediaplayer.getDuration());
int seconds = (seekBar.getMax() / 1000) % 60 ;
int minutes = ((seekBar.getMax() / (1000*60)) % 60);
finishTime.setText(String.valueOf(minutes) + ":" + String.valueOf(seconds));
progressBarMediaPlayer.setVisibility(View.INVISIBLE);
playAudio.setVisibility(View.INVISIBLE);
mSeekbarUpdateHandler.postDelayed(mUpdateSeekbar, 0);
mMediaplayer.start();
}
@Override
public void finish() {
super.finish();
if(mMediaplayer != null){
mMediaplayer.stop();
}
}
}
和我的依赖项:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.3'
implementation 'com.google.firebase:firebase-storage:16.0.3'
implementation 'com.google.firebase:firebase-auth:16.0.5'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'com.google.android.gms:play-services-ads:17.0.0'
implementation 'com.android.support:customtabs:27.1.1'
}
apply plugin: 'com.google.gms.google-services'