最初的Imageview和原始重复以及更改过快

时间:2018-12-19 17:22:59

标签: java android

我正在为学校创建一个用于学习字母的应用程序。我想在播放原始A时显示图像A。一切正常,但A和B原始文件有点混在一起,并且变化很快。 task()方法中是否有任何错误,或者我的实现是否错误?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alpha__loop);
    abc = findViewById(R.id.abc);
    img1 = findViewById(R.id.img1);
    mHandler = new Handler();
    timer();
}

public void task() {
    playsound = Soundid[s];
    s++;
    if (s == Soundid.length) {
        playsound = 0;
    }

    final MediaPlayer mediaPlayer = MediaPlayer.create(Alpha_Loop.this, playsound);

    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            mediaPlayer.start();
            updatetime = mediaPlayer.getDuration();
            imageview = ImageArray[i];
            i++;
            if (i == ImageArray.length) {
                imageview = 0;
            }
            abc.setImageResource(imageview);
            imageview2 = img1Array[i2];
            i2++;
            if (i2 == img1Array.length) {
                imageview2 = 0;
            }
            img1.setImageResource(imageview2);
        }
    });
}

public void timer() {        
    mHandler.postDelayed(updateTimerThread, 20 +updatetime);    
}

private Runnable updateTimerThread = new Runnable() {
    public void run() {
        //write here whaterver you want to repeat
        task();
        mHandler.postDelayed(this, 20 + updatetime);
    }
};

@Override
public void onBackPressed() {
    super.onBackPressed();
    if (mediaPlayer != null && mediaPlayer.isPlaying()) {
        mediaPlayer.release();
        mediaPlayer = null;
    }
    finish();
}

@Override
protected void onPause() {
    super.onPause();
    mediaPlayer.pause();
}

1 个答案:

答案 0 :(得分:1)

您可以尝试此帮助程序类,它可以帮助您“按顺序”播放音乐:

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.CountDownTimer;
import android.widget.Toast;

import java.io.Console;
import java.io.IOException;
import java.io.InputStream;

public class MediaManager
{
private static MediaPlayer _mediaPlayer;
private static Context _context;
private static int _pausedAt;
private static CountDownTimer _countDownTimer;

public static Bitmap GetBitmapFromAssets(Context c, String filename)
{
    AssetManager am = c.getAssets();
    InputStream is = null;
    try
    {
        is = am.open(filename);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return BitmapFactory.decodeStream(is);
}

public static void InitMediaPlayer(Context c)
{
    _context = c;
    _mediaPlayer = new MediaPlayer();
}
public static void LoadMusic(String filePath)
{
    try
    {
        _mediaPlayer.reset();
        AssetFileDescriptor afd = _context.getAssets().openFd(filePath);
        _mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
        _mediaPlayer.prepare();
        afd.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
        Toast.makeText(_context, "Couldn't load the music, please check your data folder.", Toast.LENGTH_SHORT).show();
    }
}
public static void PauseMusic()
{
    try
    {
        if (_mediaPlayer != null && _mediaPlayer.isPlaying())
        {
            _pausedAt = _mediaPlayer.getCurrentPosition();
            _mediaPlayer.pause();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
public static void PlayMusic()
{
    try
    {
        if (_mediaPlayer != null && !_mediaPlayer.isPlaying()) _mediaPlayer.start();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
public static void ResumeMusic()
{
    try
    {
        if (_mediaPlayer != null && !_mediaPlayer.isPlaying())
        {
            _mediaPlayer.seekTo(_pausedAt);
            _mediaPlayer.start();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
public static void StopMusic()
{
    try
    {
        if (_mediaPlayer != null && _mediaPlayer.isPlaying()) _mediaPlayer.stop();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}
public static void ReleaseMusic()
{
    try
    {
        if (_mediaPlayer != null) _mediaPlayer.release();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

}

用法非常简单,您只需要记住在最后释放媒体播放器即可。

用法:

在您的onCreate()中,启动媒体播放器

MediaManager.InitMediaPlayer(this);

然后加载声音样本:

MediaManager.LoadMusic("your_assets_path");

然后播放

MediaManager.PlayMusic();

希望这会有所帮助,因为我无法重现浇筑问题,

致谢