Please I need help with this assignment. REQUIREMENTS 2 Create an Activity that
ID: 3816759 • Letter: P
Question
Please I need help with this assignment.
REQUIREMENTS
2 Create an Activity that will be your MediaPlayer
Buttons
Play Button
1disabled when music is playing
2enabled when music is stopped or paused
Stop Button
disabled when music is stopped
enabled when music is paused or playing
you should NOT call stop on the MediaPlayer, instead
pause the MediaPlayer
seek to the beginning of the song
Pause Button
disabled when music is paused or stopped
enabled when music is playing
TextViews
iSong Title (TextView)
iiArtist Name (TextView)
iiiLook at DownloaderService.tar to see how to get the filename by using the URL for the MP3
ivParse the filename to get the Song Title and Artist Name
ImageViews
Album Cover Image (ImageView)
if a song has not yet been downloaded, then use a default image (a good looking one) that you either need to create yourself or find somewhere else
if the song has been downloaded, then use this image that I have provided
you can place these in res/drawable/ directory (or res/drawable/xhdpi, or res/drawable/hdpi)
we haven’t used ImageViews in class, but you should be able to figure it out withinin a few minutes
dOptions Menu
a Download Button that will download this MP3
an Exit button that will
exit the app
stop the MediaPlayer service
don’t stop the Download Service
if the user exits leave the Activity by pressing the home key or back key, I am not sure whether or not the broadcastreceiver method is efficient or not. Then returns to the Activity, the Play/Stop Buttons should still work as expected
3 Create a Service to download the MP3 provided
This MUST extend IntentService
MP3 should be downloaded when the Download option is clicked
you may use either DownloadService or DownloadManager discussed in class, which can also be found in the textbooks
you can hardcode the link to the MP3
4 Create another Service to handle playback of your newly downloaded song
This should extend Service
This MUST run the MediaPlayer itself inside of a Service
iThe buttons in your Activity should make requests to the Service to play/stop/pause/etc
the state of the MediaPlayer should not change when the user presses the back key or home key
that is, if the MediaPlayer is playing music, leaving the Activity should not stop the music
5 Manage Notifications for the following events
download of the MP3 has started
download of the MP3 has completed
when the user clicks on this notification
clear the notification
launch your MediaPlayer Activity (hint: PendingIntent)
the song is playing
you shouldn’t allow this notification to be cleared
the notification should go away when playback is stopped or paused
when the user clicks on this notification
launch your MediaPlayer Activity
THINGS TO NOTE
Your Activity should not actually play the music itself, that’s what your Service is for. Clicking on the playback Buttons from within your Activity should make calls to the Service, and the Service should take the appropriate action. This prevents playback from being stopped when your Activity loses focus
Do not place the MP3 inside of your res/raw directory. You’re supposed to download it, then get the URI after the download, and use the URI to launch the MediaPlayer
=================================================================================================================
my proble is that i get error
.DownloadService.url' on a null object reference
This what I have done
Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.media.*;
public class MediaPlayerDemo extends JFrame {
private Player player;
private File file;
public MediaPlayerDemo()
{
super( "Demonstrating the Java Media Player" );
JButton openFile = new JButton( "Open file to play" );
openFile.addActionListener(
new ActionListener() {
public void actionPerformed( ActionEvent e )
{
openFile();
createPlayer();
}
}
);
getContentPane().add( openFile, BorderLayout.NORTH );
setSize( 300, 300 );
show();
}
private void openFile()
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(
JFileChooser.FILES_ONLY );
int result = fileChooser.showOpenDialog( this );
// user clicked Cancel button on dialog
if ( result == JFileChooser.CANCEL_OPTION )
file = null;
else
file = fileChooser.getSelectedFile();
}
private void createPlayer()
{
if ( file == null )
return;
removePreviousPlayer();
try {
// create a new player and add listener
player = Manager.createPlayer( file.toURL() );
player.addControllerListener( new EventHandler() );
player.start(); // start player
}
catch ( Exception e ){
JOptionPane.showMessageDialog( this,
"Invalid file or location", "Error loading file",
JOptionPane.ERROR_MESSAGE );
}
}
private void removePreviousPlayer()
{
if ( player == null )
return;
player.close();
Component visual = player.getVisualComponent();
Component control = player.getControlPanelComponent();
Container c = getContentPane();
if ( visual != null )
c.remove( visual );
if ( control != null )
c.remove( control );
}
public static void main(String args[])
{
MediaPlayerDemo app = new MediaPlayerDemo();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
}
);
}
// inner class to handler events from media player
private class EventHandler implements ControllerListener {
public void controllerUpdate( ControllerEvent e ) {
if ( e instanceof RealizeCompleteEvent ) {
Container c = getContentPane();
// load Visual and Control components if they exist
Component visualComponent =
player.getVisualComponent();
if ( visualComponent != null )
c.add( visualComponent, BorderLayout.CENTER );
Component controlsComponent =
player.getControlPanelComponent();
if ( controlsComponent != null )
c.add( controlsComponent, BorderLayout.SOUTH );
c.doLayout();
}
}
}
}