KEMBAR78
Fichier JAVA | PDF | Computer Data | Software Engineering
0% found this document useful (0 votes)
18 views8 pages

Fichier JAVA

Uploaded by

Hypolite Ntambwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views8 pages

Fichier JAVA

Uploaded by

Hypolite Ntambwe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

AllSongsAdapter.

java

package com.example.musicapp;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;

public class AllSongsAdapter extends RecyclerView.Adapter<AllSongsAdapter.ViewHolder> {

private Context context;


private List<Song> songList;

public AllSongsAdapter(Context context, List<Song> songList) {


this.context = context;
this.songList = songList;
}

public static class ViewHolder extends RecyclerView.ViewHolder {


TextView title, artist;

public ViewHolder(View itemView) {


super(itemView);
title = itemView.findViewById(R.id.songTitle);
artist = itemView.findViewById(R.id.songArtist);
}
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_song, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Song song = songList.get(position);
holder.title.setText(song.getTitle());
holder.artist.setText(song.getArtist());

holder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(context, PlayerActivity.class);
intent.putExtra("title", song.getTitle());
intent.putExtra("artist", song.getArtist());
intent.putExtra("path", song.getPath());
context.startActivity(intent);
});
}

@Override
public int getItemCount() {
return songList.size();
}
}

AllSongsFragment.java

package com.example.musicapp;

import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;

public class AllSongsFragment extends Fragment {

private RecyclerView recyclerView;


private AllSongsAdapter adapter;
private ArrayList<Song> songList = new ArrayList<>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_all_songs, container, false);

recyclerView = view.findViewById(R.id.recyclerViewAllSongs);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

loadSongs();

adapter = new AllSongsAdapter(getContext(), songList);


recyclerView.setAdapter(adapter);

return view;
}

private void loadSongs() {


ContentResolver contentResolver = requireActivity().getContentResolver();
String[] projection = {
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DATA
};

try (Cursor cursor = contentResolver.query(


MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Audio.Media.IS_MUSIC + "!= 0",
null,
MediaStore.Audio.Media.TITLE + " ASC"
)) {
if (cursor != null) {
while (cursor.moveToNext()) {
String title = cursor.getString(0);
String artist = cursor.getString(1);
String path = cursor.getString(2);
songList.add(new Song(title, artist, path));
}
}
}
}
}
MainActivity.java

package com.example.musicapp;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Demande des permissions


requestStoragePermission();

// Charger directement le fragment des musiques


if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new AllSongsFragment())
.commit();
}
}

private void requestStoragePermission() {


if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
100);
}
}
}
Song.java

package com.example.musicapp;

public class Song {


private String title;
private String artist;
private String path;
private long duration;
private String album;

public Song(String title, String artist, String path) {


this.title = title;
this.artist = artist;
this.path = path;
}

public Song(String title, String artist, String path, long duration, String album) {
this.title = title;
this.artist = artist;
this.path = path;
this.duration = duration;
this.album = album;
}

public String getTitle() {


return title;
}

public String getArtist() {


return artist;
}

public String getPath() {


return path;
}

public long getDuration() {


return duration;
}

public String getAlbum() {


return album;
}
}
PlayerActivity.java

package com.example.musicapp;

import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class PlayerActivity extends AppCompatActivity {

private TextView titleTextView, artistTextView, currentTimeTextView, totalTimeTextView;


private SeekBar seekBar, volumeSeekBar;
private ImageButton playPauseBtn, backBtn;
private MediaPlayer mediaPlayer;
private Handler handler = new Handler();

private String path;


private boolean isPlaying = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player);

titleTextView = findViewById(R.id.titleTextView);
artistTextView = findViewById(R.id.artistTextView);
currentTimeTextView = findViewById(R.id.currentTime);

totalTimeTextView = findViewById(R.id.totalTime);
seekBar = findViewById(R.id.seekBar);
volumeSeekBar = findViewById(R.id.volumeSeekBar);

playPauseBtn = findViewById(R.id.playPauseBtn);
backBtn = findViewById(R.id.backBtn);
titleTextView.setText(getIntent().getStringExtra("title"));
artistTextView.setText(getIntent().getStringExtra("artist"));
path = getIntent().getStringExtra("path");

mediaPlayer = new MediaPlayer();


mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

try {
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}

totalTimeTextView.setText(milliSecondsToTimer(mediaPlayer.getDuration()));
seekBar.setMax(mediaPlayer.getDuration());

playPauseBtn.setOnClickListener(v -> {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
playPauseBtn.setImageResource(android.R.drawable.ic_media_play);
} else {
mediaPlayer.start();
playPauseBtn.setImageResource(android.R.drawable.ic_media_pause);
updateSeekBar();
}
});

backBtn.setOnClickListener(v -> finish());

seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser) mediaPlayer.seekTo(progress);
}
@Override public void onStartTrackingTouch(SeekBar seekBar) {}
@Override public void onStopTrackingTouch(SeekBar seekBar) {}
});

AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);


volumeSeekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
@Override public void onStartTrackingTouch(SeekBar seekBar) {}
@Override public void onStopTrackingTouch(SeekBar seekBar) {}
});
}

private void updateSeekBar() {


seekBar.setProgress(mediaPlayer.getCurrentPosition());
currentTimeTextView.setText(milliSecondsToTimer(mediaPlayer.getCurrentPosition()));
if(mediaPlayer.isPlaying()){
handler.postDelayed(this::updateSeekBar, 1000);
}
}

private String milliSecondsToTimer(long milliseconds){


return String.format("%02d:%02d",
TimeUnit.MILLISECONDS.toMinutes(milliseconds),
TimeUnit.MILLISECONDS.toSeconds(milliseconds) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(milliseconds)));
}

@Override
protected void onDestroy() {
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
super.onDestroy();
}

You might also like