首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Seekbar不工作

Seekbar不工作
EN

Stack Overflow用户
提问于 2013-09-23 12:10:43
回答 1查看 2K关注 0票数 0

我找到了一些搜索栏与在线媒体播放器一起工作的代码:

代码语言:javascript
复制
 @Override
 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {

 }

 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {
     // remove message Handler from updating progress bar
     mHandler.removeCallbacks(mUpdateTimeTask);
 }
 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {
     mHandler.removeCallbacks(mUpdateTimeTask);
     int totalDuration = mp.getDuration();
     int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);

     // forward or backward to certain seconds
     mp.seekTo(currentPosition);

     // update timer progress again
     updateProgressBar();
 }

由于重写,我得到了一个错误,告诉我它们必须重写或实现超类型方法。当我移除它们时,我的代码会编译并运行,但是搜索栏与音乐播放没有任何关联,就像在我可以移动它但音乐继续播放一样。

谢谢。

代码语言:javascript
复制
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mp = new MediaPlayer();
        seekBar = (SeekBar) findViewById(R.id.seekBar1);
        seekBar.setOnSeekBarChangeListener(this); //this line causes an error
        try {
            mp.setDataSource(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getPath() + "/SpaceJam.mp3");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mp.prepare();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onStart(){
        super.onStart();

        btnPlay = (ImageButton) findViewById(R.id.play_pause);
        btnStop = (ImageButton) findViewById(R.id.stop);
        songTitleLabel = (TextView) findViewById(R.id.songname);
        songTitleArtist = (TextView) findViewById(R.id.artist);
        songCurrentDurationLabel = (TextView) findViewById(R.id.current);
        songTotalDurationLabel = (TextView) findViewById(R.id.total);
        albumArt = (ImageView) findViewById(R.id.albumArt);
        res = getResources().getDrawable(R.drawable.album_art);
        utils = new Utilities();
        . . .
}
EN

回答 1

Stack Overflow用户

发布于 2013-09-23 14:41:39

正如prijupaul提到的,你必须实现OnSeekBarChangeListener。按如下方式更改您的代码:

代码语言:javascript
复制
 public class MainActivity extends Activity implements OnSeekBarChangeListener
 {
     @Override
     protected void onCreate(Bundle savedInstanceState) {

          ...
          seekBar.setOnSeekBarChangeListener(this);
          ...
     }

     @Override
     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {

     }

     @Override
     public void onStartTrackingTouch(SeekBar seekBar) {

     }

     @Override
     public void onStopTrackingTouch(SeekBar seekBar) {

     }
 }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18951643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档