如何与自定义媒体播放器一起显示搜索栏

发布于 2024-10-21 05:50:29 字数 2465 浏览 2 评论 0原文

我已经构建了一个自定义媒体播放器,我也想显示搜索栏。 搜索栏目前正在显示,但不起作用,即播放歌曲时它没有进度。让我知道我错在哪里。以下是代码

public class myPlayer extends Activity implements Runnable,SeekBar.OnSeekBarChangeListener {

    MediaPlayer mPlayer;
    Button play,stop;
    SeekBar seekbar;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        seekbar = (SeekBar)findViewById(R.id.seekbar);
        mPlayer = MediaPlayer.create(this,R.raw.sajnave);
        play=(Button)findViewById(R.id.play);
        stop=(Button)findViewById(R.id.stop);
        Toast.makeText(myPlayer.this, ""+mPlayer.getDuration(), Toast.LENGTH_SHORT).show();

        play.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mPlayer.start();

            }

        });

        stop.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(myPlayer.this, ""+mPlayer.getCurrentPosition(), Toast.LENGTH_SHORT).show();
                mPlayer.stop();
            }

        });
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

        int currentPosition = 0;

                int total = mPlayer.getDuration();

                seekbar.setMax(total);

                while (mPlayer != null && currentPosition < total) {

                    try {

                        Thread.sleep(1000);

                        currentPosition = mPlayer.getCurrentPosition();

                    } catch (InterruptedException e) {

                        return;

                    } catch (Exception e) {

                        return;

                    }

                    seekbar.setProgress(currentPosition);

                }

    }

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        // TODO Auto-generated method stub
        mPlayer.seekTo(arg1);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

}

i have build a custom media player and i want to show the seekbar also.
The seekbar is showing for now but is not functional i.e. it is not progressing when the song is played. Let me know where am i wrong . Following is the code

public class myPlayer extends Activity implements Runnable,SeekBar.OnSeekBarChangeListener {

    MediaPlayer mPlayer;
    Button play,stop;
    SeekBar seekbar;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        seekbar = (SeekBar)findViewById(R.id.seekbar);
        mPlayer = MediaPlayer.create(this,R.raw.sajnave);
        play=(Button)findViewById(R.id.play);
        stop=(Button)findViewById(R.id.stop);
        Toast.makeText(myPlayer.this, ""+mPlayer.getDuration(), Toast.LENGTH_SHORT).show();

        play.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mPlayer.start();

            }

        });

        stop.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(myPlayer.this, ""+mPlayer.getCurrentPosition(), Toast.LENGTH_SHORT).show();
                mPlayer.stop();
            }

        });
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

        int currentPosition = 0;

                int total = mPlayer.getDuration();

                seekbar.setMax(total);

                while (mPlayer != null && currentPosition < total) {

                    try {

                        Thread.sleep(1000);

                        currentPosition = mPlayer.getCurrentPosition();

                    } catch (InterruptedException e) {

                        return;

                    } catch (Exception e) {

                        return;

                    }

                    seekbar.setProgress(currentPosition);

                }

    }

    @Override
    public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
        // TODO Auto-generated method stub
        mPlayer.seekTo(arg1);
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人如初 2024-10-28 05:50:29

mediplayer 启动后调用 updatePosition() 方法。它对我有用。

private final Handler handler = new Handler();

private final Runnable updatePositionRunnable = new Runnable() {
    public void run() {
        updatePosition();
    }
};

private void updatePosition() {
    handler.removeCallbacks(updatePositionRunnable);
    if (player != null) seekbar.setProgress(player.getCurrentPosition());

    handler.postDelaye(updatePositionRunnable, 500);

}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
    // TODO Auto-generated method stub
    player.seekTo(arg1);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

Call updatePosition() method after mediplayer starts..It works for me.

private final Handler handler = new Handler();

private final Runnable updatePositionRunnable = new Runnable() {
    public void run() {
        updatePosition();
    }
};

private void updatePosition() {
    handler.removeCallbacks(updatePositionRunnable);
    if (player != null) seekbar.setProgress(player.getCurrentPosition());

    handler.postDelaye(updatePositionRunnable, 500);

}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
    // TODO Auto-generated method stub
    player.seekTo(arg1);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub

}
深居我梦 2024-10-28 05:50:29
SurfaceView mPreview=(SurfaceView)findViewById(R.id.surface);    
mPreview.postDelayed(runnable,200);    
Runnable runnable = new Runnable() {

    @Override
    public void run() {
        if (mMediaPlayer != null) {
            if (isPlaying) {
                // converting current position into seconds so dividing by
                // 1000
                try {
                    currentPosition = mMediaPlayer.getCurrentPosition();
                    mVideoProgress
                            .setProgress((int) ((currentPosition / (float) mMediaPlayer
                                    .getDuration()) * 100f));

                } catch (Exception e) {
                    e.printStackTrace();
                }

                mPreview.postDelayed(runnable, 150);
            }
        }

    }
};
SurfaceView mPreview=(SurfaceView)findViewById(R.id.surface);    
mPreview.postDelayed(runnable,200);    
Runnable runnable = new Runnable() {

    @Override
    public void run() {
        if (mMediaPlayer != null) {
            if (isPlaying) {
                // converting current position into seconds so dividing by
                // 1000
                try {
                    currentPosition = mMediaPlayer.getCurrentPosition();
                    mVideoProgress
                            .setProgress((int) ((currentPosition / (float) mMediaPlayer
                                    .getDuration()) * 100f));

                } catch (Exception e) {
                    e.printStackTrace();
                }

                mPreview.postDelayed(runnable, 150);
            }
        }

    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文