You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

音乐App开发:调用Song类方法及SeekBar显示歌曲时长问题

解决你的音乐App两个核心问题:调用Song类方法 & SeekBar分钟级时长显示

嘿,我来帮你搞定这两个Android开发里的常见问题,一步步来:


问题1:无法在SongDisplay中调用Song类的get方法

你现在直接写getSinger()肯定行不通的——这个方法是Song类的实例方法,只能通过Song对象来调用,而不是直接在Activity里用。你需要先把MainActivity里的Song数据传递到SongDisplay,再获取实例调用方法。这里给你两种常用方案:

方案1:通过Intent逐个传递属性值

如果Song的字段不多,这种方式简单直接:

  1. 在MainActivity启动SongDisplay时,把Song的各个属性打包进Intent:
// MainActivity中启动SongDisplay的代码示例
Song currentSong = new Song("夜曲", "4:15", "周杰伦", R.drawable.song_cover);
Intent intent = new Intent(MainActivity.this, SongDisplay.class);
intent.putExtra("SONG_TITLE", currentSong.getTitle());
intent.putExtra("SONG_SINGER", currentSong.getSinger());
intent.putExtra("SONG_IMAGE_ID", currentSong.getmImageId());
startActivity(intent);
  1. 在SongDisplay的onCreate里取出数据并设置UI:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);
    
    imageDisplay = findViewById(R.id.image_display);
    singerText = findViewById(R.id.singer_text);
    songName = findViewById(R.id.title_text);

    // 从Intent中取出传递的数据
    Intent intent = getIntent();
    String songTitle = intent.getStringExtra("SONG_TITLE");
    String songSinger = intent.getStringExtra("SONG_SINGER");
    int imageId = intent.getIntExtra("SONG_IMAGE_ID", 0); // 0是默认占位值

    // 给控件赋值
    songName.setText(songTitle);
    singerText.setText(songSinger);
    imageDisplay.setImageResource(imageId);
}

方案2:让Song实现序列化,直接传递整个对象

如果Song字段多,逐个传太麻烦,可以让Song类实现序列化接口,直接传对象:

  1. 修改Song类实现Serializable(最简单的序列化方式):
import java.io.Serializable;

public class Song implements Serializable {
    private String mTitle;
    private String mDuration;
    private String mSinger;
    private int mImageId;

    // 构造方法和getter方法保持不变
}
  1. MainActivity中传递Song对象:
Song currentSong = new Song("夜曲", "4:15", "周杰伦", R.drawable.song_cover);
Intent intent = new Intent(MainActivity.this, SongDisplay.class);
intent.putExtra("CURRENT_SONG", currentSong);
startActivity(intent);
  1. SongDisplay中获取对象并调用get方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display);
    
    imageDisplay = findViewById(R.id.image_display);
    singerText = findViewById(R.id.singer_text);
    songName = findViewById(R.id.title_text);

    // 取出传递的Song对象
    Intent intent = getIntent();
    Song currentSong = (Song) intent.getSerializableExtra("CURRENT_SONG");

    // 直接调用Song的get方法设置UI
    songName.setText(currentSong.getTitle());
    singerText.setText(currentSong.getSinger());
    imageDisplay.setImageResource(currentSong.getmImageId());
}

问题2:实现SeekBar以分钟为单位显示歌曲时长

要让SeekBar显示分钟级的时长,核心是把秒数转换成mm:ss的格式,再绑定到SeekBar和TextView上。这里给你完整实现:

步骤1:布局中添加SeekBar和时长显示TextView

<!-- activity_display.xml 中新增控件 -->
<SeekBar
    android:id="@+id/song_seekbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"/>

<TextView
    android:id="@+id/display_duration"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="0:00 / 4:15"
    android:layout_marginTop="10dp"/>

步骤2:在SongDisplay中处理时长转换和SeekBar逻辑

public class SongDisplay extends AppCompatActivity {
    // 新增控件引用
    SeekBar songSeekBar;
    TextView durationText;
    private int totalDuration; // 歌曲总时长(秒)

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);
        
        // 初始化其他控件...
        songSeekBar = findViewById(R.id.song_seekbar);
        durationText = findViewById(R.id.display_duration);

        // 假设你已经通过Intent拿到了Song对象
        Intent intent = getIntent();
        Song currentSong = (Song) intent.getSerializableExtra("CURRENT_SONG");
        // 把Song里的"4:15"格式转换成秒数
        totalDuration = convertDurationToSeconds(currentSong.getDuration());

        // 设置SeekBar最大值为总秒数
        songSeekBar.setMax(totalDuration);
        // 初始化时长显示
        String totalTime = convertSecondsToMinute(totalDuration);
        durationText.setText("0:00 / " + totalTime);

        // 监听SeekBar进度变化
        songSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // 把当前进度转换成分钟格式
                String currentTime = convertSecondsToMinute(progress);
                durationText.setText(currentTime + " / " + totalTime);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });
    }

    // 工具方法:把"mm:ss"格式转换成秒数
    private int convertDurationToSeconds(String duration) {
        String[] parts = duration.split(":");
        int minutes = Integer.parseInt(parts[0]);
        int seconds = Integer.parseInt(parts[1]);
        return minutes * 60 + seconds;
    }

    // 工具方法:把秒数转换成"mm:ss"格式
    private String convertSecondsToMinute(int seconds) {
        int minutes = seconds / 60;
        int remainingSeconds = seconds % 60;
        // 格式化秒数,不足两位补0
        return String.format("%d:%02d", minutes, remainingSeconds);
    }
}

内容的提问来源于stack exchange,提问作者Dedeepya.S

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:24:37